Skip to content

Media creation within folder with a space in its name causes unexpected errors

Description

While developing the demodata API I encountered an unexpected error.
What i did is to create the QUIQQER media area by reading a physical folder and creating the respective media area entries.

When i try to create a file entry within a folder,that contains a space in its name, i receive an unexpected exception.

The root cause of this error is the parent folder creation.
While creating the parent folder, it gets renamed to remove the spaces in its name:
Example directory becomes example_directory.

Unfortunately this renaming process removes the trailing slash in the file attribute.
This attribute gets used to check if a file needs renaming. As the slash is missing now, this check results in a false positive and tries to rename the file.
WHile renaming the file an exception gets thrown, because the file exists already.

Reproduction

I encountered the problem while working with the media API and not in the admin area.
I am not sure if you can reproduce the problem within the admin area.

  • Create a media directory with a space in its name
  • Create a media entry within that folder
$MediaBaseFolder = $Project->getMedia()->get(1)
$SubFolder = $MediaBaseFolder->createFolder("Folder with spaces");

$UploadedFile    = $SubFolder->uploadFile($fullPath, Folder::FILE_OVERWRITE_TRUE);
if (isset($this->mediaSection[$pathInSourceDir])) {
   $UploadedFile->setAttributes([
         'name'     => $this->mediaSection[$pathInSourceDir]['name'],
         'title'    => $this->mediaSection[$pathInSourceDir]['title'],
         'alt'      => $this->mediaSection[$pathInSourceDir]['alt'],
         'priority' => $this->mediaSection[$pathInSourceDir]['priority'],
         'short'    => $this->mediaSection[$pathInSourceDir]['short'],
     ]);
}
$UploadedFile->save();
$UploadedFile->activate();

Resolution

Make sure the file attribute has a trailing slash, when renaming a folder

diff --git a/lib/QUI/Projects/Media/Folder.php b/lib/QUI/Projects/Media/Folder.php
index f6b38f2a..fbbd56a4 100644
--- a/lib/QUI/Projects/Media/Folder.php
+++ b/lib/QUI/Projects/Media/Folder.php
@@ -321,7 +321,7 @@ class Folder extends Item implements QUI\Interfaces\Projects\Media\File
         $this->deleteCache();
 
         $this->setAttribute('name', $newname);
-        $this->setAttribute('file', $new_path);
+        $this->setAttribute('file', $new_path.'/');
 
         QUI::getEvents()->fireEvent('mediaRename', [$this]);
     }

Merge Request

Please refer to the merge request !13 (merged)

von Florian Bogner bearbeitet