![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/CoreBundle/Helper/ |
<?php namespace Mautic\CoreBundle\Helper; use Mautic\CoreBundle\Exception\FilePathException; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\File\UploadedFile; class FilePathResolver { public function __construct( private Filesystem $filesystem, private InputHelper $inputHelper ) { } /** * @param string $uploadDir * * @throws FilePathException */ public function getUniqueFileName($uploadDir, UploadedFile $file): string { $inputHelper = $this->inputHelper; $fullName = $file->getClientOriginalName(); $fullNameSanitized = $inputHelper::filename($fullName); $ext = $this->getFileExtension($file); $baseFileName = pathinfo($fullNameSanitized, PATHINFO_FILENAME); $name = $baseFileName; $filePath = $this->getFilePath($uploadDir, $baseFileName, $ext); $i = 1; while ($this->filesystem->exists($filePath)) { $name = $baseFileName.'-'.$i; $filePath = $this->getFilePath($uploadDir, $name, $ext); ++$i; if ($i > 1000) { throw new FilePathException('Could not generate path'); } } return $name.$ext; } /** * @param string $directory * * @throws FilePathException */ public function createDirectory($directory): void { if ($this->filesystem->exists($directory)) { return; } try { $this->filesystem->mkdir($directory); } catch (IOException) { throw new FilePathException('Could not create directory'); } } /** * @param string $path */ public function delete($path): void { if (!$this->filesystem->exists($path)) { return; } try { $this->filesystem->remove($path); } catch (IOException) { } } public function move(string $originPath, string $targetPath): void { $this->filesystem->rename($originPath, $targetPath); } /** * @param string $uploadDir * @param string $fileName * @param string $ext */ private function getFilePath($uploadDir, $fileName, $ext): string { return $uploadDir.DIRECTORY_SEPARATOR.$fileName.$ext; } private function getFileExtension(UploadedFile $file): string { $ext = $file->getClientOriginalExtension(); return ('' === $ext ? '' : '.').$ext; } }