Octobercms 在 media.file.upload 事件中上传后重命名文件

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

目前,媒体库允许您上传包含空格的文件。 出于多种原因,我想阻止这种情况。 我目前的计划是在 file.media.upload 事件中上传文件后重命名该文件。

我得到了 $originalPath 和一个 UploadedFile($newPath) 的实例

我可以使用..获取原始文件名

$newPath->getClientOriginalName();

然后对其进行清理。

[更新]

为了重命名文件,我使用了 MediaLibrary class' moveFile() 方法。请参阅下面的更新代码

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);
    }
});

我现在面临的问题是通过 richeditor 上传的图像没有 return 重命名的文件,它仍在尝试显示带有原始/上传文件名的文件。

我目前使用的一个临时解决方案是通过扩展 Froala 选项隐藏上传选项并禁止将图像粘贴到编辑器中,这使得您不得不通过媒体选择图像图书馆。

是的,我们可以做到,我们只需要重命名磁盘上上传的文件,我们就可以使用 MediaLibrary

您为 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

上传到媒体后,您可以使用此代码 rename 文件名

<?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.
});

此代码将为您完成工作:)

如有疑问请评论。