add ability to rename file in file manager

This commit is contained in:
Simon Vieille 2022-03-06 15:19:09 +01:00
parent 7355685009
commit a563ab3caf
6 changed files with 190 additions and 9 deletions

View file

@ -7,6 +7,7 @@ use App\Core\FileManager\FsFileManager;
use App\Core\Form\FileManager\DirectoryCreateType;
use App\Core\Form\FileManager\DirectoryRenameType;
use App\Core\Form\FileManager\FileInformationType;
use App\Core\Form\FileManager\FileRenameType;
use App\Core\Form\FileManager\FileUploadType;
use App\Core\Manager\EntityManager;
use Symfony\Component\HttpFoundation\Request;
@ -204,7 +205,9 @@ class FileManagerAdminController extends AdminController
]);
}
$form = $this->createForm(DirectoryRenameType::class);
$form = $this->createForm(DirectoryRenameType::class, [
'name' => $splInfo->getFilename(),
]);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
@ -252,6 +255,78 @@ class FileManagerAdminController extends AdminController
]);
}
/**
* @Route("/file/rename/{ajax}", name="admin_file_manager_file_rename", methods={"GET", "POST"})
*/
public function fileRename(FsFileManager $manager, Request $request, TranslatorInterface $translator, bool $ajax = false): Response
{
$splInfo = $manager->getSplInfo($request->query->get('file'));
if (!$splInfo) {
throw $this->createNotFoundException();
}
if ($splInfo->isDir()) {
throw $this->createNotFoundException();
}
if ($manager->isLocked($request->query->get('file'))) {
return $this->render('@Core/file_manager/file_rename.html.twig', [
'locked' => true,
]);
}
$form = $this->createForm(FileRenameType::class, [
'name' => preg_replace(sprintf('/\.%s/', $splInfo->getExtension()), '', $splInfo->getFilename()),
]);
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$status = $manager->renameFile($form->get('name')->getData(), $request->query->get('file'));
if (true === $status) {
if (!$request->isXmlHttpRequest()) {
$this->addFlash('success', 'File renamed.');
} else {
return $this->json([
'_error' => 0,
'_message' => $translator->trans('File renamed.'),
'_level' => 'success',
'_dispatch' => 'file_manager.file.rename.success',
]);
}
} else {
if (!$request->isXmlHttpRequest()) {
$this->addFlash('warning', 'File not renamed.');
} else {
return $this->json([
'_error' => 1,
'_message' => $translator->trans('File not renamed.'),
'_level' => 'warning',
'_dispatch' => 'file_manager.file.rename.error',
]);
}
}
} else {
$this->addFlash('warning', 'Unauthorized char(s).');
}
return $this->redirectToRoute('admin_file_manager_index', [
'path' => $splInfo->getRelativePath(),
]);
}
return $this->render('@Core/file_manager/file_rename.html.twig', [
'form' => $form->createView(),
'file' => $request->query->get('file'),
'exention' => $splInfo->getExtension(),
'locked' => false,
'ajax' => $ajax,
]);
}
/**
* @Route("/upload/{ajax}", name="admin_file_manager_upload", options={"expose"=true}, methods={"GET", "POST"})
*/

View file

@ -199,6 +199,30 @@ class FsFileManager
return true;
}
public function renameFile(string $name, string $path, bool $keepExtension = true): bool
{
$file = $this->getSplInfo($path);
if (!$file || $this->isLocked($path)) {
return false;
}
$filesystem = new Filesystem();
$newPath = $file->getPath().'/'.$this->normalizePath($name);
if ($keepExtension && $file->getExtension()) {
$newPath .= sprintf('.%s', $file->getExtension());
}
if ($filesystem->exists($newPath)) {
return false;
}
$filesystem->rename($file->getPathName(), $newPath);
return true;
}
public function upload($files, string $path, array $fullPaths = [])
{
if ($files instanceof UploadedFile) {

View file

@ -0,0 +1,40 @@
<?php
namespace App\Core\Form\FileManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
class FileRenameType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'name',
TextType::class,
[
'label' => 'Name',
'required' => true,
'attr' => [
],
'constraints' => [
new Regex([
'pattern' => '#['.preg_quote('\\/?%*:|"<>').'\t\n\r]+#',
'match' => false,
]),
new NotBlank(),
],
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
]);
}
}

