artgris / FileManagerBundle

FileManager is a simple Multilingual File Manager Bundle for Symfony
MIT License
171 stars 89 forks source link

Problem with File Path Format in FileManagerBundle when Integrating with fos_ckeditor in easyadmin 3 #147

Open ethsam opened 2 weeks ago

ethsam commented 2 weeks ago

Issue Description

I am encountering an issue with FileManagerBundle where the file path returned after uploading an image includes unnecessary query parameters and formatting that complicates its use within CKEditor. The path returned is as follows:

/manager/file/mypicture.jpeg?conf=images&module=ckeditor&CKEditor=Item_content&CKEditorFuncNum=3&langCode=fr

Steps to Reproduce

  1. Go to the CKEditor instance integrated with FileManager.
  2. Upload an image using FileManager.
  3. Select the image to insert it into CKEditor.

Expected Behavior

The expected behavior is that the file path should be clean and usable directly within CKEditor, ideally formatted as:

/uploads/images/mypicture.jpeg

Actual Behavior

The actual file path returned includes query parameters and URL encoding that are not necessary for the use within CKEditor, making it less clean and potentially problematic for routing:

/manager/file/mypicture.jpeg?conf=images&module=ckeditor&CKEditor=Item_content&CKEditorFuncNum=3&langCode=fr

Suggested Fix or Enhancement

A potential fix would be to adjust the FileManagerBundle's integration with CKEditor to strip unnecessary URL parameters and possibly provide a cleaner, more direct path format. This might involve configuring how FileManager handles file path returns or modifying how CKEditor receives and processes these paths.

Additional Information

This issue impacts usability and could affect other integrations where clean paths are necessary. A solution would help ensure FileManagerBundle works seamlessly with CKEditor and other similar applications.

Thank you for looking into this matter. I am looking forward to your suggestions or fixes.

ethsam commented 2 weeks ago

Hi,

I wanted to provide an update regarding the issue with the file path format when integrating FileManagerBundle with fos_ckeditor in EasyAdmin 3. I encountered a problem where the file path returned after uploading an image included unnecessary query parameters and was not formatted as needed for clean usage within CKEditor and Easyadmin.

Solution Implemented: To resolve this, I forked the FileManagerBundle and made modifications to handle the file path more appropriately. Specifically, I added a function to FileTypeService.php that strips out the unwanted /public prefix from the file paths. This function, extractUploadsPath, looks for the /public/uploads part in the file path and removes the /public part to conform to the expected format.

And I've added and replaced a line in the function preview()

            $filePathReal = $this->extractUploadsPath($file->getPathname());
            // return $this->fileIcon($filePath, $extension, $size, true, $fileManager->getConfigurationParameter('twig_extension'), $fileManager->getConfigurationParameter('cachebreaker'));
            return $this->fileIcon($filePathReal, $extension, $size, true, $fileManager->getConfigurationParameter('twig_extension'), $fileManager->getConfigurationParameter('cachebreaker'));
function extractUploadsPath($fullPath) {
    $partToFind = '/public/uploads';
    $result = strstr($fullPath, $partToFind);

    if ($result !== false) {
        return str_replace('/public', '', $result);
    }

    return '';
}

Due to time constraints and the immediate needs of our ongoing project, I opted to implement a direct modification rather than exploring a more comprehensive solution that might involve deeper changes to the FileManagerBundle's core functionalities or its integration strategy with CKEditor.

This solution allows us to move forward with our project deadlines while still maintaining a clean and functional integration with CKEditor. It would be great to explore a more generic solution that could be incorporated into the FileManagerBundle in the future to avoid such issues.

I hope that the modifications I've implemented might provide a useful reference for addressing this file path issue in future versions of the FileManagerBundle. It would be beneficial for the community if a more generic solution could be integrated into the bundle to prevent similar issues when using FileManager with CKEditor or other editors.

Incorporating a way to configure the path formatting directly through the FileManagerBundle's settings could offer a more flexible and robust integration, making it easier for users to adapt the bundle to their specific needs without requiring custom forks or modifications.

Thank you again for considering this feedback. I am looking forward to seeing how this can be evolved to enhance the FileManagerBundle for all users.