]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Delete file on class destruction or we do it too quickly
authorMikael Nordfeldth <mmn@hethane.se>
Wed, 20 Jul 2016 22:23:27 +0000 (00:23 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Wed, 20 Jul 2016 22:23:27 +0000 (00:23 +0200)
Source image was removed when trying to use it for resizeTo

lib/imagefile.php

index ab59e16b253f15ff43f83cd7fb85ed6a4fdb5ebc..0d711ad9861d24a2a13b05b54173e1d97f3e5fe1 100644 (file)
@@ -120,6 +120,14 @@ class ImageFile
         Event::handle('FillImageFileMetadata', array($this));
     }
 
+    public function __destruct()
+    {
+        if (strlen($this->filepath) > 0 && (!$this->fileRecord instanceof File || $this->filepath !== $this->fileRecord->getPath())) {
+            common_debug(__METHOD__.': Deleting temporary file that was created as image file thumbnail source: '._ve($this->filepath));
+            @unlink($this->filepath);
+        }
+    }
+
     public static function fromFileObject(File $file)
     {
         $imgPath = null;
@@ -152,9 +160,6 @@ class ImageFile
         try {
             $image = new ImageFile($file->getID(), $imgPath);
         } catch (Exception $e) {
-            common_debug(sprintf('Exception caught when creating ImageFile for File id==%s and imgPath==', _ve($file->id), _ve($imgPath)));
-            throw $e;
-        } finally {
             // Avoid deleting the original
             try {
                 if (strlen($imgPath) > 0 && $imgPath !== $file->getPath()) {
@@ -166,6 +171,8 @@ class ImageFile
                 // doesn't exist anyway, so it's safe to delete $imgPath
                 @unlink($imgPath);
             }
+            common_debug(sprintf('Exception caught when creating ImageFile for File id==%s and imgPath==', _ve($file->id), _ve($imgPath)));
+            throw $e;
         }
         return $image;
     }
@@ -587,7 +594,7 @@ class ImageFile
         list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop);
 
         $thumb = File_thumbnail::pkeyGet(array(
-                                            'file_id'=> $this->fileRecord->id,
+                                            'file_id'=> $this->fileRecord->getID(),
                                             'width'  => $width,
                                             'height' => $height,
                                         ));
@@ -597,7 +604,7 @@ class ImageFile
 
         $filename = $this->fileRecord->filehash ?: $this->filename;    // Remote files don't have $this->filehash
         $extension = File::guessMimeExtension($this->mimetype);
-        $outname = "thumb-{$this->fileRecord->id}-{$width}x{$height}-{$filename}." . $extension;
+        $outname = "thumb-{$this->fileRecord->getID()}-{$width}x{$height}-{$filename}." . $extension;
         $outpath = File_thumbnail::path($outname);
 
         // The boundary box for our resizing
@@ -615,7 +622,7 @@ class ImageFile
             throw new ServerException('Bad thumbnail size parameters.');
         }
 
-        common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->fileRecord->id, $width, $height));
+        common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->fileRecord->getID(), $width, $height));
 
         // Perform resize and store into file
         $this->resizeTo($outpath, $box);