[CORE][GSFile] Use pathinfo rather than regular expressions and don't attempt to persist an already persisted entity

This commit is contained in:
Hugo Sales 2021-08-16 17:10:33 +01:00
parent 3843348c1b
commit 2351e7c6d1
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -27,8 +27,8 @@ use App\Entity\Attachment;
use App\Util\Common;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\NotStoredLocallyException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\NotStoredLocallyException;
use App\Util\Exception\ServerException;
use SplFileInfo;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@ -81,7 +81,6 @@ class GSFile
$attachment->setHeight($height);
$attachment->setSize($file->getSize());
$file->move(Common::config('attachments', 'dir'), $hash);
DB::persist($attachment);
}
} catch (NotFoundException) {
// Create an Attachment
@ -241,16 +240,24 @@ class GSFile
$valid_extensions = MimeTypes::getDefault()->getExtensions($mimetype);
// If title seems to be a filename with an extension
if (preg_match('/\.[a-z0-9]/i', $title) === 1) {
$title_without_extension = substr($title, 0, strrpos($title, '.'));
$original_extension = substr($title, strrpos($title, '.') + 1);
$pathinfo = pathinfo($title);
if ($pathinfo['extension'] ?? '' != '') {
$title_without_extension = $pathinfo['filename'];
$original_extension = $pathinfo['extension'];
if (empty(MimeTypes::getDefault()->getMimeTypes($original_extension)) || !in_array($original_extension, $valid_extensions)) {
unset($title_without_extension, $original_extension);
}
}
$fallback = function ($title) use ($ext) {
if (!is_null($ext)) {
return ($title) . ".{$ext}";
}
return null;
};
if ($force) {
return ($title_without_extension ?? $title) . ".{$ext}";
return $fallback($title_without_extension ?? $title);
} else {
if (isset($original_extension)) {
return $title;
@ -258,10 +265,9 @@ class GSFile
if (!empty($valid_extensions)) {
return "{$title}.{$valid_extensions[0]}";
} else {
if (!is_null($ext)) {
return ($title_without_extension ?? $title) . ".{$ext}";
}
return null;
// @codeCoverageIgnoreStart
return $fallback($title_without_extension ?? $title);
// @codeCoverageIgnoreEnd
}
}
}