don't fail on duplicate documents, only upload new documents if possible

This commit is contained in:
j 2019-12-05 12:18:57 +00:00
parent 16b900c541
commit 05dcc997b6

View file

@ -5,6 +5,8 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
extensions = files.map(function(file) { extensions = files.map(function(file) {
return file.name.split('.').pop().toLowerCase() return file.name.split('.').pop().toLowerCase()
}), }),
existingFiles = [],
uploadFiles = [],
supportedExtensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'], supportedExtensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'],
@ -56,9 +58,9 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
height: 112, height: 112,
keys: {escape: 'close'}, keys: {escape: 'close'},
width: 288, width: 288,
title: files.length == 1 title: uploadFiles.length == 1
? Ox._('Upload Document') ? Ox._('Upload Document')
: Ox._('Upload {0} Documents', [files.length]) : Ox._('Upload {0} Documents', [uploadFiles.length])
}) })
.bindEvent({ .bindEvent({
open: function() { open: function() {
@ -73,12 +75,17 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
Ox._('Supported file types are GIF, JPG, PNG and PDF.') Ox._('Supported file types are GIF, JPG, PNG and PDF.')
); );
} else { } else {
var valid = true; var oshashes = [];
Ox.parallelForEach(files, function(file, index, array, callback) { Ox.parallelForEach(files, function(file, index, array, callback) {
var extension = file.name.split('.').pop().toLowerCase(), var extension = file.name.split('.').pop().toLowerCase(),
filename = file.name.split('.').slice(0, -1).join('.') + '.' filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension); + (extension == 'jpeg' ? 'jpg' : extension);
valid && Ox.oshash(file, function(oshash) { Ox.oshash(file, function(oshash) {
// skip duplicate files
if (Ox.contains(oshashes, oshash)) {
callback();
} else {
oshashes.push(oshash)
pandora.api.findDocuments({ pandora.api.findDocuments({
keys: ['id', 'user', 'title', 'extension'], keys: ['id', 'user', 'title', 'extension'],
query: { query: {
@ -95,7 +102,25 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
if (result.data.items.length) { if (result.data.items.length) {
var id = result.data.items[0].title + '.' var id = result.data.items[0].title + '.'
+ result.data.items[0].extension; + result.data.items[0].extension;
valid && errorDialog( existingFiles.push({
id: id,
filename: filename
})
} else {
uploadFiles.push(file)
}
callback();
})
}
});
} ,function() {
if (uploadFiles.length) {
$uploadDialog.open();
} else if (existingFiles.length) {
var filename = existingFiles[0].filename
var id = existingFiles[0].id
errorDialog(
filename == id filename == id
? Ox._( ? Ox._(
'The file "{0}" already exists.', 'The file "{0}" already exists.',
@ -106,13 +131,7 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
[filename, id] [filename, id]
) )
).open(); ).open();
valid = false;
} }
callback();
})
});
} ,function() {
valid && $uploadDialog.open();
}); });
return {open: Ox.noop}; return {open: Ox.noop};
} }
@ -138,7 +157,7 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
function uploadFile(part) { function uploadFile(part) {
var data = { var data = {
}, },
file = files[part], file = uploadFiles[part],
extension = file.name.split('.').pop().toLowerCase(), extension = file.name.split('.').pop().toLowerCase(),
filename = file.name.split('.').slice(0, -1).join('.') + '.' filename = file.name.split('.').slice(0, -1).join('.') + '.'
+ (extension == 'jpeg' ? 'jpg' : extension); + (extension == 'jpeg' ? 'jpg' : extension);
@ -158,7 +177,7 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
if (data.progress == 1) { if (data.progress == 1) {
part++; part++;
ids.push(data.response.id); ids.push(data.response.id);
if (part == files.length) { if (part == uploadFiles.length) {
$progress.options({progress: data.progress}); $progress.options({progress: data.progress});
callback && callback({ids: ids}); callback && callback({ids: ids});
$uploadDialog.close(); $uploadDialog.close();
@ -174,7 +193,7 @@ pandora.ui.uploadDocumentDialog = function(options, callback) {
}, },
progress: function(data) { progress: function(data) {
var progress = data.progress || 0; var progress = data.progress || 0;
progress = part / files.length + 1 / files.length * progress; progress = part / uploadFiles.length + 1 / uploadFiles.length * progress;
$progress.options({progress: progress}); $progress.options({progress: progress});
} }
}); });