Ability to rename a file before uploading it to the server

This commit is contained in:
yflory
2017-11-16 18:11:13 +01:00
parent 2813b7219e
commit 25d3d321da
4 changed files with 52 additions and 17 deletions

View File

@@ -105,6 +105,7 @@ define([
var ctx = c2.getContext('2d');
ctx.drawImage(canvas, D.x, D.y, D.w, D.h);
cb(void 0, c2.toDataURL());
};
@@ -124,19 +125,18 @@ define([
Thumb.fromVideoBlob = function (blob, cb) {
var url = URL.createObjectURL(blob);
var video = document.createElement("VIDEO");
video.src = url;
video.addEventListener('loadedmetadata', function() {
video.currentTime = Number(Math.floor(Math.min(video.duration/10, 5)));
video.addEventListener('loadeddata', function() {
var D = getResizedDimensions(video, 'video');
Thumb.fromCanvas(video, D, cb);
});
video.currentTime = Number(Math.floor(Math.min(video.duration/10, 5)));
});
video.addEventListener('error', function (e) {
console.error(e);
cb('ERROR');
});
video.src = url;
};
Thumb.fromPdfBlob = function (blob, cb) {
require.config({paths: {'pdfjs-dist': '/common/pdfjs'}});

View File

@@ -95,7 +95,7 @@ define([
var id = file.id;
var dropEvent = file.dropEvent;
delete file.dropEvent;
if (dropEvent.path) { file.path = dropEvent.path; }
if (dropEvent && dropEvent.path) { file.path = dropEvent.path; }
if (queue.inProgress) { return; }
queue.inProgress = true;
@@ -221,12 +221,41 @@ define([
queue.next();
};
var handleFile = File.handleFile = function (file, e, thumbnail) {
var showNamePrompt = true;
var promptName = function (file, cb) {
var extIdx = file.name.lastIndexOf('.');
var name = extIdx !== -1 ? file.name.slice(0,extIdx) : file.name;
var ext = extIdx !== -1 ? file.name.slice(extIdx) : "";
var msg = Messages._getKey('upload_rename', [
Util.fixHTML(file.name),
Util.fixHTML(ext)
]);
UI.prompt(msg, name, function (newName) {
if (newName === null) {
showNamePrompt = false;
return void cb (file.name);
}
if (!newName || !newName.trim()) { return void cb (file.name); }
var newExtIdx = newName.lastIndexOf('.');
var newExt = newExtIdx !== -1 ? newName.slice(newExtIdx) : "";
if (newExt !== ext) { newName += ext; }
cb(newName);
}, null, true);
};
var handleFileState = {
queue: [],
inProgress: false
};
var handleFile = File.handleFile = function (file, e) {
//if (handleFileState.inProgress) { return void handleFileState.queue.push(file); }
handleFileState.inProgress = true;
var thumb;
var file_arraybuffer;
var name = file.name;
var finish = function () {
var metadata = {
name: file.name,
name: name,
type: file.type,
};
if (thumb) { metadata.thumbnail = thumb; }
@@ -235,26 +264,26 @@ define([
metadata: metadata,
dropEvent: e
});
handleFileState.inProgress = false;
if (handleFileState.queue.length) { handleFile(handleFileState.queue.shift()); }
};
var getName = function () {
promptName(file, function (newName) {
name = newName;
finish();
});
};
blobToArrayBuffer(file, function (e, buffer) {
if (e) { console.error(e); }
file_arraybuffer = buffer;
if (thumbnail) { // there is already a thumbnail
return blobToArrayBuffer(thumbnail, function (e, buffer) {
if (e) { console.error(e); }
thumb = arrayBufferToString(buffer);
finish();
});
}
if (!Thumb.isSupportedType(file.type)) { return finish(); }
if (!Thumb.isSupportedType(file.type)) { return getName(); }
// make a resized thumbnail from the image..
Thumb.fromBlob(file, function (e, thumb64) {
if (e) { console.error(e); }
if (!thumb64) { return finish(); }
if (!thumb64) { return getName(); }
thumb = thumb64;
finish();
getName();
});
});
};