diff --git a/action/ajax.php b/action/ajax.php index 7e15fd1..99883a3 100644 --- a/action/ajax.php +++ b/action/ajax.php @@ -14,7 +14,7 @@ class action_plugin_dropfiles_ajax extends DokuWiki_Action_Plugin { - protected $NS = ''; + #protected $NS = ''; /** @@ -59,6 +59,39 @@ public function handleAjaxCallUnknown(Doku_Event $event, $param) } } + + /** + * renames files according to configuration seeting + */ + protected function renameFile($id) + { + global $INPUT; + $NS = $INPUT->str('ns'); + $namespace = $this->getConf('namespace'); + $basename = pathinfo(noNS($id), PATHINFO_BASENAME); + $namespace = str_replace( + array( + '@NS@', + '@ID@', + '@USER@', + '@PAGE@', + ), + array( + $NS, + $INPUT->post->str('id'), + $_SERVER['REMOTE_USER'], + noNS($INPUT->post->str('id')), + ), + $namespace + ); + $namespace = strftime($namespace); + if (substr($namespace, -1) != ':') { + $namespace .= ':'; + } + return $namespace.$basename; +; + } + /** * this is an adjusted version of @see ajax_mediaupload * @@ -66,7 +99,7 @@ public function handleAjaxCallUnknown(Doku_Event $event, $param) */ protected function callMediaupload() { - global $NS, $MSG, $INPUT; + global $MSG, $INPUT; $id = ''; if ($_FILES['qqfile']['tmp_name']) { @@ -74,12 +107,11 @@ protected function callMediaupload() } elseif ($INPUT->get->has('qqfile')) { $id = $INPUT->get->str('qqfile'); } - - $id = cleanID($id); - - $NS = $INPUT->str('ns'); - $ns = $NS . ':' . getNS($id); - + + $filename = $this->renameFile($id); + $ns = getNS($filename); + $id = cleanID($filename); + $AUTH = auth_quickaclcheck("$ns:*"); if ($AUTH >= AUTH_UPLOAD) { io_createNamespace("$ns:xxx", 'media'); @@ -91,18 +123,18 @@ protected function callMediaupload() $res = false; if ($_FILES['qqfile']['tmp_name']) { - $res = media_upload($NS, $AUTH, $_FILES['qqfile']); + $res = media_upload($ns, $AUTH, $_FILES['qqfile']); } if ($INPUT->get->has('qqfile')) { - $res = media_upload_xhr($NS, $AUTH); + $res = media_upload_xhr($ns, $AUTH); } if ($res) { $result = array( 'success' => true, - 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'), - 'id' => $NS . ':' . $id, - 'ns' => $NS, + 'link' => media_managerURL(array('ns' => $ns, 'image' => $id), '&'), + 'id' => $id, + 'ns' => $ns, ); } else { $error = ''; @@ -114,7 +146,8 @@ protected function callMediaupload() $result = array( 'error' => $error, 'errorType' => $this->determineErrorCause($id, $ns), - 'ns' => $NS, + 'fileNameRenamed' => $id, + 'ns' => $ns, ); } header('Content-Type: application/json'); @@ -127,7 +160,7 @@ protected function callMediaupload() */ protected function checkFiles() { global $INPUT; - $this->NS = $INPUT->str('ns'); + #$this->NS = $INPUT->str('ns'); // loop over files $filelist = $INPUT->post->arr('filenames'); @@ -144,10 +177,12 @@ protected function checkFiles() { * @return array */ protected function checkFileCallback($carry, $filename){ - $id = cleanID($filename); - $ns = $this->NS . ':' . getNS($id); + $renamedFileName = $this->renameFile($filename); + + $id = cleanID($renamedFileName); + $ns = getNS($renamedFileName); $error = $this->determineErrorCause($id, $ns, false); - $carry[$filename] = $error; + $carry[$filename] = ['error' => $error, 'renamedFileName' => $renamedFileName]; return $carry; } @@ -174,8 +209,8 @@ protected function determineErrorCause($id, $ns, $contentAvailable = true) if ($AUTH < AUTH_UPLOAD) { return 'missing permissions'; } - $fullID = $ns . ':' . $id; - $fn = mediaFN($fullID); + #$fullID = $ns . ':' . $id; + $fn = mediaFN($id); // get filetype regexp $types = array_keys(getMimeTypes()); @@ -217,4 +252,3 @@ function ($q) { } } -// vim:ts=4:sw=4:et: diff --git a/action/jsinfoconfig.php b/action/jsinfoconfig.php index 3bf3c8a..dece47d 100644 --- a/action/jsinfoconfig.php +++ b/action/jsinfoconfig.php @@ -24,7 +24,7 @@ class action_plugin_dropfiles_jsinfoconfig extends DokuWiki_Action_Plugin */ public function register(Doku_Event_Handler $controller) { - $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'addDataToJSINFO'); + $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addDataToJSINFO'); } /** diff --git a/conf/default.php b/conf/default.php index df46ccc..d5db2f0 100644 --- a/conf/default.php +++ b/conf/default.php @@ -6,3 +6,4 @@ */ $conf['insertFileLink'] = 0; +$conf['namespace'] = '@NS@'; diff --git a/conf/metadata.php b/conf/metadata.php index 2c5a87a..99c0c5e 100644 --- a/conf/metadata.php +++ b/conf/metadata.php @@ -7,4 +7,4 @@ $meta['insertFileLink'] = array('onoff'); - +$meta['namespace'] = array('string'); diff --git a/lang/de/lang.php b/lang/de/lang.php index 01e1d4c..ecdc955 100644 --- a/lang/de/lang.php +++ b/lang/de/lang.php @@ -14,7 +14,7 @@ $lang['js']['overwriteAll'] = 'Alle überschreiben'; $lang['js']['rename'] = 'Umbenennen'; $lang['js']['cancel'] = 'Abbrechen'; -$lang['js']['popup:fileExists'] = 'Die Datei %s existiert bereits.'; +$lang['js']['popup:fileExists'] = 'Die Datei %s1 (hochgeladen als %s2) existiert bereits.'; $lang['js']['popup:originalName'] = '(Urspünglicher Dateiname: %s)'; $lang['js']['title:fileUpload'] = 'Datei upload'; $lang['js']['title:fileExistsError'] = 'Fehler: Datei existiert bereits'; diff --git a/lang/de/settings.php b/lang/de/settings.php index 5b732fa..e5285d2 100644 --- a/lang/de/settings.php +++ b/lang/de/settings.php @@ -7,6 +7,7 @@ // keys need to match the config setting name $lang['insertFileLink'] = 'Link zu erfolgreich hochgeladenen Dateien an Cursor-Position in Seite einfügen'; + $lang['namespace'] = 'Namespace für die hochladenen Dateien. Die Platzhalter @USER@, @NS@, @ID@, @PAGE@ and strftime Platzhalter können verwendet werden.'; diff --git a/lang/en/lang.php b/lang/en/lang.php index 4609093..15fc0d7 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -14,7 +14,7 @@ $lang['js']['overwriteAll'] = 'Overwrite all'; $lang['js']['rename'] = 'Rename'; $lang['js']['cancel'] = 'Cancel'; -$lang['js']['popup:fileExists'] = 'The file %s already exists.'; +$lang['js']['popup:fileExists'] = 'The file %s1 (uploaded as %s2) already exists.'; $lang['js']['popup:originalName'] = '(Original filename: %s)'; $lang['js']['title:fileUpload'] = 'File upload'; $lang['js']['title:fileExistsError'] = 'Error: File exists'; diff --git a/lang/en/settings.php b/lang/en/settings.php index ed23cd6..3744d3d 100644 --- a/lang/en/settings.php +++ b/lang/en/settings.php @@ -7,6 +7,7 @@ // keys need to match the config setting name $lang['insertFileLink'] = 'Insert link to successfully uploaded files at cursor position in page'; + $lang['namespace'] = 'Namespace to upload to. You can use the placeholders @USER@, @NS@, @ID@, @PAGE@ and strftime placeholders.'; diff --git a/src/upload.js b/src/upload.js index db2ad17..142ba48 100644 --- a/src/upload.js +++ b/src/upload.js @@ -104,7 +104,8 @@ jQuery(function () { */ function showErrorDialog() { var fileName = filesThatExist[0].newFileName || filesThatExist[0].name; - var text = window.LANG.plugins.dropfiles['popup:fileExists'].replace('%s', fileName); + var renamedFileName = filesThatExist[0].renamedFileName; + var text = window.LANG.plugins.dropfiles['popup:fileExists'].replace('%s2', renamedFileName).replace('%s1', fileName); if (fileName !== filesThatExist[0].name) { text += ' ' + window.LANG.plugins.dropfiles['popup:originalName'].replace('%s', filesThatExist[0].name); } @@ -205,13 +206,13 @@ jQuery(function () { }).done(function handleCheckFilesResult(json) { var data = JSON.parse(json); var filesWithoutErrors = filelist.filter(function (file) { - return data[file.name] === ''; + return data[file.name].error === ''; }); filesThatExist = filelist.filter(function (file) { - return data[file.name] === 'file exists'; + return data[file.name].error === 'file exists'; }); var filesWithOtherErrors = filelist.filter(function (file) { - return data[file.name] && data[file.name] !== 'file exists'; + return data[file.name].error && data[file.name].error !== 'file exists'; }); // show errors / pending files @@ -222,13 +223,16 @@ jQuery(function () { if (filesWithOtherErrors.length) { filesWithOtherErrors.map(function (file) { var $errorMessage = jQuery('
'); - $errorMessage.text(file.name + ': ' + data[file.name]); + $errorMessage.text(file.name + ' (uploaded as ' + data[file.name].renamedFileName + ')' + ': ' + data[file.name].error); jQuery(elementOntoWhichItWasDropped).before($errorMessage); }); } // upload valid files if (filesThatExist.length) { + filesThatExist.map(function (file) { + file.renamedFileName = data[file.name].renamedFileName; + }); showErrorDialog(); } @@ -326,6 +330,7 @@ jQuery(function () { jQuery.ajax(DW_AJAX_URL, settings) .done( function (data) { + file.renamedFileName = data.fileNameRenamed; if (data.success) { $progressBar.find('.ui-progressbar-value').css('background-color', 'green'); $statusbar.find('.filename').wrap(jQuery('').attr({