[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
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 16 additions and 10 deletions

View File

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