1
|
|
|
<?php |
2
|
|
|
namespace Aoe\Imgix\TYPO3; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2018 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use Aoe\Imgix\Domain\Service\ImagePurgeService; |
29
|
|
|
use TYPO3\CMS\Core\Resource\File; |
30
|
|
|
use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility; |
31
|
|
|
use TYPO3\CMS\Core\Utility\File\ExtendedFileUtilityProcessDataHookInterface; |
32
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
33
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
34
|
|
|
|
35
|
|
|
class FileUtilityProcessDataHook implements ExtendedFileUtilityProcessDataHookInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Configuration |
39
|
|
|
*/ |
40
|
|
|
private $configuration; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ImagePurgeService |
44
|
|
|
*/ |
45
|
|
|
private $imagePurgeService; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Configuration $configuration |
49
|
|
|
* @param ImagePurgeService $imagePurgeService |
50
|
|
|
*/ |
51
|
9 |
|
public function __construct(Configuration $configuration = null, ImagePurgeService $imagePurgeService = null) |
52
|
|
|
{ |
53
|
9 |
|
if ($configuration === null || $imagePurgeService === null) { |
54
|
|
|
// TYPO3 doesn't support Dependency-Injection in Hooks - so we must create the objects manually |
55
|
|
|
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
56
|
|
|
$configuration = $objectManager->get(Configuration::class); |
57
|
|
|
$imagePurgeService = $objectManager->get(ImagePurgeService::class); |
58
|
|
|
} |
59
|
9 |
|
$this->configuration = $configuration; |
60
|
9 |
|
$this->imagePurgeService = $imagePurgeService; |
61
|
9 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $action |
65
|
|
|
* @param array $cmdArr |
66
|
|
|
* @param array $result |
67
|
|
|
* @param ExtendedFileUtility $parentObject |
68
|
|
|
* @return void |
69
|
|
|
* @see \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::processData |
70
|
|
|
*/ |
71
|
2 |
|
public function processData_postProcessAction($action, array $cmdArr, array $result, ExtendedFileUtility $parentObject) |
72
|
|
|
{ |
73
|
2 |
|
$file = $this->getFile($result); |
74
|
2 |
|
if ($this->isPurgingOfImgixCacheRequired($action, $file)) { |
75
|
1 |
|
$this->imagePurgeService->purgeImgixCache($this->buildImgUrl($file)); |
|
|
|
|
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param File $file |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
2 |
|
protected function buildImgUrl(File $file) |
84
|
|
|
{ |
85
|
2 |
|
return sprintf('http://%s/%s', $this->configuration->getHost(), $file->getPublicUrl()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $result |
90
|
|
|
* @return File|null |
91
|
|
|
*/ |
92
|
3 |
|
protected function getFile(array $result) |
93
|
|
|
{ |
94
|
3 |
|
if (isset($result[0]) && is_array($result[0]) && isset($result[0][0]) && $result[0][0] instanceof File) { |
95
|
2 |
|
return $result[0][0]; |
96
|
|
|
} |
97
|
1 |
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* We must purge the imgix-cache, when: |
102
|
|
|
* - editor has updated an existing image (by uploading and overwriting an existing image) |
103
|
|
|
* - editor has replaced an existing image |
104
|
|
|
* |
105
|
|
|
* @param string $action |
106
|
|
|
* @param File|null $file |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
5 |
|
protected function isPurgingOfImgixCacheRequired($action, File $file = null) |
110
|
|
|
{ |
111
|
5 |
|
if ($file === null || $file->getType() !== File::FILETYPE_IMAGE) { |
112
|
1 |
|
return false; |
113
|
|
|
} |
114
|
4 |
|
if ($action === 'replace') { |
115
|
1 |
|
return true; |
116
|
|
|
} |
117
|
3 |
|
if ($action === 'upload' && count($file->getUpdatedProperties()) > 0) { |
118
|
|
|
// editor has updated an existing image (by uploading and overwriting an existing image) |
119
|
2 |
|
return true; |
120
|
|
|
} |
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|