FileUtils.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use App\Entity\Core\File;
  5. use Mimey\MimeTypes;
  6. use Path\Exception\FileNotFoundException;
  7. use Path\Exception\IOException;
  8. use Path\Path;
  9. use Symfony\Component\Config\FileLocator;
  10. class FileUtils
  11. {
  12. public function __construct()
  13. {
  14. }
  15. /**
  16. * Return a Path object
  17. * > Method is for testing purposes
  18. *
  19. * @see https://path-php.net/api
  20. *
  21. * @param string $path
  22. * @return Path
  23. */
  24. protected function makePath(string $path): Path
  25. {
  26. return new Path($path);
  27. }
  28. /**
  29. * Return a FileLocator object
  30. * > Method is for testing purposes
  31. *
  32. * @param array<string> $directories
  33. * @return FileLocator
  34. */
  35. protected function makeFileLocator(array $directories): FileLocator
  36. {
  37. return new FileLocator($directories);
  38. }
  39. /**
  40. * Return the mimetype corresponding to the given file extension.
  41. */
  42. public function getMimeTypeFromExt(string $ext): ?string
  43. {
  44. return (new MimeTypes())->getMimeType(ltrim($ext, '.'));
  45. }
  46. /**
  47. * Try to guess the mimetype from the filename.
  48. *
  49. * Return null if it did not manage to guess it.
  50. */
  51. public function guessMimeTypeFromFilename(string $filename): ?string
  52. {
  53. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  54. if (empty($ext)) {
  55. return null;
  56. }
  57. return $this->getMimeTypeFromExt($ext);
  58. }
  59. /**
  60. * Test si le fichier passé en paramètre est une image.
  61. */
  62. public function isImage(File $file): bool
  63. {
  64. $mimetype = $file->getMimeType() ?? $this->guessMimeTypeFromFilename($file->getName());
  65. return boolval(preg_match('#^image#', $mimetype ?? ''));
  66. }
  67. /**
  68. * Génère un nom de fichier temporaire situé dans le répertoire var/tmp,
  69. * avec l'extension et le préfixe donnés.
  70. *
  71. * @throws \RuntimeException
  72. */
  73. public function getTempFilename(string $ext = 'tmp', string $prefix = ''): string
  74. {
  75. if (empty($ext)) {
  76. throw new \RuntimeException('Extension can not be empty');
  77. }
  78. $tempDir = PathUtils::getProjectDir().'/var/tmp';
  79. $this->makePath($tempDir)->mkdir(0777, true);
  80. return PathUtils::join($tempDir, uniqid($prefix).'.'.$ext);
  81. }
  82. public function rmIfExist(string $path): void
  83. {
  84. $this->makePath($path)->remove_p();
  85. }
  86. /**
  87. * Reads the content of a file.
  88. *
  89. * @param string $path the path to the file to be read
  90. * @return string the content of the file
  91. * @throws \Path\Exception\FileNotFoundException
  92. * @throws \Path\Exception\IOException
  93. */
  94. public function getFileContent(string $path): string
  95. {
  96. return $this->makePath($path)->getContent();
  97. }
  98. /**
  99. * Locate a file in the given directories and return its absolute path, or
  100. * null if no file were found.
  101. *
  102. * @param array<string> $directories
  103. */
  104. public function locate(array $directories, string $filename): ?string
  105. {
  106. $fileLocator = $this->makeFileLocator($directories);
  107. return $fileLocator->locate($filename, null, false)[0] ?? null;
  108. }
  109. /**
  110. * Recursively removes a directory and all its contents.
  111. *
  112. * @param string $path the path to the directory to be removed
  113. *
  114. * @throws FileNotFoundException
  115. * @throws IOException
  116. */
  117. public function rmTree(string $path): void
  118. {
  119. $this->makePath($path)->rmdir(true);
  120. }
  121. /**
  122. * List the files located in the given directory.
  123. *
  124. * @param string $path
  125. * @param string $glob
  126. * @return list<string>
  127. * @throws FileNotFoundException
  128. * @throws IOException
  129. */
  130. public function list(string $path, string $glob = '*'): array
  131. {
  132. $paths = $this->makePath($path)->glob($glob);
  133. return array_map('strval', $paths);
  134. }
  135. }