|
10 | 10 | use SplFileInfo; |
11 | 11 | use Symfony\Component\Filesystem\Filesystem; |
12 | 12 | use Symfony\Component\Finder\Finder; |
| 13 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
13 | 14 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 15 | +use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
14 | 16 | use Symfony\Component\String\Slugger\AsciiSlugger; |
15 | 17 | // use Symfony\Contracts\Translation\TranslatorInterface; |
16 | 18 |
|
17 | 19 | /** |
18 | 20 | * |
19 | 21 | * METHODS : |
| 22 | + * @method private getKernelDirectory(): string |
| 23 | + * @method public getDefaultDirectory(): string |
| 24 | + * @method public setDefaultDirectory(@var string $directory): static |
| 25 | + * @method public getRelativeDirectory(@var string $directory): string |
| 26 | + * @method public setRelativeDirectory(@var string $directory): static |
20 | 27 | * @method public getMimeTypes(): array |
21 | 28 | * @method public getMimeType(@var string $key): string|array|null |
22 | | - * @method public createSlug(@var string $text): string |
23 | | - * @method public createFile(@var string $path, @var string $content = '<!DOCTYPE...'): @return void |
| 29 | + * @method public getMimeContent(@var string $relativeFile): string |
| 30 | + * @method public getFileContent(@var string $relativeFile): string |
| 31 | + * @method public exists(@var string $filePath, @var bool $absolute = false): bool |
| 32 | + * @method public createSlug(@var string $string): string |
| 33 | + * @method public createFile(@var string $filename, @var string $content = '<!DOCTYPE html><html lang="en"><body style="background: #ffffff;"></body></html>'): @return void |
| 34 | + * @method public createDir(@var string $directory, @var bool $return = false): @return array |
| 35 | + * @method public categorizeFiles(@var array $files, @var bool $basename = false, @var bool $path = false): @return array |
| 36 | + * @method public getExtractedFolder(@var string $folder): @return string |
| 37 | + * @method public getExtByType(@var string $type): @return array |
24 | 38 | * @method public getDirs(@var string $path = '/', @var string $excludeDir = "", @var string|null $depth = '== 0'): @return array |
| 39 | + * @method public getDirsTree(@var string $path = '/', @var string $excludeDir = "", @var string|null $depth = '== 0'): @return array |
25 | 40 | * @method public getSliceDirs(@var string|array $dirs, @var int $slice, @var bool $implode = false): @return string|array |
26 | 41 | * @method public getFiles(@var string $path = '/', @var string|null $depth = '== 0'): @return array|bool |
27 | 42 | * @method public getSize(@var string|array $files, @var int $totalFileSize = 0): @return int|float |
28 | 43 | * @method public getSizeName(@var int|float $size): @return string |
29 | 44 | * @method public upload(@var UploadedFile|array $files, @var string $folder, @var bool $return = false): @return array|bool |
30 | 45 | * @method public resizeImages(@var array $files, @var string $sourceDir, @var string $targetDir, @var int $width, @var int $quality = 100): @return array|bool |
| 46 | + * @method public hasDir(): @return bool |
| 47 | + * @method public download(@var string $filename, @var ?string $directory): @return BinaryFileResponse |
| 48 | + * @method public downloadBulk(@var array $files, @var array $folders, @var ?string $directory): @return BinaryFileResponse |
| 49 | + * @method public remove(@var string $relativePath = ''): @return bool |
| 50 | + * @method public copy(@var string $source, @var string $destination, @var bool $override = false): @return bool |
| 51 | + * @method public move(@var string $newName, @var bool $override = false): @return bool |
31 | 52 | */ |
32 | 53 |
|
33 | 54 | class FileManagerService |
@@ -437,6 +458,45 @@ public function getDirs(string $path = '/', string $excludeDir = "", string|null |
437 | 458 | return $directories; |
438 | 459 | } |
439 | 460 |
|
| 461 | + public function getDirsTree(string $path = '/', string $excludeDir = "", string|null $depth = '== 0'): array |
| 462 | + { |
| 463 | + $realPath = realpath($this->getDefaultDirectory() . '/' . trim($path, '/')); |
| 464 | + |
| 465 | + if (!$realPath || !is_dir($realPath)) { |
| 466 | + return []; |
| 467 | + } |
| 468 | + |
| 469 | + $finder = new Finder(); |
| 470 | + if ($depth) { |
| 471 | + $finder->depth($depth); // Search only folders at the given depth $depth |
| 472 | + } |
| 473 | + $finder->directories()->in($realPath); // Search only folders at the root |
| 474 | + // $finder->directories()->in($realPath)->depth('== 0'); // seulement 1er niveau |
| 475 | + |
| 476 | + $directories = []; |
| 477 | + |
| 478 | + foreach ($finder as $dir) { |
| 479 | + $dirPath = $dir->getRealPath(); |
| 480 | + |
| 481 | + if ($excludeDir && str_contains($dirPath, $excludeDir)) { |
| 482 | + continue; |
| 483 | + } |
| 484 | + |
| 485 | + $relative = str_replace($this->getDefaultDirectory(), '', $dirPath); |
| 486 | + |
| 487 | + $directories[] = [ |
| 488 | + 'absolute' => $dirPath, |
| 489 | + 'relative' => $relative, |
| 490 | + 'ltrimed_relative' => ltrim($relative, '/'), |
| 491 | + 'foldername' => $dir->getFilename(), |
| 492 | + // appel récursif pour sous-dossiers |
| 493 | + 'children' => $this->getDirsTree($relative, $excludeDir), |
| 494 | + ]; |
| 495 | + } |
| 496 | + |
| 497 | + return $directories; |
| 498 | + } |
| 499 | + |
440 | 500 | /** |
441 | 501 | * Récupère des parties spécifiques des répertoires fournis et les concatène si nécessaire. |
442 | 502 | * |
@@ -502,7 +562,7 @@ public function cleanDir(string $dir = ''): void |
502 | 562 | $this->cleanDir($parentDir); |
503 | 563 | } |
504 | 564 | } |
505 | | - } |
| 565 | + } |
506 | 566 |
|
507 | 567 | /** |
508 | 568 | * Récupère la liste des fichiers d'un répertoire donné. |
@@ -838,6 +898,70 @@ public function hasDir(): bool |
838 | 898 | } |
839 | 899 | } |
840 | 900 |
|
| 901 | +/* **************************************************************************************************************************************************************** */ |
| 902 | + public function download(string $filename, ?string $directory = null): BinaryFileResponse |
| 903 | + { |
| 904 | + // Si aucun répertoire n'est fourni, on prend le defaultDirectory |
| 905 | + $baseDir = $directory |
| 906 | + ? $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . ltrim($directory, DIRECTORY_SEPARATOR) |
| 907 | + : $this->getDefaultDirectory(); |
| 908 | + |
| 909 | + $filePath = $baseDir . DIRECTORY_SEPARATOR . $filename; |
| 910 | + // dump($this->getKernelDirectory()); |
| 911 | + // dump($this->getDefaultDirectory()); |
| 912 | + // dd($filePath); |
| 913 | + |
| 914 | + if (!file_exists($filePath)) { |
| 915 | + throw new \RuntimeException(sprintf('Le fichier "%s" est introuvable.', str_replace($this->getDefaultDirectory(), "", $filePath))); |
| 916 | + } |
| 917 | + |
| 918 | + $response = new BinaryFileResponse($filePath); |
| 919 | + $response->setContentDisposition( |
| 920 | + ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
| 921 | + basename($filePath) |
| 922 | + ); |
| 923 | + |
| 924 | + return $response; |
| 925 | + } |
| 926 | + |
| 927 | + public function downloadBulk(array $filenames, array $folders, ?string $directory = null): BinaryFileResponse |
| 928 | + { |
| 929 | + $baseDir = $directory |
| 930 | + ? $this->getDefaultDirectory() . DIRECTORY_SEPARATOR . ltrim($directory, DIRECTORY_SEPARATOR) |
| 931 | + : $this->getDefaultDirectory(); |
| 932 | + |
| 933 | + // Créer un fichier zip temporaire |
| 934 | + $zipPath = sys_get_temp_dir() . '/files_' . uniqid() . '.zip'; |
| 935 | + $zip = new \ZipArchive(); |
| 936 | + |
| 937 | + if ($zip->open($zipPath, \ZipArchive::CREATE) !== true) { |
| 938 | + throw new \RuntimeException('Impossible de créer le ZIP.'); |
| 939 | + } |
| 940 | + |
| 941 | + foreach ($filenames as $filename) { |
| 942 | + $filePath = $baseDir . DIRECTORY_SEPARATOR . $filename; |
| 943 | + |
| 944 | + if (!file_exists($filePath)) { |
| 945 | + throw new \RuntimeException(sprintf('Le fichier "%s" est introuvable.', $filePath)); |
| 946 | + } |
| 947 | + |
| 948 | + // Ajouter le fichier au zip |
| 949 | + $zip->addFile($filePath, basename($filePath)); |
| 950 | + } |
| 951 | + |
| 952 | + $zip->close(); |
| 953 | + |
| 954 | + // Retourner le zip en téléchargement |
| 955 | + $response = new BinaryFileResponse($zipPath); |
| 956 | + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'files.zip'); |
| 957 | + |
| 958 | + // Supprimer le zip après envoi |
| 959 | + $response->deleteFileAfterSend(true); |
| 960 | + |
| 961 | + return $response; |
| 962 | + } |
| 963 | +/* **************************************************************************************************************************************************************** */ |
| 964 | + |
841 | 965 | public function remove(string $relativePath = ''): bool |
842 | 966 | { |
843 | 967 | if (empty($relativePath)) { |
|
0 commit comments