Compare commits
4 Commits
bugfix/upl
...
b189753c1e
Author | SHA1 | Date | |
---|---|---|---|
b189753c1e | |||
5fcc7c78bb | |||
c1e1ab081f | |||
4826805f57 |
@ -23,8 +23,7 @@
|
||||
"symfony/yaml": "7.1.*",
|
||||
"symfonycasts/tailwind-bundle": "^0.6.1",
|
||||
"twig/extra-bundle": "^2.12|^3.0",
|
||||
"twig/twig": "^2.12|^3.0",
|
||||
"ext-fileinfo": "*"
|
||||
"twig/twig": "^2.12|^3.0"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
|
@ -28,7 +28,7 @@ class HomeController extends AbstractController
|
||||
|
||||
return $this->render('home.html.twig', [
|
||||
'content' => $fileSystemService->getDirs($dirs),
|
||||
'fileForm' => $this->createForm(UploadFileForm::class, data: ['dirs' => $dirs]),
|
||||
'fileForm' => $this->createForm(UploadFileForm::class),
|
||||
]);
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ namespace App\Controller;
|
||||
use App\Service\FileSystemService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
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
|
||||
{
|
||||
$file = $this->fileSystemService->getFile($filePath);
|
||||
|
@ -13,24 +13,17 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class UploadController extends AbstractController
|
||||
{
|
||||
#[Route(
|
||||
path: '/upload/{dirs?}',
|
||||
name: 'app_upload',
|
||||
requirements: ['dirs' => '.+'],
|
||||
defaults: ['dirs' => ''],
|
||||
methods: Request::METHOD_POST,
|
||||
)]
|
||||
#[Route(path: '/upload', name: 'app_upload', methods: Request::METHOD_POST)]
|
||||
public function __invoke(
|
||||
FileSystemService $fileSystemService,
|
||||
Request $request,
|
||||
string $dirs,
|
||||
): Response
|
||||
{
|
||||
$fileData = new UploadedFileData(dir: $dirs);
|
||||
$form = $this->createForm(UploadFileForm::class, $fileData)->handleRequest($request);
|
||||
$fileData = new UploadedFileData();
|
||||
$form = $this->createForm(UploadFileForm::class, $fileData)->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$fileSystemService->uploadFile($fileData);
|
||||
$fileSystemService->uploadFile($fileData->files);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_home');
|
||||
|
@ -16,7 +16,7 @@ class UploadFileForm extends AbstractType
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->setAction($this->urlGenerator->generate('app_upload', ['dirs' => $options['data']['dirs']]))
|
||||
->setAction($this->urlGenerator->generate('app_upload'))
|
||||
->add('files', FileType::class, [
|
||||
'attr' => ['class' => 'hidden'],
|
||||
'multiple' => true,
|
||||
|
@ -14,7 +14,6 @@ readonly class DirContent
|
||||
private string $type,
|
||||
private string $path,
|
||||
private string $content,
|
||||
private string $mimeType,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -25,8 +24,7 @@ readonly class DirContent
|
||||
$fileInfo->getSize() ?? 0,
|
||||
$fileInfo->getType() ?? 'N/A',
|
||||
$fileInfo->getPath(),
|
||||
$content,
|
||||
mime_content_type($fileInfo->getPath() . '/' . $fileInfo->getFilename()),
|
||||
$content
|
||||
);
|
||||
}
|
||||
|
||||
@ -59,11 +57,6 @@ readonly class DirContent
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
|
||||
private function getHumanReadableSize(): string
|
||||
{
|
||||
$bytes = $this->size;
|
||||
|
@ -6,13 +6,8 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
class UploadedFileData
|
||||
{
|
||||
public function __construct(
|
||||
/**
|
||||
* @var UploadedFile[] $files
|
||||
*/
|
||||
public array $files = [],
|
||||
|
||||
public string $dir = '',
|
||||
) {
|
||||
}
|
||||
/**
|
||||
* @var UploadedFile[] $files
|
||||
*/
|
||||
public array $files;
|
||||
}
|
@ -3,20 +3,18 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\Objects\DirContent;
|
||||
use App\Objects\UploadedFileData;
|
||||
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;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
class FileSystemService
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(env: 'DATA_DIR')]
|
||||
private readonly string $dir,
|
||||
private readonly Filesystem $filesystem,
|
||||
private readonly Filesystem $filesystem,
|
||||
) {
|
||||
}
|
||||
|
||||
@ -37,11 +35,13 @@ class FileSystemService
|
||||
return $contents;
|
||||
}
|
||||
|
||||
public function uploadFile(UploadedFileData $files): void
|
||||
/**
|
||||
* @param UploadedFile[] $files
|
||||
*/
|
||||
public function uploadFile(array $files): void
|
||||
{
|
||||
dd($files);
|
||||
foreach ($files->files as $file) {
|
||||
$this->filesystem->dumpFile($files->dir . $this->getTotalPath($file->getClientOriginalName()), $file->getContent());
|
||||
foreach ($files as $file) {
|
||||
$this->filesystem->dumpFile($this->getTotalPath($file->getClientOriginalName()), $file->getContent());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,7 @@
|
||||
</a>
|
||||
|
||||
<div class="mt-6 text-gray-700 overflow-auto max-h-96">
|
||||
{% if file.mimeType starts with 'image' %}
|
||||
<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 %}
|
||||
<p class="whitespace-pre-wrap leading-relaxed break-all text-balance">{{ file.content|raw }}</p>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
Reference in New Issue
Block a user