[BUGFIX] ProcessedFile is persisted in sys_file as well
[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-2013 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 \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::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 $addedFile = $this->storage->addFile($filePath, $this->storage->getProcessingFolder(), $this->name, 'replace');
195 $addedFile->setIndexable(FALSE);
196
197 // Update some related properties
198 $this->identifier = $addedFile->getIdentifier();
199 $this->originalFileSha1 = $this->originalFile->getSha1();
200 $this->updateProperties($addedFile->getProperties());
201 $this->deleted = FALSE;
202 $this->updated = TRUE;
203 }
204
205 /*****************************************
206 * STORAGE AND MANAGEMENT RELATED METHDOS
207 *****************************************/
208 /**
209 * Returns TRUE if this file is indexed
210 *
211 * @return boolean
212 */
213 public function isIndexed() {
214 // Processed files are never indexed; instead you might be looking for isPersisted()
215 return FALSE;
216 }
217
218 /**
219 * Checks whether the ProcessedFile already has an entry in sys_file_processedfile table
220 *
221 * @return boolean
222 */
223 public function isPersisted() {
224 return is_array($this->properties) && array_key_exists('uid', $this->properties) && $this->properties['uid'] > 0;
225 }
226
227 /**
228 * Checks whether the ProcessedFile Object is newly created
229 *
230 * @return boolean
231 */
232 public function isNew() {
233 return !$this->isPersisted();
234 }
235
236 /**
237 * Checks whether the object since last reconstitution, and therefore
238 * needs persistence again
239 *
240 * @return boolean
241 */
242 public function isUpdated() {
243 return $this->updated;
244 }
245
246 /**
247 * Sets a new file name
248 *
249 * @param string $name
250 */
251 public function setName($name) {
252 // Remove the existing file
253 if ($this->name !== $name && $this->name !== '' && $this->exists()) {
254 $this->delete();
255 }
256
257 $this->name = $name;
258 // TODO this is a *weird* hack that will fail if the storage is non-hierarchical!
259 $this->identifier = $this->storage->getProcessingFolder()->getIdentifier() . $this->name;
260
261 $this->updated = TRUE;
262 }
263
264 /******************
265 * SPECIAL METHODS
266 ******************/
267
268 /**
269 * Returns TRUE if this file is already processed.
270 *
271 * @return boolean
272 */
273 public function isProcessed() {
274 return ($this->isPersisted() && !$this->needsReprocessing()) || $this->updated;
275 }
276
277 /**
278 * Getter for the Original, unprocessed File
279 *
280 * @return File
281 */
282 public function getOriginalFile() {
283 return $this->originalFile;
284 }
285
286 /**
287 * Get the identifier of the file
288 *
289 * If there is no processed file in the file system (as the original file did not have to be modified e.g.
290 * when the original image is in the boundaries of the maxW/maxH stuff), then just return the identifier of
291 * the original file
292 *
293 * @return string
294 */
295 public function getIdentifier() {
296 return (!$this->usesOriginalFile()) ? $this->identifier : $this->getOriginalFile()->getIdentifier();
297 }
298
299 /**
300 * Get the name of the file
301 *
302 * If there is no processed file in the file system (as the original file did not have to be modified e.g.
303 * when the original image is in the boundaries of the maxW/maxH stuff)
304 * then just return the name of the original file
305 *
306 * @return string
307 */
308 public function getName() {
309 if ($this->usesOriginalFile()) {
310 return $this->originalFile->getName();
311 } else {
312 return $this->name;
313 }
314 }
315
316 /**
317 * Updates properties of this object. Do not use this to reconstitute an object from the database; use
318 * reconstituteFromDatabaseRecord() instead!
319 *
320 * @param array $properties
321 */
322 public function updateProperties(array $properties) {
323 if (!is_array($this->properties)) {
324 $this->properties = array();
325 }
326
327 if (array_key_exists('uid', $properties) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($properties['uid'])) {
328 $this->properties['uid'] = $properties['uid'];
329 }
330
331 // TODO we should have a blacklist of properties that might not be updated
332 $this->properties = array_merge($this->properties, $properties);
333
334 // TODO when should this update be done?
335 if (!$this->isUnchanged() && $this->exists()) {
336 $this->properties = array_merge($this->properties, $this->storage->getFileInfo($this));
337 }
338
339 }
340
341 /**
342 * Basic array function for the DB update
343 *
344 * @return array
345 */
346 public function toArray() {
347 if ($this->usesOriginalFile()) {
348 $properties = $this->originalFile->getProperties();
349 unset($properties['uid']);
350 unset($properties['pid']);
351 unset($properties['identifier']);
352 unset($properties['name']);
353 unset($properties['width']);
354 unset($properties['height']);
355 } else {
356 $properties = $this->properties;
357 $properties['identifier'] = $this->getIdentifier();
358 $properties['name'] = $this->getName();
359 }
360
361 return array_merge($properties, array(
362 'storage' => $this->getStorage()->getUid(),
363 'checksum' => $this->calculateChecksum(),
364 'task_type' => $this->taskType,
365 'configuration' => serialize($this->processingConfiguration),
366 'original' => $this->originalFile->getUid(),
367 'originalfilesha1' => $this->originalFileSha1
368 ));
369 }
370
371 /**
372 * Returns TRUE if this file has not been changed during processing (i.e., we just deliver the original file)
373 *
374 * @return boolean
375 */
376 protected function isUnchanged() {
377 return $this->identifier == NULL || $this->identifier === $this->originalFile->getIdentifier();
378 }
379
380 /**
381 * @return void
382 */
383 public function setUsesOriginalFile() {
384 // TODO check if some of these properties can/should be set in a generic update method
385 $this->identifier = $this->originalFile->getIdentifier();
386 $this->updated = TRUE;
387 $this->originalFileSha1 = $this->originalFile->getSha1();
388 }
389
390 /**
391 * @return boolean
392 */
393 public function usesOriginalFile() {
394 return $this->isUnchanged();
395 }
396
397 /**
398 * Returns TRUE if the original file of this file changed and the file should be processed again.
399 *
400 * @return boolean
401 */
402 public function isOutdated() {
403 return $this->needsReprocessing();
404 }
405
406 /**
407 * @return boolean
408 */
409 public function delete() {
410 if ($this->isUnchanged()) {
411 return FALSE;
412 }
413 return parent::delete();
414 }
415
416 /**
417 * Getter for file-properties
418 *
419 * @param string $key
420 *
421 * @return mixed
422 */
423 public function getProperty($key) {
424 // The uid always (!) has to come from this file and never the original file (see getOriginalFile() to get this)
425 if ($this->isUnchanged() && $key !== 'uid') {
426 return $this->originalFile->getProperty($key);
427 } else {
428 return $this->properties[$key];
429 }
430 }
431
432 /**
433 * Returns the uid of this file
434 *
435 * @return int
436 */
437 public function getUid() {
438 return $this->properties['uid'];
439 }
440
441
442 /**
443 * Checks if the ProcessedFile needs reprocessing
444 *
445 * @return boolean
446 */
447 public function needsReprocessing() {
448 $fileMustBeRecreated = FALSE;
449
450 // processedFile does not exist
451 if (!$this->usesOriginalFile() && !$this->exists()) {
452 $fileMustBeRecreated = TRUE;
453 }
454
455 // hash does not match
456 if (array_key_exists('checksum', $this->properties) && $this->calculateChecksum() !== $this->properties['checksum']) {
457 $fileMustBeRecreated = TRUE;
458 }
459
460 // original file changed
461 if ($this->originalFile->getSha1() !== $this->originalFileSha1) {
462 $fileMustBeRecreated = TRUE;
463 }
464
465 if (!array_key_exists('uid', $this->properties)) {
466 $fileMustBeRecreated = TRUE;
467 }
468
469 // remove outdated file
470 if ($fileMustBeRecreated && $this->exists()) {
471 $this->delete();
472 }
473 return $fileMustBeRecreated;
474 }
475
476 /**
477 * Returns the processing information
478 *
479 * @return array
480 */
481 public function getProcessingConfiguration() {
482 return $this->processingConfiguration;
483 }
484
485 /**
486 * Getter for the task identifier.
487 *
488 * @return string
489 */
490 public function getTaskIdentifier() {
491 return $this->taskType;
492 }
493
494 /**
495 * Returns the task object associated with this processed file.
496 *
497 * @return Processing\TaskInterface
498 * @throws \RuntimeException
499 */
500 public function getTask() {
501 if ($this->task == NULL) {
502 $this->task = $this->taskTypeRegistry->getTaskForType($this->taskType, $this, $this->processingConfiguration);
503 }
504
505 return $this->task;
506 }
507
508 /**
509 * Generate the name of of the new File
510 *
511 * @return string
512 */
513 public function generateProcessedFileNameWithoutExtension() {
514 $name = $this->originalFile->getNameWithoutExtension();
515 $name .= '_' . $this->originalFile->getUid();
516 $name .= '_' . $this->calculateChecksum();
517
518 return $name;
519 }
520
521 }
522
523 ?>