forked from projects/file-explorer
46
src/Service/FileSystemService.php
Normal file
46
src/Service/FileSystemService.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Objects\DirContent;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
class FileSystemService
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'DATA_DIR')]
|
||||
private readonly string $dir,
|
||||
private readonly Filesystem $filesystem,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DirContent[]
|
||||
*/
|
||||
public function getDirs(): array
|
||||
{
|
||||
$finder = new Finder();
|
||||
$finder->in($this->dir);
|
||||
|
||||
$contents = [];
|
||||
|
||||
foreach ($finder->depth(0) as $content) {
|
||||
$contents[] = DirContent::make($content);
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UploadedFile[] $files
|
||||
*/
|
||||
public function uploadFile(array $files): void
|
||||
{
|
||||
foreach ($files as $file) {
|
||||
$this->filesystem->dumpFile($this->dir . '/' . $file->getClientOriginalName(), $file->getContent());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user