[COMPONENT][Attachment][TESTS] Fix Entity/AttachmentTest

This commit is contained in:
2022-03-07 17:08:55 +00:00
parent cc4f967186
commit 47f03d4c9f
4 changed files with 23 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotStoredLocallyException;
use App\Util\Exception\ServerException;
use App\Util\Exception\TemporaryFileException;
use App\Util\TemporaryFile;
use Component\Attachment\Entity\Attachment;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -55,8 +56,10 @@ class GSFile
{
/**
* Perform file validation (checks and normalization), store the given file if needed
* IMPORTANT: A new attachment is stored with 1 live, a known attachment has its lives incremented
*
* @throws DuplicateFoundException
* @throws TemporaryFileException
*/
public static function storeFileAsAttachment(TemporaryFile|SymfonyFile $file, bool $check_is_supported_mimetype = true): Attachment
{
@@ -65,8 +68,8 @@ class GSFile
try {
$attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
// Attachment Exists
// We had this attachment, but not the file, thus no filename, update meta
if (\is_null($attachment->getFilename())) {
// We had this attachment, but not the file, thus no filename, update meta
$mimetype = $attachment->getMimetype() ?? $file->getMimeType();
$width = $attachment->getWidth();
$height = $attachment->getHeight();
@@ -99,6 +102,8 @@ class GSFile
throw new FileNotAllowedException($mimetype);
}
}
// Increment lives
$attachment->livesIncrementAndGet();
} catch (NotFoundException) {
// Create an Attachment
// The following properly gets the mimetype with `file` or other
@@ -127,7 +132,7 @@ class GSFile
'size' => $file->getSize(),
'width' => $width,
'height' => $height,
'lives' => 0,
'lives' => 1,
]);
if (!$check_is_supported_mimetype || self::isMimetypeAllowed($mimetype)) {
$file->move(Common::config('attachments', 'dir'), $hash);