Skip to content

Commit 5f00578

Browse files
authored
Update FileManagerService.php
1 parent 54b999f commit 5f00578

File tree

1 file changed

+127
-3
lines changed

1 file changed

+127
-3
lines changed

src/Service/FileManagerService.php

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,45 @@
1010
use SplFileInfo;
1111
use Symfony\Component\Filesystem\Filesystem;
1212
use Symfony\Component\Finder\Finder;
13+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1314
use Symfony\Component\HttpFoundation\File\UploadedFile;
15+
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
1416
use Symfony\Component\String\Slugger\AsciiSlugger;
1517
// use Symfony\Contracts\Translation\TranslatorInterface;
1618

1719
/**
1820
*
1921
* 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
2027
* @method public getMimeTypes(): array
2128
* @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
2438
* @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
2540
* @method public getSliceDirs(@var string|array $dirs, @var int $slice, @var bool $implode = false): @return string|array
2641
* @method public getFiles(@var string $path = '/', @var string|null $depth = '== 0'): @return array|bool
2742
* @method public getSize(@var string|array $files, @var int $totalFileSize = 0): @return int|float
2843
* @method public getSizeName(@var int|float $size): @return string
2944
* @method public upload(@var UploadedFile|array $files, @var string $folder, @var bool $return = false): @return array|bool
3045
* @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
3152
*/
3253

3354
class FileManagerService
@@ -437,6 +458,45 @@ public function getDirs(string $path = '/', string $excludeDir = "", string|null
437458
return $directories;
438459
}
439460

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+
440500
/**
441501
* Récupère des parties spécifiques des répertoires fournis et les concatène si nécessaire.
442502
*
@@ -502,7 +562,7 @@ public function cleanDir(string $dir = ''): void
502562
$this->cleanDir($parentDir);
503563
}
504564
}
505-
}
565+
}
506566

507567
/**
508568
* Récupère la liste des fichiers d'un répertoire donné.
@@ -838,6 +898,70 @@ public function hasDir(): bool
838898
}
839899
}
840900

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+
841965
public function remove(string $relativePath = ''): bool
842966
{
843967
if (empty($relativePath)) {

0 commit comments

Comments
 (0)