mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-10 02:14:31 +02:00
Applied symplify rules to codebase.
This commit is contained in:
parent
2f20d90041
commit
388e847b17
136 changed files with 1370 additions and 789 deletions
|
@ -25,6 +25,8 @@ declare(strict_types=1);
|
|||
namespace App\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use SplFileInfo;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* This service contains basic commonly used functions to work with attachments.
|
||||
|
@ -47,16 +49,16 @@ class AttachmentManager
|
|||
*
|
||||
* @param Attachment $attachment The attachment for which the file should be generated
|
||||
*
|
||||
* @return \SplFileInfo|null The fileinfo for the attachment file. Null, if the attachment is external or has
|
||||
* invalid file.
|
||||
* @return SplFileInfo|null The fileinfo for the attachment file. Null, if the attachment is external or has
|
||||
* invalid file.
|
||||
*/
|
||||
public function attachmentToFile(Attachment $attachment): ?\SplFileInfo
|
||||
public function attachmentToFile(Attachment $attachment): ?SplFileInfo
|
||||
{
|
||||
if ($attachment->isExternal() || ! $this->isFileExisting($attachment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new \SplFileInfo($this->toAbsoluteFilePath($attachment));
|
||||
return new SplFileInfo($this->toAbsoluteFilePath($attachment));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,7 +109,7 @@ class AttachmentManager
|
|||
|
||||
$absolute_path = $this->toAbsoluteFilePath($attachment);
|
||||
|
||||
if ($absolute_path === null) {
|
||||
if (null === $absolute_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -155,7 +157,7 @@ class AttachmentManager
|
|||
//Taken from: https://www.php.net/manual/de/function.filesize.php#106569 and slightly modified
|
||||
|
||||
$sz = 'BKMGTP';
|
||||
$factor = (int) floor((\strlen($bytes) - 1) / 3);
|
||||
$factor = (int) floor((strlen($bytes) - 1) / 3);
|
||||
|
||||
return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor).@$sz[$factor];
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\Attachments;
|
||||
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
/**
|
||||
|
@ -106,7 +107,7 @@ class AttachmentPathResolver
|
|||
}
|
||||
|
||||
//Otherwise prepend the project path
|
||||
$tmp = realpath($this->project_dir.\DIRECTORY_SEPARATOR.$param_path);
|
||||
$tmp = realpath($this->project_dir.DIRECTORY_SEPARATOR.$param_path);
|
||||
|
||||
//If path does not exist then disable the placeholder
|
||||
if (false === $tmp) {
|
||||
|
@ -139,7 +140,7 @@ class AttachmentPathResolver
|
|||
}
|
||||
|
||||
//If we have now have a placeholder left, the string is invalid:
|
||||
if (preg_match('/%\w+%/', $placeholder_path)) {
|
||||
if (preg_match('#%\w+%#', $placeholder_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -183,7 +184,7 @@ class AttachmentPathResolver
|
|||
}
|
||||
|
||||
//If the new string does not begin with a placeholder, it is invalid
|
||||
if (! preg_match('/^%\w+%/', $real_path)) {
|
||||
if (! preg_match('#^%\w+%#', $real_path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,8 +25,10 @@ declare(strict_types=1);
|
|||
namespace App\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use function count;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
|
||||
use SplFileInfo;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
|
@ -52,11 +54,11 @@ class AttachmentReverseSearch
|
|||
/**
|
||||
* Find all attachments that use the given file.
|
||||
*
|
||||
* @param \SplFileInfo $file The file for which is searched
|
||||
* @param SplFileInfo $file The file for which is searched
|
||||
*
|
||||
* @return Attachment[] an list of attachments that use the given file
|
||||
*/
|
||||
public function findAttachmentsByFile(\SplFileInfo $file): array
|
||||
public function findAttachmentsByFile(SplFileInfo $file): array
|
||||
{
|
||||
//Path with %MEDIA%
|
||||
$relative_path_new = $this->pathResolver->realPathToPlaceholder($file->getPathname());
|
||||
|
@ -65,21 +67,23 @@ class AttachmentReverseSearch
|
|||
|
||||
$repo = $this->em->getRepository(Attachment::class);
|
||||
|
||||
return $repo->findBy(['path' => [$relative_path_new, $relative_path_old]]);
|
||||
return $repo->findBy([
|
||||
'path' => [$relative_path_new, $relative_path_old],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given file if it is not used by more than $threshold attachments.
|
||||
*
|
||||
* @param \SplFileInfo $file The file that should be removed
|
||||
* @param int $threshold the threshold used, to determine if a file should be deleted or not
|
||||
* @param SplFileInfo $file The file that should be removed
|
||||
* @param int $threshold the threshold used, to determine if a file should be deleted or not
|
||||
*
|
||||
* @return bool True, if the file was delete. False if not.
|
||||
*/
|
||||
public function deleteIfNotUsed(\SplFileInfo $file, int $threshold = 1): bool
|
||||
public function deleteIfNotUsed(SplFileInfo $file, int $threshold = 1): bool
|
||||
{
|
||||
/* When the file is used more then $threshold times, don't delete it */
|
||||
if (\count($this->findAttachmentsByFile($file)) > $threshold) {
|
||||
if (count($this->findAttachmentsByFile($file)) > $threshold) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,10 @@ use App\Entity\Attachments\StorelocationAttachment;
|
|||
use App\Entity\Attachments\SupplierAttachment;
|
||||
use App\Entity\Attachments\UserAttachment;
|
||||
use App\Exceptions\AttachmentDownloadException;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use function get_class;
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\Mime\MimeTypesInterface;
|
||||
|
@ -66,12 +70,20 @@ class AttachmentSubmitHandler
|
|||
$this->mimeTypes = $mimeTypes;
|
||||
|
||||
//The mapping used to determine which folder will be used for an attachment type
|
||||
$this->folder_mapping = [PartAttachment::class => 'part', AttachmentTypeAttachment::class => 'attachment_type',
|
||||
CategoryAttachment::class => 'category', CurrencyAttachment::class => 'currency',
|
||||
DeviceAttachment::class => 'device', FootprintAttachment::class => 'footprint',
|
||||
GroupAttachment::class => 'group', ManufacturerAttachment::class => 'manufacturer',
|
||||
MeasurementUnitAttachment::class => 'measurement_unit', StorelocationAttachment::class => 'storelocation',
|
||||
SupplierAttachment::class => 'supplier', UserAttachment::class => 'user', ];
|
||||
$this->folder_mapping = [
|
||||
PartAttachment::class => 'part',
|
||||
AttachmentTypeAttachment::class => 'attachment_type',
|
||||
CategoryAttachment::class => 'category',
|
||||
CurrencyAttachment::class => 'currency',
|
||||
DeviceAttachment::class => 'device',
|
||||
FootprintAttachment::class => 'footprint',
|
||||
GroupAttachment::class => 'group',
|
||||
ManufacturerAttachment::class => 'manufacturer',
|
||||
MeasurementUnitAttachment::class => 'measurement_unit',
|
||||
StorelocationAttachment::class => 'storelocation',
|
||||
SupplierAttachment::class => 'supplier',
|
||||
UserAttachment::class => 'user',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,18 +129,18 @@ class AttachmentSubmitHandler
|
|||
}
|
||||
|
||||
//Ensure the given attachment class is known to mapping
|
||||
if (! isset($this->folder_mapping[\get_class($attachment)])) {
|
||||
throw new \InvalidArgumentException('The given attachment class is not known! The passed class was: '.\get_class($attachment));
|
||||
if (! isset($this->folder_mapping[get_class($attachment)])) {
|
||||
throw new InvalidArgumentException('The given attachment class is not known! The passed class was: '.get_class($attachment));
|
||||
}
|
||||
//Ensure the attachment has an assigned element
|
||||
if (null === $attachment->getElement()) {
|
||||
throw new \InvalidArgumentException('The given attachment is not assigned to an element! An element is needed to generate a path!');
|
||||
throw new InvalidArgumentException('The given attachment is not assigned to an element! An element is needed to generate a path!');
|
||||
}
|
||||
|
||||
//Build path
|
||||
return
|
||||
$base_path.\DIRECTORY_SEPARATOR //Base path
|
||||
.$this->folder_mapping[\get_class($attachment)].\DIRECTORY_SEPARATOR.$attachment->getElement()->getID();
|
||||
$base_path.DIRECTORY_SEPARATOR //Base path
|
||||
.$this->folder_mapping[get_class($attachment)].DIRECTORY_SEPARATOR.$attachment->getElement()->getID();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -209,14 +221,14 @@ class AttachmentSubmitHandler
|
|||
|
||||
$filename = basename($old_path);
|
||||
//If the basename is not one of the new unique on, we have to save the old filename
|
||||
if (! preg_match('/\w+-\w{13}\./', $filename)) {
|
||||
if (! preg_match('#\w+-\w{13}\.#', $filename)) {
|
||||
//Save filename to attachment field
|
||||
$attachment->setFilename($attachment->getFilename());
|
||||
}
|
||||
|
||||
$ext = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
$new_path = $this->generateAttachmentPath($attachment, $secure_location)
|
||||
.\DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, $ext);
|
||||
.DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, $ext);
|
||||
|
||||
//Move file to new directory
|
||||
$fs = new Filesystem();
|
||||
|
@ -240,14 +252,14 @@ class AttachmentSubmitHandler
|
|||
{
|
||||
//Check if we are allowed to download files
|
||||
if (! $this->allow_attachments_downloads) {
|
||||
throw new \RuntimeException('Download of attachments is not allowed!');
|
||||
throw new RuntimeException('Download of attachments is not allowed!');
|
||||
}
|
||||
|
||||
$url = $attachment->getURL();
|
||||
|
||||
$fs = new Filesystem();
|
||||
$attachment_folder = $this->generateAttachmentPath($attachment, $options['secure_attachment']);
|
||||
$tmp_path = $attachment_folder.\DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'tmp');
|
||||
$tmp_path = $attachment_folder.DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'tmp');
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request('GET', $url, [
|
||||
|
@ -296,14 +308,14 @@ class AttachmentSubmitHandler
|
|||
}
|
||||
|
||||
//Rename the file to its new name and save path to attachment entity
|
||||
$new_path = $attachment_folder.\DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, $new_ext);
|
||||
$new_path = $attachment_folder.DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, $new_ext);
|
||||
$fs->rename($tmp_path, $new_path);
|
||||
|
||||
//Make our file path relative to %BASE%
|
||||
$new_path = $this->pathResolver->realPathToPlaceholder($new_path);
|
||||
//Save the path to the attachment
|
||||
$attachment->setPath($new_path);
|
||||
} catch (TransportExceptionInterface $exception) {
|
||||
} catch (TransportExceptionInterface $transportExceptionInterface) {
|
||||
throw new AttachmentDownloadException('Transport error!');
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,10 @@ declare(strict_types=1);
|
|||
namespace App\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use InvalidArgumentException;
|
||||
use Liip\ImagineBundle\Service\FilterService;
|
||||
use RuntimeException;
|
||||
use function strlen;
|
||||
use Symfony\Component\Asset\Packages;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
|
@ -76,7 +79,7 @@ class AttachmentURLGenerator
|
|||
}
|
||||
|
||||
//Return the part relative after public path.
|
||||
return substr($absolute_path, \strlen($public_path) + 1);
|
||||
return substr($absolute_path, strlen($public_path) + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,7 +89,7 @@ class AttachmentURLGenerator
|
|||
{
|
||||
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
|
||||
if (null === $absolute_path) {
|
||||
throw new \RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!
|
||||
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!
|
||||
Use Attachment::getURL() to get the external URL!');
|
||||
}
|
||||
|
||||
|
@ -106,7 +109,7 @@ class AttachmentURLGenerator
|
|||
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): string
|
||||
{
|
||||
if (! $attachment->isPicture()) {
|
||||
throw new \InvalidArgumentException('Thumbnail creation only works for picture attachments!');
|
||||
throw new InvalidArgumentException('Thumbnail creation only works for picture attachments!');
|
||||
}
|
||||
|
||||
if ($attachment->isExternal()) {
|
||||
|
@ -115,7 +118,7 @@ class AttachmentURLGenerator
|
|||
|
||||
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
|
||||
if (null === $absolute_path) {
|
||||
throw new \RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!');
|
||||
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!');
|
||||
}
|
||||
|
||||
$asset_path = $this->absolutePathToAssetPath($absolute_path);
|
||||
|
|
|
@ -25,6 +25,7 @@ declare(strict_types=1);
|
|||
namespace App\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
|
@ -77,7 +78,7 @@ class BuiltinAttachmentsFinder
|
|||
|
||||
return $results;
|
||||
});
|
||||
} catch (\Psr\Cache\InvalidArgumentException $ex) {
|
||||
} catch (InvalidArgumentException $invalidArgumentException) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ declare(strict_types=1);
|
|||
namespace App\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use function in_array;
|
||||
use Symfony\Component\Mime\MimeTypesInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
@ -72,9 +73,9 @@ class FileTypeFilterTools
|
|||
//Check for each element if it is valid:
|
||||
foreach ($elements as $element) {
|
||||
$element = trim($element);
|
||||
if (! preg_match('/^\.\w+$/', $element) // .ext is allowed
|
||||
&& ! preg_match('/^[-\w.]+\/[-\w.]+/', $element) //Explicit MIME type is allowed
|
||||
&& ! \in_array($element, static::ALLOWED_MIME_PLACEHOLDERS, false)) { //image/* is allowed
|
||||
if (! preg_match('#^\.\w+$#', $element) // .ext is allowed
|
||||
&& ! preg_match('#^[-\w.]+\/[-\w.]+#', $element) //Explicit MIME type is allowed
|
||||
&& ! in_array($element, static::ALLOWED_MIME_PLACEHOLDERS, false)) { //image/* is allowed
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -120,7 +121,7 @@ class FileTypeFilterTools
|
|||
$element = 'video/*';
|
||||
} elseif ('audio' === $element || 'audio/' === $element) {
|
||||
$element = 'audio/*';
|
||||
} elseif (! preg_match('/^[-\w.]+\/[-\w.*]+/', $element) && 0 !== strpos($element, '.')) {
|
||||
} elseif (! preg_match('#^[-\w.]+\/[-\w.*]+#', $element) && 0 !== strpos($element, '.')) {
|
||||
//Convert jpg to .jpg
|
||||
$element = '.'.$element;
|
||||
}
|
||||
|
@ -157,7 +158,7 @@ class FileTypeFilterTools
|
|||
$extensions = array_merge($extensions, static::AUDIO_EXTS);
|
||||
} elseif ('image/*' === $element) {
|
||||
$extensions = array_merge($extensions, static::VIDEO_EXTS);
|
||||
} elseif (preg_match('/^[-\w.]+\/[-\w.*]+/', $element)) {
|
||||
} elseif (preg_match('#^[-\w.]+\/[-\w.*]+#', $element)) {
|
||||
$extensions = array_merge($extensions, $this->mimeTypes->getExtensions($element));
|
||||
}
|
||||
}
|
||||
|
@ -178,6 +179,6 @@ class FileTypeFilterTools
|
|||
{
|
||||
$extension = strtolower($extension);
|
||||
|
||||
return empty($filter) || \in_array($extension, $this->resolveFileExtensions($filter), false);
|
||||
return empty($filter) || in_array($extension, $this->resolveFileExtensions($filter), false);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue