diff --git a/assets/controllers/upload_file_controller.js b/assets/controllers/upload_file_controller.js index 9c59c4b..dce5706 100644 --- a/assets/controllers/upload_file_controller.js +++ b/assets/controllers/upload_file_controller.js @@ -2,7 +2,6 @@ import { Controller } from '@hotwired/stimulus'; export default class extends Controller { connect() { - console.log(this.element); this.element.querySelector('input').addEventListener('change', () => { this.element.querySelector('button[type="submit"]').click(); }); diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 0b824d6..7d31df1 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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), diff --git a/src/Controller/UploadController.php b/src/Controller/UploadController.php index d10aa9d..3e8585d 100644 --- a/src/Controller/UploadController.php +++ b/src/Controller/UploadController.php @@ -23,7 +23,7 @@ class UploadController extends AbstractController $form = $this->createForm(UploadFileForm::class, $fileData)->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $fileSystemService->uploadFile($fileData->file); + $fileSystemService->uploadFile($fileData->files); } return $this->redirectToRoute('app_home'); diff --git a/src/Forms/UploadFileForm.php b/src/Forms/UploadFileForm.php index 698c549..0d0fe5e 100644 --- a/src/Forms/UploadFileForm.php +++ b/src/Forms/UploadFileForm.php @@ -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, ]); diff --git a/src/Objects/DirContent.php b/src/Objects/DirContent.php index 03e07e3..91d8d01 100644 --- a/src/Objects/DirContent.php +++ b/src/Objects/DirContent.php @@ -6,22 +6,25 @@ namespace App\Objects; use SplFileInfo; -class DirContent +readonly class DirContent { private function __construct( - private readonly string $name, - private readonly int $size, - private readonly string $type, - ) - { + private string $name, + 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 ); } @@ -44,10 +47,20 @@ class DirContent return $this->type; } - private function getHumanReadableSize() + public function getPath(): string + { + return $this->path; + } + + public function getContent(): string + { + return $this->content; + } + + private function getHumanReadableSize(): string { $bytes = $this->size; - $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); + $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; $factor = floor((strlen((string) $bytes) - 1) / 3); return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]); diff --git a/src/Objects/UploadedFileData.php b/src/Objects/UploadedFileData.php index 76b917a..fc3230e 100644 --- a/src/Objects/UploadedFileData.php +++ b/src/Objects/UploadedFileData.php @@ -7,7 +7,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; class UploadedFileData { /** - * @var UploadedFile[] $file + * @var UploadedFile[] $files */ - public array $file; + public array $files; } \ No newline at end of file diff --git a/src/Service/FileSystemService.php b/src/Service/FileSystemService.php index 6df4667..d7a194a 100644 --- a/src/Service/FileSystemService.php +++ b/src/Service/FileSystemService.php @@ -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; + } } \ No newline at end of file diff --git a/src/Service/Twig/RoutingService.php b/src/Service/Twig/RoutingService.php index fdca911..57268ac 100644 --- a/src/Service/Twig/RoutingService.php +++ b/src/Service/Twig/RoutingService.php @@ -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)]); } diff --git a/templates/_partials/_table.html.twig b/templates/_partials/_table.html.twig index 5f7c8ed..f8dc13d 100644 --- a/templates/_partials/_table.html.twig +++ b/templates/_partials/_table.html.twig @@ -27,14 +27,14 @@ {{ form_start(fileForm, {'attr': {'data-controller': 'upload-file'}}) }}
-
diff --git a/templates/file.html.twig b/templates/file.html.twig new file mode 100644 index 0000000..6ed481f --- /dev/null +++ b/templates/file.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block title %} + {{ file.name }} +{% endblock %} + +{% block body %} +
+ + + + + Back + + +
+

{{ file.content|raw }}

+
+
+{% endblock %} \ No newline at end of file