[PLUGIN][StoreRemoteMedia] Do not save empty files

This commit is contained in:
Diogo Peralta Cordeiro 2021-12-03 01:16:57 +00:00
parent 70ed04a7db
commit b66873e289
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 29 additions and 26 deletions

View File

@ -152,34 +152,37 @@ class StoreRemoteMedia extends Plugin
} }
} }
// Create an attachment for this // We can ignore empty files safely, the user can guess them (:
$temp_file = new TemporaryFile(); if (!empty($media)) {
$temp_file->write($media); // Create an attachment for this
$attachment = GSFile::storeFileAsAttachment($temp_file); $temp_file = new TemporaryFile();
$temp_file->write($media);
$attachment = GSFile::storeFileAsAttachment($temp_file);
// Relate the link with the attachment // 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 // TODO: Create a function that gets the title from content disposition or URL when such header isn't available
DB::persist(AttachmentToLink::create([ DB::persist(AttachmentToLink::create([
'link_id' => $link->getId(), 'link_id' => $link->getId(),
'attachment_id' => $attachment->getId(), 'attachment_id' => $attachment->getId(),
])); ]));
// Relate the note with the attachment // Relate the note with the attachment
DB::persist(AttachmentToNote::create([ DB::persist(AttachmentToNote::create([
'attachment_id' => $attachment->getId(), 'attachment_id' => $attachment->getId(),
'note_id' => $note->getId(), 'note_id' => $note->getId(),
])); ]));
DB::flush(); DB::flush();
// Should we create a thumb and delete the original file? // Should we create a thumb and delete the original file?
if (!$this->getStoreOriginal()) { if (!$this->getStoreOriginal()) {
$thumbnail = AttachmentThumbnail::getOrCreate( $thumbnail = AttachmentThumbnail::getOrCreate(
attachment: $attachment, attachment: $attachment,
size: 'medium', size: 'medium',
crop: $this->getSmartCrop(), crop: $this->getSmartCrop(),
); );
$attachment->deleteStorage(); $attachment->deleteStorage();
}
} }
return Event::stop; return Event::stop;

View File

@ -25,7 +25,6 @@ namespace App\Core;
use App\Core\DB\DB; use App\Core\DB\DB;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use Component\Attachment\Entity\Attachment;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\FileNotAllowedException; use App\Util\Exception\FileNotAllowedException;
@ -34,6 +33,7 @@ use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotStoredLocallyException; use App\Util\Exception\NotStoredLocallyException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\TemporaryFile; use App\Util\TemporaryFile;
use Component\Attachment\Entity\Attachment;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\HeaderUtils;
@ -68,7 +68,7 @@ class GSFile
$attachment->livesIncrementAndGet(); $attachment->livesIncrementAndGet();
// We had this attachment, but not the file, thus no filename, update meta // We had this attachment, but not the file, thus no filename, update meta
if (\is_null($attachment->getFilename())) { if (\is_null($attachment->getFilename())) {
$mimetype = $attachment->getMimetype(); $mimetype = $attachment->getMimetype() ?? $file->getMimeType();
$width = $attachment->getWidth(); $width = $attachment->getWidth();
$height = $attachment->getHeight(); $height = $attachment->getHeight();
$event_map[$mimetype] = []; $event_map[$mimetype] = [];