[BUGFIX] Processed files get corrupted when regenerated
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ProcessedFile.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Benjamin Mack <benni@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Representation of a specific processed version of a file. These are created by the FileProcessingService,
32 * which in turn uses helper classes for doing the actual file processing. See there for a detailed description.
33 *
34 * Objects of this class may be freshly created during runtime or being fetched from the database. The latter
35 * indicates that the file has been processed earlier and was then cached.
36 *
37 * Each processed file—besides belonging to one file—has been created for a certain task (context) and
38 * configuration. All these won't change during the lifetime of a processed file; the only thing
39 * that can change is the original file, or rather it's contents. In that case, the processed file has to
40 * be processed again. Detecting this is done via comparing the current SHA1 hash of the original file against
41 * the one it had at the time the file was processed.
42 * The configuration of a processed file indicates what should be done to the original file to create the
43 * processed version. This may include things like cropping, scaling, rotating, flipping or using some special
44 * magic.
45 * A file may also meet the expectations set in the configuration without any processing. In that case, the
46 * ProcessedFile object still exists, but there is no physical file directly linked to it. Instead, it then
47 * redirects most method calls to the original file object. The data of these objects are also stored in the
48 * database, to indicate that no processing is required. With such files, the identifier and name fields in the
49 * database are empty to show this.
50 *
51 * @author Benjamin Mack <benni@typo3.org>
52 */
53 class ProcessedFile extends AbstractFile {
54
55 /*********************************************
56 * FILE PROCESSING CONTEXTS
57 *********************************************/
58 /**
59 * Basic processing context to get a processed image with smaller
60 * width/height to render a preview
61 */
62 const CONTEXT_IMAGEPREVIEW = 'Image.Preview';
63 /**
64 * Standard processing context for the frontend, that was previously
65 * in tslib_cObj::getImgResource which only takes cropping, masking and scaling
66 * into account
67 */
68 const CONTEXT_IMAGECROPSCALEMASK = 'Image.CropScaleMask';
69
70 /**
71 * Processing context, i.e. the type of processing done
72 *
73 * @var string
74 */
75 protected $taskType;
76
77 /**
78 * @var Processing\TaskInterface
79 */
80 protected $task;
81
82 /**
83 * @var Processing\TaskTypeRegistry
84 */
85 protected $taskTypeRegistry;
86
87 /**
88 * Processing configuration
89 *
90 * @var array
91 */
92 protected $processingConfiguration;
93
94 /**
95 * Reference to the original file this processed file has been created from.
96 *
97 * @var File
98 */
99 protected $originalFile;
100
101 /**
102 * The SHA1 hash of the original file this processed version has been created for.
103 * Is used for detecting changes if the original file has been changed and thus
104 * we have to recreate this processed file.
105 *
106 * @var string
107 */
108 protected $originalFileSha1;
109
110 /**
111 * A flag that shows if this object has been updated during its lifetime, i.e. the file has been
112 * replaced with a new one.
113 *
114 * @var boolean
115 */
116 protected $updated = FALSE;
117
118 /**
119 * Constructor for a processed file object. Should normally not be used
120 * directly, use the corresponding factory methods instead.
121 *
122 * @param File $originalFile
123 * @param string $taskType
124 * @param array $processingConfiguration
125 * @param array $databaseRow
126 */
127 public function __construct(File $originalFile, $taskType, array $processingConfiguration, array $databaseRow = NULL) {
128 $this->originalFile = $originalFile;
129 $this->storage = $originalFile->getStorage();
130 $this->taskType = $taskType;
131 $this->processingConfiguration = $processingConfiguration;
132 if (is_array($databaseRow)) {
133 $this->reconstituteFromDatabaseRecord($databaseRow);
134 }
135 $this->taskTypeRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Processing\\TaskTypeRegistry');
136 }
137
138 /**
139 * Creates a ProcessedFile object from a database record.
140 *
141 * @param array $databaseRow
142 * @return ProcessedFile
143 */
144 protected function reconstituteFromDatabaseRecord(array $databaseRow) {
145 $this->taskType = empty($this->taskType) ? $databaseRow['task_type'] : $this->taskType;
146 $this->processingConfiguration = empty($this->processingConfiguration) ? unserialize($databaseRow['configuration']) : $this->processingConfiguration;
147
148 $this->originalFileSha1 = $databaseRow['originalfilesha1'];
149 $this->identifier = $databaseRow['identifier'];
150 $this->name = $databaseRow['name'];
151 $this->properties = $databaseRow;
152 }
153
154 /********************************
155 * VARIOUS FILE PROPERTY GETTERS
156 ********************************/
157
158 /**
159 * Returns a unique checksum for this file's processing configuration and original file.
160 *
161 * @return string
162 */
163 // TODO replace these usages with direct calls to the task object
164 public function calculateChecksum() {
165 return $this->getTask()->getConfigurationChecksum();
166 }
167
168 /*******************
169 * CONTENTS RELATED
170 *******************/
171 /**
172 * Replace the current file contents with the given string
173 *
174 * @param string $contents The contents to write to the file.
175 * @return File The file object (allows chaining).
176 * @throws \BadMethodCallException
177 */
178 public function setContents($contents) {
179 throw new \BadMethodCallException('Setting contents not possible for processed file.', 1305438528);
180 }
181
182 /**
183 * Injects a local file, which is a processing result into the object.
184 *
185 * @param string $filePath
186 * @return void
187 * @throws \RuntimeException
188 */
189 public function updateWithLocalFile($filePath) {
190 if ($this->identifier === NULL) {
191 throw new \RuntimeException('Cannot update original file!', 1350582054);
192 }
193 // TODO this should be more generic (in fact it only works for local file paths)
194 $this->storage->addFile($filePath, $this->storage->getProcessingFolder(), $this->name, 'replace');
195 // Update some related properties
196 $this->originalFileSha1 = $this->originalFile->getSha1();
197 $this->deleted = FALSE;
198 $this->updated = TRUE;
199 }
200
201 /*****************************************
202 * STORAGE AND MANAGEMENT RELATED METHDOS
203 *****************************************/
204 /**
205 * Returns TRUE if this file is indexed
206 *
207 * @return boolean
208 */
209 public function isIndexed() {
210 // Processed files are never indexed; instead you might be looking for isPersisted()
211 return FALSE;
212 }
213
214 /**
215 * Checks whether the ProcessedFile already has an entry in sys_file_processedfile table
216 *
217 * @return boolean
218 */
219 public function isPersisted() {
220 return is_array($this->properties) && array_key_exists('uid', $this->properties) && $this->properties['uid'] > 0;
221 }
222
223 /**
224 * Checks whether the ProcessedFile Object is newly created
225 *
226 * @return boolean
227 */
228 public function isNew() {
229 return !$this->isPersisted();
230 }
231
232 /**
233 * Checks whether the object since last reconstitution, and therefore
234 * needs persistence again
235 *
236 * @return boolean
237 */
238 public function isUpdated() {
239 return $this->updated;
240 }
241
242 /**
243 * Sets a new file name
244 *
245 * @param string $name
246 */
247 public function setName($name) {
248 // Remove the existing file
249 if ($this->name !== $name && $this->name !== '' && $this->exists()) {
250 $this->delete();
251 }
252
253 $this->name = $name;
254 // TODO this is a *weird* hack that will fail if the storage is non-hierarchical!
255 $this->identifier = $this->storage->getProcessingFolder()->getIdentifier() . $this->name;
256
257 $this->updated = TRUE;
258 }
259
260 /******************
261 * SPECIAL METHODS
262 ******************/
263
264 /**
265 * Returns TRUE if this file is already processed.
266 *
267 * @return boolean
268 */
269 public function isProcessed() {
270 return ($this->isPersisted() && !$this->needsReprocessing()) || $this->updated;
271 }
272
273 /**
274 * Getter for the Original, unprocessed File
275 *
276 * @return File
277 */
278 public function getOriginalFile() {
279 return $this->originalFile;
280 }
281
282 /**
283 * Get the identifier of the file
284 *
285 * If there is no processed file in the file system (as the original file did not have to be modified e.g.
286 * when the original image is in the boundaries of the maxW/maxH stuff), then just return the identifier of
287 * the original file
288 *
289 * @return string
290 */
291 public function getIdentifier() {
292 return (!$this->usesOriginalFile()) ? $this->identifier : $this->getOriginalFile()->getIdentifier();
293 }
294
295 /**
296 * Get the name of the file
297 *
298 * If there is no processed file in the file system (as the original file did not have to be modified e.g.
299 * when the original image is in the boundaries of the maxW/maxH stuff)
300 * then just return the name of the original file
301 *
302 * @return string
303 */
304 public function getName() {
305 if ($this->usesOriginalFile()) {
306 return $this->originalFile->getName();
307 } else {
308 return $this->name;
309 }
310 }
311
312 /**
313 * Updates properties of this object. Do not use this to reconstitute an object from the database; use
314 * reconstituteFromDatabaseRecord() instead!
315 *
316 * @param array $properties
317 */
318 public function updateProperties(array $properties) {
319 if (!is_array($this->properties)) {
320 $this->properties = array();
321 }
322
323 if (array_key_exists('uid', $properties) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($properties['uid'])) {
324 $this->properties['uid'] = $properties['uid'];
325 }
326
327 // TODO we should have a blacklist of properties that might not be updated
328 $this->properties = array_merge($this->properties, $properties);
329
330 // TODO when should this update be done?
331 if (!$this->isUnchanged() && $this->exists()) {
332 $this->properties = array_merge($this->properties, $this->storage->getFileInfo($this));
333 }
334
335 }
336
337 /**
338 * Basic array function for the DB update
339 *
340 * @return array
341 */
342 public function toArray() {
343 if ($this->usesOriginalFile()) {
344 $properties = $this->originalFile->getProperties();
345 unset($properties['uid']);
346 unset($properties['pid']);
347 unset($properties['identifier']);
348 unset($properties['name']);
349 unset($properties['width']);
350 unset($properties['height']);
351 } else {
352 $properties = $this->properties;
353 $properties['identifier'] = $this->getIdentifier();
354 $properties['name'] = $this->getName();
355 }
356
357 return array_merge($properties, array(
358 'storage' => $this->getStorage()->getUid(),
359 'checksum' => $this->calculateChecksum(),
360 'task_type' => $this->taskType,
361 'configuration' => serialize($this->processingConfiguration),
362 'original' => $this->originalFile->getUid(),
363 'originalfilesha1' => $this->originalFileSha1
364 ));
365 }
366
367 /**
368 * Returns TRUE if this file has not been changed during processing (i.e., we just deliver the original file)
369 *
370 * @return boolean
371 */
372 protected function isUnchanged() {
373 return $this->identifier == NULL || $this->identifier === $this->originalFile->getIdentifier();
374 }
375
376 /**
377 * @return void
378 */
379 public function setUsesOriginalFile() {
380 // TODO check if some of these properties can/should be set in a generic update method
381 $this->identifier = $this->originalFile->getIdentifier();
382 $this->updated = TRUE;
383 $this->originalFileSha1 = $this->originalFile->getSha1();
384 }
385
386 /**
387 * @return boolean
388 */
389 public function usesOriginalFile() {
390 return $this->isUnchanged();
391 }
392
393 /**
394 * Returns TRUE if the original file of this file changed and the file should be processed again.
395 *
396 * @return boolean
397 */
398 public function isOutdated() {
399 return $this->needsReprocessing();
400 }
401
402 /**
403 * @return boolean
404 */
405 public function delete() {
406 if ($this->isUnchanged()) {
407 return FALSE;
408 }
409 return parent::delete();
410 }
411
412 /**
413 * Getter for file-properties
414 *
415 * @param string $key
416 *
417 * @return mixed
418 */
419 public function getProperty($key) {
420 // The uid always (!) has to come from this file and never the original file (see getOriginalFile() to get this)
421 if ($this->isUnchanged() && $key !== 'uid') {
422 return $this->originalFile->getProperty($key);
423 } else {
424 return $this->properties[$key];
425 }
426 }
427
428 /**
429 * Returns the uid of this file
430 *
431 * @return int
432 */
433 public function getUid() {
434 return $this->properties['uid'];
435 }
436
437
438 /**
439 * Checks if the ProcessedFile needs reprocessing
440 *
441 * @return boolean
442 */
443 public function needsReprocessing() {
444 $fileMustBeRecreated = FALSE;
445
446 // processedFile does not exist
447 if (!$this->usesOriginalFile() && !$this->exists()) {
448 $fileMustBeRecreated = TRUE;
449 }
450
451 // hash does not match
452 if (array_key_exists('checksum', $this->properties) && $this->calculateChecksum() !== $this->properties['checksum']) {
453 $fileMustBeRecreated = TRUE;
454 }
455
456 // original file changed
457 if ($this->originalFile->getSha1() !== $this->originalFileSha1) {
458 $fileMustBeRecreated = TRUE;
459 }
460
461 if (!array_key_exists('uid', $this->properties)) {
462 $fileMustBeRecreated = TRUE;
463 }
464
465 // remove outdated file
466 if ($fileMustBeRecreated && $this->exists()) {
467 $this->delete();
468 }
469 return $fileMustBeRecreated;
470 }
471
472 /**
473 * Returns the processing information
474 *
475 * @return array
476 */
477 public function getProcessingConfiguration() {
478 return $this->processingConfiguration;
479 }
480
481 /**
482 * Getter for the task identifier.
483 *
484 * @return string
485 */
486 public function getTaskIdentifier() {
487 return $this->taskType;
488 }
489
490 /**
491 * Returns the task object associated with this processed file.
492 *
493 * @return Processing\TaskInterface
494 * @throws \RuntimeException
495 */
496 public function getTask() {
497 if ($this->task == NULL) {
498 $this->task = $this->taskTypeRegistry->getTaskForType($this->taskType, $this, $this->processingConfiguration);
499 }
500
501 return $this->task;
502 }
503
504 /**
505 * Generate the name of of the new File
506 *
507 * @return string
508 */
509 public function generateProcessedFileNameWithoutExtension() {
510 $name = $this->originalFile->getNameWithoutExtension();
511 $name .= '_' . $this->originalFile->getUid();
512 $name .= '_' . $this->calculateChecksum();
513
514 return $name;
515 }
516
517 }
518
519 ?>