4 Commits

Author SHA1 Message Date
b189753c1e feat: add ServeFileController to serve files dynamically 2024-12-21 14:49:27 +01:00
5fcc7c78bb Merge pull request 'center files table' (#3) from sites/file-explorer:main into main
Reviewed-on: jank1619/file-explorer#3
2024-12-21 13:25:57 +00:00
c1e1ab081f style(home.html.twig): adjust centering of container element (#2)
All checks were successful
build / build (pull_request) Successful in 5m19s
Co-authored-by: Jan Klattenhoff <j.klattenhoff@neusta.de>
Reviewed-on: jank1619/file-explorer#2
2024-12-20 07:03:38 +00:00
4826805f57 Merge pull request 'main' (#1) from sites/file-explorer:main into main
Reviewed-on: jank1619/file-explorer#1
2024-12-20 06:54:18 +00:00
9 changed files with 22 additions and 45 deletions

View File

@ -23,8 +23,7 @@
"symfony/yaml": "7.1.*", "symfony/yaml": "7.1.*",
"symfonycasts/tailwind-bundle": "^0.6.1", "symfonycasts/tailwind-bundle": "^0.6.1",
"twig/extra-bundle": "^2.12|^3.0", "twig/extra-bundle": "^2.12|^3.0",
"twig/twig": "^2.12|^3.0", "twig/twig": "^2.12|^3.0"
"ext-fileinfo": "*"
}, },
"config": { "config": {
"allow-plugins": { "allow-plugins": {

View File

@ -28,7 +28,7 @@ class HomeController extends AbstractController
return $this->render('home.html.twig', [ return $this->render('home.html.twig', [
'content' => $fileSystemService->getDirs($dirs), 'content' => $fileSystemService->getDirs($dirs),
'fileForm' => $this->createForm(UploadFileForm::class, data: ['dirs' => $dirs]), 'fileForm' => $this->createForm(UploadFileForm::class),
]); ]);
} }
} }

View File

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Service\FileSystemService; use App\Service\FileSystemService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
@ -14,7 +15,7 @@ class ServeFileController extends AbstractController
{ {
} }
#[Route("/serve/{filePath}", name: "serve_file")] #[Route("/serve-file/{filePath}", name: "serve_file")]
public function __invoke(string $filePath): BinaryFileResponse public function __invoke(string $filePath): BinaryFileResponse
{ {
$file = $this->fileSystemService->getFile($filePath); $file = $this->fileSystemService->getFile($filePath);

View File

@ -13,24 +13,17 @@ use Symfony\Component\Routing\Attribute\Route;
class UploadController extends AbstractController class UploadController extends AbstractController
{ {
#[Route( #[Route(path: '/upload', name: 'app_upload', methods: Request::METHOD_POST)]
path: '/upload/{dirs?}',
name: 'app_upload',
requirements: ['dirs' => '.+'],
defaults: ['dirs' => ''],
methods: Request::METHOD_POST,
)]
public function __invoke( public function __invoke(
FileSystemService $fileSystemService, FileSystemService $fileSystemService,
Request $request, Request $request,
string $dirs,
): Response ): Response
{ {
$fileData = new UploadedFileData(dir: $dirs); $fileData = new UploadedFileData();
$form = $this->createForm(UploadFileForm::class, $fileData)->handleRequest($request); $form = $this->createForm(UploadFileForm::class, $fileData)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$fileSystemService->uploadFile($fileData); $fileSystemService->uploadFile($fileData->files);
} }
return $this->redirectToRoute('app_home'); return $this->redirectToRoute('app_home');

View File

@ -16,7 +16,7 @@ class UploadFileForm extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->setAction($this->urlGenerator->generate('app_upload', ['dirs' => $options['data']['dirs']])) ->setAction($this->urlGenerator->generate('app_upload'))
->add('files', FileType::class, [ ->add('files', FileType::class, [
'attr' => ['class' => 'hidden'], 'attr' => ['class' => 'hidden'],
'multiple' => true, 'multiple' => true,

View File

@ -14,7 +14,6 @@ readonly class DirContent
private string $type, private string $type,
private string $path, private string $path,
private string $content, private string $content,
private string $mimeType,
) { ) {
} }
@ -25,8 +24,7 @@ readonly class DirContent
$fileInfo->getSize() ?? 0, $fileInfo->getSize() ?? 0,
$fileInfo->getType() ?? 'N/A', $fileInfo->getType() ?? 'N/A',
$fileInfo->getPath(), $fileInfo->getPath(),
$content, $content
mime_content_type($fileInfo->getPath() . '/' . $fileInfo->getFilename()),
); );
} }
@ -59,11 +57,6 @@ readonly class DirContent
return $this->content; return $this->content;
} }
public function getMimeType(): string
{
return $this->mimeType;
}
private function getHumanReadableSize(): string private function getHumanReadableSize(): string
{ {
$bytes = $this->size; $bytes = $this->size;

View File

@ -6,13 +6,8 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadedFileData class UploadedFileData
{ {
public function __construct( /**
/** * @var UploadedFile[] $files
* @var UploadedFile[] $files */
*/ public array $files;
public array $files = [],
public string $dir = '',
) {
}
} }

View File

@ -3,20 +3,18 @@
namespace App\Service; namespace App\Service;
use App\Objects\DirContent; use App\Objects\DirContent;
use App\Objects\UploadedFileData;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\RequestStack;
class FileSystemService class FileSystemService
{ {
public function __construct( public function __construct(
#[Autowire(env: 'DATA_DIR')] #[Autowire(env: 'DATA_DIR')]
private readonly string $dir, private readonly string $dir,
private readonly Filesystem $filesystem, private readonly Filesystem $filesystem,
) { ) {
} }
@ -37,11 +35,13 @@ class FileSystemService
return $contents; return $contents;
} }
public function uploadFile(UploadedFileData $files): void /**
* @param UploadedFile[] $files
*/
public function uploadFile(array $files): void
{ {
dd($files); foreach ($files as $file) {
foreach ($files->files as $file) { $this->filesystem->dumpFile($this->getTotalPath($file->getClientOriginalName()), $file->getContent());
$this->filesystem->dumpFile($files->dir . $this->getTotalPath($file->getClientOriginalName()), $file->getContent());
} }
} }

View File

@ -15,11 +15,7 @@
</a> </a>
<div class="mt-6 text-gray-700 overflow-auto max-h-96"> <div class="mt-6 text-gray-700 overflow-auto max-h-96">
{% if file.mimeType starts with 'image' %} <p class="whitespace-pre-wrap leading-relaxed break-all text-balance">{{ file.content|raw }}</p>
<img src="{{ path('serve_file', {filePath: file.name}) }}" alt="">
{% else %}
<p class="whitespace-pre-wrap leading-relaxed break-all text-balance">{{ file.content|raw }}</p>
{% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}