forked from projects/file-explorer
Compare commits
5 Commits
feature/do
...
feature/fi
Author | SHA1 | Date | |
---|---|---|---|
a760619d95
|
|||
6702147eb1
|
|||
b9aaff7931
|
|||
2123b5464f
|
|||
e70c6db10f
|
2
.env
2
.env
@ -19,4 +19,4 @@ APP_ENV=dev
|
|||||||
APP_SECRET=bfc9c288ee3dcce80dec8622c2870f27
|
APP_SECRET=bfc9c288ee3dcce80dec8622c2870f27
|
||||||
###< symfony/framework-bundle ###
|
###< symfony/framework-bundle ###
|
||||||
|
|
||||||
DATA_DIR=/var/www/data
|
DATA_DIR='/var/www/html/data'
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
name: build
|
name: build
|
||||||
on: push
|
on:
|
||||||
# pull_request:
|
pull_request:
|
||||||
# types:
|
types:
|
||||||
# - closed
|
- closed
|
||||||
# branches:
|
branches:
|
||||||
# - main
|
- main
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
@ -17,10 +17,4 @@ RUN docker-php-ext-install pdo pdo_mysql mysqli gd opcache intl zip calendar dom
|
|||||||
RUN pecl install apcu && docker-php-ext-enable apcu
|
RUN pecl install apcu && docker-php-ext-enable apcu
|
||||||
|
|
||||||
WORKDIR /var/www/project
|
WORKDIR /var/www/project
|
||||||
RUN composer install --optimize-autoloader --no-suggest --no-progress
|
RUN composer install --no-dev --optimize-autoloader --no-suggest --no-progress
|
||||||
RUN composer dump-env prod
|
|
||||||
RUN php bin/console tailwind:build
|
|
||||||
RUN php bin/console asset-map:compile
|
|
||||||
RUN mkdir -p /var/www/data
|
|
||||||
RUN chown -R www-data:www-data /var/www/data
|
|
||||||
RUN chown -R www-data:www-data .
|
|
||||||
|
@ -2,6 +2,7 @@ import { Controller } from '@hotwired/stimulus';
|
|||||||
|
|
||||||
export default class extends Controller {
|
export default class extends Controller {
|
||||||
connect() {
|
connect() {
|
||||||
|
console.log(this.element);
|
||||||
this.element.querySelector('input').addEventListener('change', () => {
|
this.element.querySelector('input').addEventListener('change', () => {
|
||||||
this.element.querySelector('button[type="submit"]').click();
|
this.element.querySelector('button[type="submit"]').click();
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
body {
|
body {
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
a:hover {
|
|
||||||
color: deepskyblue;
|
|
||||||
text-decoration: underline;
|
|
||||||
}
|
|
@ -1,7 +1,5 @@
|
|||||||
twig:
|
twig:
|
||||||
file_name_pattern: '*.twig'
|
file_name_pattern: '*.twig'
|
||||||
globals:
|
|
||||||
routing_service: '@App\Service\Twig\RoutingService'
|
|
||||||
|
|
||||||
when@test:
|
when@test:
|
||||||
twig:
|
twig:
|
||||||
|
@ -11,23 +11,11 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||||||
|
|
||||||
class HomeController extends AbstractController
|
class HomeController extends AbstractController
|
||||||
{
|
{
|
||||||
#[Route(
|
#[Route(path: '/files', name: 'app_home', methods: [Request::METHOD_GET])]
|
||||||
path: '/files/{dirs?}',
|
public function __invoke(FileSystemService $fileSystemService): Response
|
||||||
name: 'app_home',
|
|
||||||
requirements: ['dirs' => '.+'],
|
|
||||||
defaults: ['dirs' => ''],
|
|
||||||
methods: [Request::METHOD_GET]
|
|
||||||
)]
|
|
||||||
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', [
|
return $this->render('home.html.twig', [
|
||||||
'content' => $fileSystemService->getDirs($dirs),
|
'content' => $fileSystemService->getDirs(),
|
||||||
'fileForm' => $this->createForm(UploadFileForm::class),
|
'fileForm' => $this->createForm(UploadFileForm::class),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class UploadController extends AbstractController
|
|||||||
$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->files);
|
$fileSystemService->uploadFile($fileData->file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->redirectToRoute('app_home');
|
return $this->redirectToRoute('app_home');
|
||||||
|
@ -17,7 +17,7 @@ class UploadFileForm extends AbstractType
|
|||||||
{
|
{
|
||||||
$builder
|
$builder
|
||||||
->setAction($this->urlGenerator->generate('app_upload'))
|
->setAction($this->urlGenerator->generate('app_upload'))
|
||||||
->add('files', FileType::class, [
|
->add('file', FileType::class, [
|
||||||
'attr' => ['class' => 'hidden'],
|
'attr' => ['class' => 'hidden'],
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
]);
|
]);
|
||||||
|
@ -6,25 +6,22 @@ namespace App\Objects;
|
|||||||
|
|
||||||
use SplFileInfo;
|
use SplFileInfo;
|
||||||
|
|
||||||
readonly class DirContent
|
class DirContent
|
||||||
{
|
{
|
||||||
private function __construct(
|
private function __construct(
|
||||||
private string $name,
|
private readonly string $name,
|
||||||
private int $size,
|
private readonly int $size,
|
||||||
private string $type,
|
private readonly string $type,
|
||||||
private string $path,
|
)
|
||||||
private string $content,
|
{
|
||||||
) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function make(SplFileInfo $fileInfo, string $content = ''): DirContent
|
public static function make(SplFileInfo $fileInfo): DirContent
|
||||||
{
|
{
|
||||||
return new self(
|
return new self(
|
||||||
$fileInfo->getBasename(),
|
$fileInfo->getBasename(),
|
||||||
$fileInfo->getSize() ?? 0,
|
$fileInfo->getSize() ?? 0,
|
||||||
$fileInfo->getType() ?? 'N/A',
|
$fileInfo->getType() ?? 'N/A',
|
||||||
$fileInfo->getPath(),
|
|
||||||
$content
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,20 +44,10 @@ readonly class DirContent
|
|||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPath(): string
|
private function getHumanReadableSize()
|
||||||
{
|
|
||||||
return $this->path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getContent(): string
|
|
||||||
{
|
|
||||||
return $this->content;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getHumanReadableSize(): string
|
|
||||||
{
|
{
|
||||||
$bytes = $this->size;
|
$bytes = $this->size;
|
||||||
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
||||||
$factor = floor((strlen((string) $bytes) - 1) / 3);
|
$factor = floor((strlen((string) $bytes) - 1) / 3);
|
||||||
|
|
||||||
return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]);
|
return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]);
|
||||||
|
@ -7,7 +7,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|||||||
class UploadedFileData
|
class UploadedFileData
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var UploadedFile[] $files
|
* @var UploadedFile[] $file
|
||||||
*/
|
*/
|
||||||
public array $files;
|
public array $file;
|
||||||
}
|
}
|
@ -4,7 +4,6 @@ namespace App\Service;
|
|||||||
|
|
||||||
use App\Objects\DirContent;
|
use App\Objects\DirContent;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
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;
|
||||||
@ -21,10 +20,10 @@ class FileSystemService
|
|||||||
/**
|
/**
|
||||||
* @return DirContent[]
|
* @return DirContent[]
|
||||||
*/
|
*/
|
||||||
public function getDirs(string $dirs): array
|
public function getDirs(): array
|
||||||
{
|
{
|
||||||
$finder = new Finder();
|
$finder = new Finder();
|
||||||
$finder->in($this->getTotalPath($dirs));
|
$finder->in($this->dir);
|
||||||
|
|
||||||
$contents = [];
|
$contents = [];
|
||||||
|
|
||||||
@ -41,40 +40,7 @@ class FileSystemService
|
|||||||
public function uploadFile(array $files): void
|
public function uploadFile(array $files): void
|
||||||
{
|
{
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$this->filesystem->dumpFile($this->getTotalPath($file->getClientOriginalName()), $file->getContent());
|
$this->filesystem->dumpFile($this->dir . '/' . $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;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App\Service\Twig;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
||||||
|
|
||||||
class RoutingService
|
|
||||||
{
|
|
||||||
public function __construct(private UrlGeneratorInterface $urlGenerator)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function goBack(Request $request): string
|
|
||||||
{
|
|
||||||
$dirsString = $request->attributes->get('dirs', '');
|
|
||||||
$dirs = explode('/', $dirsString);
|
|
||||||
if (array_pop($dirs) === '') {
|
|
||||||
array_pop($dirs);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->urlGenerator->generate('app_home', ['dirs' => implode('/', $dirs)]);
|
|
||||||
}
|
|
||||||
}
|
|
23
src/test.php
23
src/test.php
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace App;
|
|
||||||
|
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
||||||
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
|
||||||
|
|
||||||
class test implements EventSubscriberInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
public static function getSubscribedEvents()
|
|
||||||
{
|
|
||||||
return [KernelEvents::EXCEPTION => 'x'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function x(ExceptionEvent $event)
|
|
||||||
{
|
|
||||||
dd($event->getThrowable());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +1,13 @@
|
|||||||
{% if file.name == '..' %}
|
|
||||||
{% set link = routing_service.goBack(app.request) %}
|
|
||||||
{% else %}
|
|
||||||
{% set link = path('app_home', {dirs: app.request.attributes.get('dirs', '/') ~ file.name ~ '/'}) %}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
|
|
||||||
<tr class="border-b">
|
<tr class="border-b">
|
||||||
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
||||||
<a href="{{ link }}">
|
{% if file.type == 'dir' %}
|
||||||
{% if file.type == 'dir' %}
|
{{ ux_icon('folder:closed', {height: '32px', width: '32px'}) }}
|
||||||
{{ ux_icon('folder:closed', {height: '32px', width: '32px'}) }}
|
{% else %}
|
||||||
{% else %}
|
{{ ux_icon('file:default', {height: '32px', width: '32px'}) }}
|
||||||
{{ ux_icon('file:default', {height: '32px', width: '32px'}) }}
|
{% endif %}
|
||||||
{% endif %}
|
|
||||||
</a>
|
|
||||||
</th>
|
</th>
|
||||||
<td class="px-6 py-4">
|
<td class="px-6 py-4">
|
||||||
<a href="{{ link }}">
|
{{ file.name }}
|
||||||
{{ file.name }}
|
|
||||||
</a>
|
|
||||||
</td>
|
</td>
|
||||||
<td class="px-6 py-4">
|
<td class="px-6 py-4">
|
||||||
{{ file.size }}
|
{{ file.size }}
|
||||||
|
@ -14,9 +14,6 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% if app.request.requestUri != '/files' %}
|
|
||||||
{% include '_partials/_row.html.twig' with {file: {name: '..', size: '-', type: 'dir'}} %}
|
|
||||||
{% endif %}
|
|
||||||
{% for dirContent in content %}
|
{% for dirContent in content %}
|
||||||
{% include '_partials/_row.html.twig' with {file: dirContent} %}
|
{% include '_partials/_row.html.twig' with {file: dirContent} %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@ -27,14 +24,12 @@
|
|||||||
|
|
||||||
{{ form_start(fileForm, {'attr': {'data-controller': 'upload-file'}}) }}
|
{{ form_start(fileForm, {'attr': {'data-controller': 'upload-file'}}) }}
|
||||||
<div class="flex items-center justify-center max-w-4xl mt-5">
|
<div class="flex items-center justify-center max-w-4xl mt-5">
|
||||||
<label for="upload_file_form_files"
|
<label for="upload_file_form_file" class="flex flex-col items-center justify-center w-full h-64 border-2 border-gray-300 border-dashed rounded-lg cursor-pointer bg-gray-50 dark:hover:bg-gray-800 dark:bg-gray-700 hover:bg-gray-100 dark:border-gray-600 dark:hover:border-gray-500 dark:hover:bg-gray-600">
|
||||||
class="flex flex-col items-center justify-center w-full h-64 border-2 border-gray-300 border-dashed rounded-lg cursor-pointer bg-gray-50 dark:hover:bg-gray-800 dark:bg-gray-700 hover:bg-gray-100 dark:border-gray-600 dark:hover:border-gray-500 dark:hover:bg-gray-600">
|
|
||||||
<div class="flex flex-col items-center justify-center pt-5 pb-6">
|
<div class="flex flex-col items-center justify-center pt-5 pb-6">
|
||||||
{{ ux_icon('file:upload', {height: '64px', width: '64px'}) }}
|
{{ ux_icon('file:upload', {height: '64px', width: '64px'}) }}
|
||||||
<p class="mb-2 text-sm text-gray-500 dark:text-gray-400"><span class="font-semibold">Click to upload</span>
|
<p class="mb-2 text-sm text-gray-500 dark:text-gray-400"><span class="font-semibold">Click to upload</span> or drag and drop</p>
|
||||||
or drag and drop</p>
|
|
||||||
</div>
|
</div>
|
||||||
{{ form_widget(fileForm.files) }}
|
{{ form_widget(fileForm.file) }}
|
||||||
<button type="submit" class="hidden"></button>
|
<button type="submit" class="hidden"></button>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
{% extends 'base.html.twig' %}
|
|
||||||
|
|
||||||
{% block title %}
|
|
||||||
{{ file.name }}
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
{% block body %}
|
|
||||||
<div class="max-w-2xl mx-auto my-8 p-6 bg-white shadow-lg rounded-lg border">
|
|
||||||
<a href="{{ routing_service.goBack(app.request) }}" class="inline-flex items-center text-blue-600 hover:text-blue-800 font-medium transition-colors">
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2"
|
|
||||||
stroke="currentColor" class="w-5 h-5 mr-2">
|
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7"/>
|
|
||||||
</svg>
|
|
||||||
Back
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<div class="mt-6 text-gray-700 overflow-auto max-h-96">
|
|
||||||
<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