forked from projects/file-explorer
add file preview
This commit is contained in:
@ -20,6 +20,12 @@ class HomeController extends AbstractController
|
||||
)]
|
||||
public function __invoke(FileSystemService $fileSystemService, string $dirs): Response
|
||||
{
|
||||
if ($fileSystemService->isFile(substr_replace($dirs, '', -1))) {
|
||||
return $this->render('file.html.twig', [
|
||||
'file' => $fileSystemService->getFile((string) substr_replace($dirs, '', -1)),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->render('home.html.twig', [
|
||||
'content' => $fileSystemService->getDirs($dirs),
|
||||
'fileForm' => $this->createForm(UploadFileForm::class),
|
||||
|
@ -17,7 +17,7 @@ class UploadFileForm extends AbstractType
|
||||
{
|
||||
$builder
|
||||
->setAction($this->urlGenerator->generate('app_upload'))
|
||||
->add('file', FileType::class, [
|
||||
->add('files', FileType::class, [
|
||||
'attr' => ['class' => 'hidden'],
|
||||
'multiple' => true,
|
||||
]);
|
||||
|
@ -13,16 +13,18 @@ readonly class DirContent
|
||||
private int $size,
|
||||
private string $type,
|
||||
private string $path,
|
||||
private string $content,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function make(SplFileInfo $fileInfo): DirContent
|
||||
public static function make(SplFileInfo $fileInfo, string $content = ''): DirContent
|
||||
{
|
||||
return new self(
|
||||
$fileInfo->getBasename(),
|
||||
$fileInfo->getSize() ?? 0,
|
||||
$fileInfo->getType() ?? 'N/A',
|
||||
$fileInfo->getPath(),
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
@ -50,6 +52,11 @@ readonly class DirContent
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
private function getHumanReadableSize(): string
|
||||
{
|
||||
$bytes = $this->size;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -16,7 +16,9 @@ class RoutingService
|
||||
{
|
||||
$dirsString = $request->attributes->get('dirs', '');
|
||||
$dirs = explode('/', $dirsString);
|
||||
array_pop($dirs);
|
||||
if (array_pop($dirs) === '') {
|
||||
array_pop($dirs);
|
||||
}
|
||||
|
||||
return $this->urlGenerator->generate('app_home', ['dirs' => implode('/', $dirs)]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user