Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to select namespace for uploaded files. #4

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 55 additions & 21 deletions action/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class action_plugin_dropfiles_ajax extends DokuWiki_Action_Plugin
{

protected $NS = '';
#protected $NS = '';


/**
Expand Down Expand Up @@ -59,27 +59,59 @@ 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
*
* This version also provides a consistent error key, instead of only giving a localized error message
*/
protected function callMediaupload()
{
global $NS, $MSG, $INPUT;
global $MSG, $INPUT;

$id = '';
if ($_FILES['qqfile']['tmp_name']) {
$id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']);
} 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');
Expand All @@ -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 = '';
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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;
}

Expand All @@ -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());
Expand Down Expand Up @@ -217,4 +252,3 @@ function ($q) {
}
}

// vim:ts=4:sw=4:et:
2 changes: 1 addition & 1 deletion action/jsinfoconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
1 change: 1 addition & 0 deletions conf/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

$conf['insertFileLink'] = 0;
$conf['namespace'] = '@NS@';
2 changes: 1 addition & 1 deletion conf/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@


$meta['insertFileLink'] = array('onoff');

$meta['namespace'] = array('string');
2 changes: 1 addition & 1 deletion lang/de/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions lang/de/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';



Expand Down
2 changes: 1 addition & 1 deletion lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions lang/en/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';



Expand Down
15 changes: 10 additions & 5 deletions src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -222,13 +223,16 @@ jQuery(function () {
if (filesWithOtherErrors.length) {
filesWithOtherErrors.map(function (file) {
var $errorMessage = jQuery('<div class="error"></div>');
$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();
}

Expand Down Expand Up @@ -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('<a>').attr({
Expand Down