diff --git a/assets/controllers/file_preview_controller.js b/assets/controllers/file_preview_controller.js new file mode 100644 index 0000000..7f2382c --- /dev/null +++ b/assets/controllers/file_preview_controller.js @@ -0,0 +1,8 @@ +import {Controller} from "@hotwired/stimulus"; + + +export default class extends Controller { + connect() { + + } +} \ No newline at end of file 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/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 342a6fd..91d8d01 100644 --- a/src/Objects/DirContent.php +++ b/src/Objects/DirContent.php @@ -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; 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