phpoctobercms

Octobercms Rename file after upload in media.file.upload event


Currently the media library allows you to upload files that have spaces in them. I want to prevent this for a number of reasons. My current plan is to rename the file after it's been uploaded inside the file.media.upload event.

I get the $originalPath through, and an instance of UploadedFile($newPath)

I can get the original file name by using..

$newPath->getClientOriginalName();

and then sanitize that.

[Update]

To rename the file I used the MediaLibrary class' moveFile() method. See updated code below

Event::listen('media.file.upload', function($widget, $originalPath, $newPath) {
    if (strpos($originalPath, ' ') !== false) {
        $sanitizedFilePath = str_replace(' ', '-', $originalPath);
        MediaLibrary::instance()->moveFile($originalPath, $sanitizedFilePath);
    }
});

Event::listen('media.file.rename', function($widget, $originalPath, $newPath) {
    if (strpos($newPath, ' ') !== false) {
        $sanitizedFilePath = str_replace(' ', '-', $newPath);
        MediaLibrary::instance()->moveFile($newPath, $sanitizedFilePath);
    }
});

Event::listen('media.folder.create', function($widget, $newFolderPath) {
    if (strpos($newFolderPath, ' ') !== false) {
        $sanitizedFilePath = str_replace(' ', '-', $newFolderPath);
        MediaLibrary::instance()->moveFolder($newFolderPath, $sanitizedFilePath);
    }
});

Event::listen('media.folder.rename', function($widget, $originalPath, $newPath) {
    if (strpos($newPath, ' ') !== false) {
        $sanitizedFilePath = str_replace(' ', '-', $newPath);
        MediaLibrary::instance()->moveFolder($newPath, $sanitizedFilePath);
    }
});

The problem I face now is that images uploaded through the richeditor don't return the renamed file, it's still trying to show the file with it's original / uploaded file name.

A temporary solution I've used at the minute is to hide the upload option and disabled pasting images into the editor by extending the Froala options, this makes it so that you're forced into choosing an image through the media library.


Solution

  • Yes we can do it we just need to rename uploaded file on the disk for this we can use MediaLibrary

    you used wrong event syntax for upload

    Real one is : Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) { }); you used syntax for media.file.rename they all are same but variable passed to them are different

    You can use this code this can rename file name once its uploaded to media

    <?php
    use System\Classes\MediaLibrary;
    use Event;
    
    // ...
    
    // we listedn for file upload event
    Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
    
        // we get file's original name with spaces may be
        $originalName = $uploadedFile->getClientOriginalName();
    
        // sanitize it and wait for when we can use it
        $sanitizedFileName = str_replace(' ', '-', $originalName);
    
        // now we need only path where we need to move file 
        // break path in to chunks   
        $filePathChunks = explode(DIRECTORY_SEPARATOR, $filePath);
    
        // this simple logic will remove real file name from last 
        // and add our brand new  $sanitizedFileName yahh ! wait is over
        $filePathChunks[ (count($filePathChunks) - 1) ] = $sanitizedFileName;
    
        // again join the path
        $newPath = implode(DIRECTORY_SEPARATOR, $filePathChunks);
    
    
        $isRename = true;
        // and use MediaLibrary function to move file
        MediaLibrary::instance()->moveFile($filePath, $newPath, $isRename);
    
       // move file it means rename file in linux/unix term so do not get confuse here.
    });
    

    this code will get job done for you :)

    if any doubt please comment.