add file preview

Reviewed-on: sites/file-explorer#6
This commit is contained in:
2024-12-06 13:04:56 +00:00
parent a574c3a89a
commit 12730b8bcd
10 changed files with 94 additions and 19 deletions

View File

@ -4,6 +4,7 @@ namespace App\Service;
use App\Objects\DirContent;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@ -23,7 +24,7 @@ class FileSystemService
public function getDirs(string $dirs): array
{
$finder = new Finder();
$finder->in($this->dir . '/' . $dirs);
$finder->in($this->getTotalPath($dirs));
$contents = [];
@ -40,7 +41,40 @@ class FileSystemService
public function uploadFile(array $files): void
{
foreach ($files as $file) {
$this->filesystem->dumpFile($this->dir . '/' . $file->getClientOriginalName(), $file->getContent());
$this->filesystem->dumpFile($this->getTotalPath($file->getClientOriginalName()), $file->getContent());
}
}
public function isFile(string $dirs): bool
{
try {
$this->filesystem->readFile($this->getTotalPath($dirs));
} catch (IOException) {
return false;
}
return true;
}
public function getFile(string $filePath): DirContent
{
$dirs = explode('/', $filePath);
$fileName = array_pop($dirs);
$finder = new Finder();
$finder->in($this->getTotalPath(implode('/', $dirs)));
foreach ($finder as $file) {
if ($file->getFilename() === $fileName) {
return DirContent::make($file, $this->filesystem->readFile($this->getTotalPath($filePath)));
}
}
throw new \RuntimeException('File not found');
}
private function getTotalPath(string $filePath): string
{
return $this->dir . '/' . $filePath;
}
}