-
Notifications
You must be signed in to change notification settings - Fork 9
Added functionality for changing file names and their URI #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SHU8HAM1
wants to merge
1
commit into
mjordan:main
Choose a base branch
from
SHU8HAM1:Rename-Files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| rename files: | ||
| title: 'Rename files' | ||
| description: 'Rename files.' | ||
| restrict access: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/Controller/IslandoraWorkbenchIntegrationFileRenameController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| <?php | ||
|
|
||
| namespace Drupal\islandora_workbench_integration\Controller; | ||
|
|
||
| use Drupal\Core\Controller\ControllerBase; | ||
| use Symfony\Component\HttpFoundation\JsonResponse; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Drupal\file\Entity\File; | ||
| use Drupal\Core\File\FileSystemInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerInterface; | ||
| use Drupal\Core\File\Event\FileUploadSanitizeNameEvent; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
|
||
| class IslandoraWorkbenchIntegrationFileRenameController extends ControllerBase | ||
| { | ||
|
|
||
|
|
||
| /** | ||
| * FileSystem service. | ||
| * | ||
| * @var \Drupal\Core\File\FileSystemInterface | ||
| */ | ||
| protected $fileSystem; | ||
|
|
||
| /** | ||
| * EventDispatcher service. | ||
| * | ||
| * @var \Symfony\Component\EventDispatcher\EventDispatcher | ||
| */ | ||
| protected $eventDispatcher; | ||
|
|
||
|
|
||
| /** | ||
| * Constructor to inject the Symfony Filesystem service. | ||
| */ | ||
| public function __construct(FileSystemInterface $fileSystem, EventDispatcherInterface $event_dispatcher) | ||
| { | ||
| $this->fileSystem = $fileSystem; | ||
| $this->eventDispatcher = $event_dispatcher; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public static function create(ContainerInterface $container) | ||
| { | ||
| return new static( | ||
| $container->get('file_system'), | ||
| $container->get('event_dispatcher') | ||
| ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Renames a file while keeping the extension unchanged. | ||
| * | ||
| * Expects a URL parameter for the file ID (fid) and a JSON payload with: | ||
| * - new_filename: The new base file name (without extension). | ||
| * | ||
| * @param \Symfony\Component\HttpFoundation\Request $request | ||
| * The incoming request. | ||
| * @param Drupal\file\Entity\File $file | ||
| * The file ID from the URL. | ||
| * | ||
| * @return \Symfony\Component\HttpFoundation\JsonResponse | ||
| * The JSON response. | ||
| */ | ||
| public function main(Request $request, $file) | ||
| { | ||
| // Decode JSON payload. | ||
|
|
||
| $data = json_decode($request->getContent(), true); | ||
|
|
||
| // Validate that new_filename is provided. | ||
| if (empty($data['new_filename'])) { | ||
| return new JsonResponse(['error' => 'Missing new_filename parameter']); | ||
| } | ||
|
|
||
|
|
||
| if (!$file) { | ||
| return new JsonResponse(["Error 404" => 'No file found']); | ||
| } | ||
|
|
||
|
|
||
| $pathinfo = pathinfo($file->getFileUri()); | ||
| $filename_new = $data['new_filename']; | ||
| $msg = $this->validate($file, $filename_new); | ||
| if ($msg != 'OK') { | ||
| return new JsonResponse(['Error' => $msg]); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| if ($filename_new != $file->getFilename()) { | ||
| $filepath_new = $this->getRenamedFilePath($file, $filename_new); | ||
| rename($file->getFileUri(), $filepath_new); | ||
| // Update file entity. | ||
| $file->setFilename($filename_new); | ||
| $file->setFileUri($filepath_new); | ||
| $status = $file->save(); | ||
|
|
||
| return new JsonResponse(['message' => 'File renamed successfully', 'new_filename' => $filename_new]); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Check if the file name is valid | ||
| * | ||
| * @param Drupal\file\Entity\File $file | ||
| * File that needs to be renamed | ||
| * | ||
| * @param string $new_filename | ||
| * File Name of the new file | ||
| * | ||
| * @return string | ||
| * Error Message if not valid else OK | ||
| */ | ||
| public function validate($file, $new_filename) | ||
| { | ||
| $pathinfo = pathinfo($file->getFileUri()); | ||
| $source_file_uri = $file->getFileUri(); | ||
|
|
||
| if (!file_exists($source_file_uri)) { | ||
| // Show an error if no file on disc. | ||
| return 'No file exists in this'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be useful to have this error message say "No file with name XX exists in the XXX filesystem." |
||
| } | ||
|
|
||
|
|
||
| $new_basename = $new_filename; | ||
|
|
||
| if ($new_basename !== $file->getFilename()) { | ||
| // File renamed. | ||
| if ($new_basename !== basename($new_basename) | ||
| || strpos($new_basename, '\\') !== false | ||
| ) { | ||
| // If filename contains a slash or a backslash. | ||
| return 'Value must be a filename with no path information'; | ||
| } else { | ||
| // Dispatching a event to use default filename validation. | ||
| $event = new FileUploadSanitizeNameEvent($new_basename, $pathinfo['extension']); | ||
| $this->eventDispatcher->dispatch($event); | ||
|
|
||
| if ($event->isSecurityRename()) { | ||
| // If new filename contains forbidden characters. | ||
| return ('File name is invalid'); | ||
| } | ||
| } | ||
|
|
||
| $new_file_path = $this->getRenamedFilePath($file, $new_basename); | ||
| if (file_exists($new_file_path)) { | ||
| // File with given name already on disc. | ||
| return ('File with this name already exists in the directory.'); | ||
| } | ||
| } | ||
|
|
||
| return 'OK'; | ||
| } | ||
|
|
||
| /** | ||
| * Get Renamed File Path. | ||
| * | ||
| * @param Drupal\file\Entity\File $file | ||
| * File that needs to be renamed. | ||
| * | ||
| * @param string $new_filename | ||
| * File Name of the new file | ||
| * | ||
| * @return string | ||
| * File name after rename. | ||
| */ | ||
| protected function getRenamedFilePath($file, $new_filename) | ||
| { | ||
| $pathinfo = pathinfo($file->getFileUri()); | ||
| $old_filename = $pathinfo['filename'] . '.' . $pathinfo['extension']; | ||
| // Path after renaming. | ||
| return str_replace($old_filename, $new_filename, $file->getFileUri()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message should contain both the original and new filename. Also, can we add this entry to the Drupal logger?