revolution
revolution copied to clipboard
[3.x] File extension drops in media manager when friendly url pattern restricts dot
Bug report
Summary
I want to restrict dot in resource aliases, but related setting affects uploaded files.
Step to reproduce
On fresh modx installed, add . to friendly_alias_restrict_chars_pattern system setting default value (for exclude dot in resource aliases in my case)
Setting value become to /[\0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\\.]/
Then try to upload file with name te st.txt via media manager.
Observed behavior
After upload, you get te-sttxt (without dot before .txt extension)
If i disable upload_translit setiing, file name become to te st.txt → unsafe, non-friendly-url compatible an poor looking. May contain special chars and etc.
Expected behavior
Dots in file extensions (and other dots may be via special pattern) preserved when upload or remane file via media manager.
It will be great to set up RegExp pattenrs for uploaded file names and for fliendly URLs separately!
Environment
MODX 3.0.4. upload_translit setiing activated (by default) and friendly_alias_restrict_chars = pattern (by default)
+1
+1
In other systems, I remove the file extension, sanitize the file name, and then append an extension based on the file's Mime type. I do this because the extension isn't always right; sometimes for innocent reasons (a PNG with a .jpg extension) and other times to circumvent restrictions.
Either a separate system setting for each regex or removing the file extension/appending a new file extension after sanitization would avoid this problem.
As temporary solutin, i use this plugin for fix the problem
/*
@author Dima Kasatkin <[email protected]>
inpired by filetranslit plugin for MODX2 by Anton Andersen
Compatible with MODX3
*/
$restrict_pattern_for_files = '/[\0\x0B\t\n\r\f\a&=+%#<>"~`@\?\[\]\{\}\|\^\'\\\\]/';
switch ($modx->event->name) {
case 'OnFileManagerUpload':
foreach ($files as &$file) {
if ($file['error'] == 0) {
$newName = $modx->call(modResource::class, 'filterPathSegment', array(&$modx, $file['name'], array('friendly_alias_restrict_chars_pattern' => $restrict_pattern_for_files)));
//file rename logic
if ($file['name'] !== $newName) {
$arDirFiles = $source->getObjectsInContainer($directory);
foreach ($arDirFiles as &$dirFile) {
if ($dirFile['name'] === $newName) {
//delete file if there is one with new name
$source->removeObject($directory . $newName);
}
}
//transliterate uploaded file
$result = $source->renameObject($directory . $file['name'], $newName);
}
}
}
break;
case 'OnFileManagerFileRename':
$bases = $source->getBases($path);
$filepath = str_replace($bases['pathAbsolute'], '', $path);
$oldname = pathinfo($filepath,PATHINFO_BASENAME);
$newname = $modx->call(modResource::class, 'filterPathSegment', array(&$modx, $oldname, array('friendly_alias_restrict_chars_pattern' => $restrict_pattern_for_files)));
if ($newname !== $oldname) {
$source->renameObject($filepath, $newname);
}
break;
}
And it works ok for MODX3.
Guide: Create plugin, paste code, activate for OnFileManagerUpload and OnFileManagerFileRename MODX system events