View file

@ -192,8 +192,11 @@
"Directory not created.": "Répertoire non créé."
"Directory renamed.": "Répertoire renommé."
"Directory not renamed.": "Répertoire non renommé."
"File renamed.": "Fichier renommé."
"File not renamed.": "Fichier non renommé."
"Rename": "Renommer"
"Rename directory": "Renommer le répertoire"
"Rename file": "Renommer le fichier"
"New directory": "Nouveau répertoire"
"Preview": "Aperçu"
"Insert": "Insérer"

View file

@ -0,0 +1,33 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">
{{ 'Rename file'|trans }}
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
{% if locked %}
<p class="text-danger text-center">
<span class="d-block display-4 mb-3">
<span class="fa fa-lock"></span>
</span>
{{ 'File locked.'|trans }}
</p>
{% else %}
<form action="{{ path('admin_file_manager_file_rename', {file: file}) }}" {% if ajax %}data-form-ajax{% endif %} id="form-file-manager-directory" method="POST" enctype="multipart/form-data">
{{ include('@Core/file_manager/_form.html.twig') }}
</form>
{% endif %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
{% if not locked %}
<button type="submit" form="form-file-manager-directory" class="btn btn-primary">{{ 'Save'|trans }}</button>
{% endif %}
</div>
</div>
</div>

View file

@ -134,11 +134,13 @@
{% if context == 'tinymce' %}
{% if not isLocked %}
{% if splInfo.isDir %}
<button form="form-file-delete" class="btn btn-primary" data-modal="{{ path('admin_file_manager_directory_rename', {file: splInfo.relativePathname, ajax: ajax}) }}">
<button form="form-file-delete" class="btn btn-sm btn-primary" data-modal="{{ path('admin_file_manager_directory_rename', {file: splInfo.relativePathname, ajax: ajax}) }}">
{{ 'Rename'|trans }}
</button>
{% else %}
<span></span>
<button form="form-file-delete" class="btn btn-sm btn-primary" data-modal="{{ path('admin_file_manager_file_rename', {file: splInfo.relativePathname, ajax: ajax}) }}">
{{ 'Rename'|trans }}
</button>
{% endif %}
{% else %}
<div>
@ -149,23 +151,27 @@
{% endif %}
<div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Close'|trans }}</button>
<button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">{{ 'Close'|trans }}</button>
{% if splInfo.isFile %}
<button type="submit" class="btn btn-primary" form="form-fm-attributes">{{ 'Save'|trans }}</button>
<button type="button" class="btn btn-success ml-3" id="file-manager-insert" data-value="{{ asset(path) }}">{{ 'Insert'|trans }}</button>
<button type="submit" class="btn btn-sm btn-primary" form="form-fm-attributes">{{ 'Save'|trans }}</button>
<button type="button" class="btn btn-sm btn-success ml-3" id="file-manager-insert" data-value="{{ asset(path) }}">{{ 'Insert'|trans }}</button>
{% endif %}
</div>
{% else %}
<div>
{% if not isLocked %}
<button type="submit" form="form-fm-delete" class="btn btn-danger">
<button type="submit" form="form-fm-delete" class="btn btn-sm btn-danger">
{{ 'Delete'|trans }}
</button>
{% if splInfo.isDir %}
<button form="form-file-delete" class="btn btn-primary" data-modal="{{ path('admin_file_manager_directory_rename', {file: splInfo.relativePathname}) }}">
<button form="form-file-delete" class="btn btn-sm btn-primary" data-modal="{{ path('admin_file_manager_directory_rename', {file: splInfo.relativePathname}) }}">
{{ 'Rename'|trans }}
</button>
{% else %}
<button form="form-file-delete" class="btn btn-sm btn-primary" data-modal="{{ path('admin_file_manager_file_rename', {file: splInfo.relativePathname}) }}">
{{ 'Rename'|trans }}
</button>
{% endif %}
@ -176,7 +182,7 @@
{% endif %}
{% if splInfo.type == 'file' %}
<a class="btn btn-success" href="{{ asset(path) }}" target="_blank">
<a class="btn btn-sm btn-success" href="{{ asset(path) }}" target="_blank">
{{ 'Download'|trans }}
</a>
{% endif %}