From b66873e289f266c18c451cd64b9ce3fd5d0a729d Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Fri, 3 Dec 2021 01:16:57 +0000 Subject: [PATCH] [PLUGIN][StoreRemoteMedia] Do not save empty files --- plugins/StoreRemoteMedia/StoreRemoteMedia.php | 51 ++++++++++--------- src/Core/GSFile.php | 4 +- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/plugins/StoreRemoteMedia/StoreRemoteMedia.php b/plugins/StoreRemoteMedia/StoreRemoteMedia.php index 9c3edfcef1..3fdb427a83 100644 --- a/plugins/StoreRemoteMedia/StoreRemoteMedia.php +++ b/plugins/StoreRemoteMedia/StoreRemoteMedia.php @@ -152,34 +152,37 @@ class StoreRemoteMedia extends Plugin } } - // Create an attachment for this - $temp_file = new TemporaryFile(); - $temp_file->write($media); - $attachment = GSFile::storeFileAsAttachment($temp_file); + // We can ignore empty files safely, the user can guess them (: + if (!empty($media)) { + // Create an attachment for this + $temp_file = new TemporaryFile(); + $temp_file->write($media); + $attachment = GSFile::storeFileAsAttachment($temp_file); - // Relate the link with the attachment - // TODO: Create a function that gets the title from content disposition or URL when such header isn't available - DB::persist(AttachmentToLink::create([ - 'link_id' => $link->getId(), - 'attachment_id' => $attachment->getId(), - ])); + // Relate the link with the attachment + // TODO: Create a function that gets the title from content disposition or URL when such header isn't available + DB::persist(AttachmentToLink::create([ + 'link_id' => $link->getId(), + 'attachment_id' => $attachment->getId(), + ])); - // Relate the note with the attachment - DB::persist(AttachmentToNote::create([ - 'attachment_id' => $attachment->getId(), - 'note_id' => $note->getId(), - ])); + // Relate the note with the attachment + DB::persist(AttachmentToNote::create([ + 'attachment_id' => $attachment->getId(), + 'note_id' => $note->getId(), + ])); - DB::flush(); + DB::flush(); - // Should we create a thumb and delete the original file? - if (!$this->getStoreOriginal()) { - $thumbnail = AttachmentThumbnail::getOrCreate( - attachment: $attachment, - size: 'medium', - crop: $this->getSmartCrop(), - ); - $attachment->deleteStorage(); + // Should we create a thumb and delete the original file? + if (!$this->getStoreOriginal()) { + $thumbnail = AttachmentThumbnail::getOrCreate( + attachment: $attachment, + size: 'medium', + crop: $this->getSmartCrop(), + ); + $attachment->deleteStorage(); + } } return Event::stop; diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index cbd0a11bfc..c0116cedf2 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -25,7 +25,6 @@ namespace App\Core; use App\Core\DB\DB; use function App\Core\I18n\_m; -use Component\Attachment\Entity\Attachment; use App\Util\Common; use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\FileNotAllowedException; @@ -34,6 +33,7 @@ use App\Util\Exception\NotFoundException; use App\Util\Exception\NotStoredLocallyException; use App\Util\Exception\ServerException; use App\Util\TemporaryFile; +use Component\Attachment\Entity\Attachment; use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -68,7 +68,7 @@ class GSFile $attachment->livesIncrementAndGet(); // We had this attachment, but not the file, thus no filename, update meta if (\is_null($attachment->getFilename())) { - $mimetype = $attachment->getMimetype(); + $mimetype = $attachment->getMimetype() ?? $file->getMimeType(); $width = $attachment->getWidth(); $height = $attachment->getHeight(); $event_map[$mimetype] = [];