| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Utils;
- use App\Entity\Core\File;
- use Mimey\MimeTypes;
- use Path\Exception\FileNotFoundException;
- use Path\Exception\IOException;
- use Path\Path;
- use Symfony\Component\Config\FileLocator;
- class FileUtils
- {
- public function __construct()
- {
- }
- /**
- * Return a Path object
- * > Method is for testing purposes
- *
- * @see https://path-php.net/api
- *
- * @param string $path
- * @return Path
- */
- protected function makePath(string $path): Path
- {
- return new Path($path);
- }
- /**
- * Return a FileLocator object
- * > Method is for testing purposes
- *
- * @param array<string> $directories
- * @return FileLocator
- */
- protected function makeFileLocator(array $directories): FileLocator
- {
- return new FileLocator($directories);
- }
- /**
- * Return the mimetype corresponding to the given file extension.
- */
- public function getMimeTypeFromExt(string $ext): ?string
- {
- return (new MimeTypes())->getMimeType(ltrim($ext, '.'));
- }
- /**
- * Try to guess the mimetype from the filename.
- *
- * Return null if it did not manage to guess it.
- */
- public function guessMimeTypeFromFilename(string $filename): ?string
- {
- $ext = pathinfo($filename, PATHINFO_EXTENSION);
- if (empty($ext)) {
- return null;
- }
- return $this->getMimeTypeFromExt($ext);
- }
- /**
- * Test si le fichier passé en paramètre est une image.
- */
- public function isImage(File $file): bool
- {
- $mimetype = $file->getMimeType() ?? $this->guessMimeTypeFromFilename($file->getName());
- return boolval(preg_match('#^image#', $mimetype ?? ''));
- }
- /**
- * Génère un nom de fichier temporaire situé dans le répertoire var/tmp,
- * avec l'extension et le préfixe donnés.
- *
- * @throws \RuntimeException
- */
- public function getTempFilename(string $ext = 'tmp', string $prefix = ''): string
- {
- if (empty($ext)) {
- throw new \RuntimeException('Extension can not be empty');
- }
- $tempDir = PathUtils::getProjectDir().'/var/tmp';
- $this->makePath($tempDir)->mkdir(0777, true);
- return PathUtils::join($tempDir, uniqid($prefix).'.'.$ext);
- }
- public function rmIfExist(string $path): void
- {
- $this->makePath($path)->remove_p();
- }
- /**
- * Reads the content of a file.
- *
- * @param string $path the path to the file to be read
- * @return string the content of the file
- * @throws \Path\Exception\FileNotFoundException
- * @throws \Path\Exception\IOException
- */
- public function getFileContent(string $path): string
- {
- return $this->makePath($path)->getContent();
- }
- /**
- * Locate a file in the given directories and return its absolute path, or
- * null if no file were found.
- *
- * @param array<string> $directories
- */
- public function locate(array $directories, string $filename): ?string
- {
- $fileLocator = $this->makeFileLocator($directories);
- return $fileLocator->locate($filename, null, false)[0] ?? null;
- }
- /**
- * Recursively removes a directory and all its contents.
- *
- * @param string $path the path to the directory to be removed
- *
- * @throws FileNotFoundException
- * @throws IOException
- */
- public function rmTree(string $path): void
- {
- $this->makePath($path)->rmdir(true);
- }
- /**
- * List the files located in the given directory.
- *
- * @param string $path
- * @param string $glob
- * @return list<string>
- * @throws FileNotFoundException
- * @throws IOException
- */
- public function list(string $path, string $glob = '*'): array
- {
- $paths = $this->makePath($path)->glob($glob);
- return array_map('strval', $paths);
- }
- }
|