[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
No known key found for this signature in database
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
$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;

View File

@ -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] = [];