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