Fix attachment handling during translation
Synchronously determine whether attachment saving should be attempted and only start attachment progress if it can
This commit is contained in:
parent
d19d90a33c
commit
8c2c097c6b
1 changed files with 41 additions and 28 deletions
|
@ -93,8 +93,10 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
}
|
}
|
||||||
// Handle standalone attachments differently
|
// Handle standalone attachments differently
|
||||||
else if (type == "attachment") {
|
else if (type == "attachment") {
|
||||||
standaloneAttachments.push(items[iitem]);
|
if (this._canSaveAttachment(item)) {
|
||||||
attachmentCallback(items[iitem], 0);
|
standaloneAttachments.push(item);
|
||||||
|
attachmentCallback(item, 0);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
newItem = new Zotero.Item(type);
|
newItem = new Zotero.Item(type);
|
||||||
|
@ -135,8 +137,10 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
// handle attachments
|
// handle attachments
|
||||||
if (specialFields.attachments) {
|
if (specialFields.attachments) {
|
||||||
for (let attachment of specialFields.attachments) {
|
for (let attachment of specialFields.attachments) {
|
||||||
|
if (this._canSaveAttachment(attachment)) {
|
||||||
|
attachmentCallback(attachment, 0);
|
||||||
|
}
|
||||||
childAttachments.push([attachment, myID]);
|
childAttachments.push([attachment, myID]);
|
||||||
attachmentCallback(attachment, 0);
|
|
||||||
}
|
}
|
||||||
// Restore the attachments field, since we use it later in
|
// Restore the attachments field, since we use it later in
|
||||||
// translation
|
// translation
|
||||||
|
@ -157,15 +161,15 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
let newItem = yield this._saveAttachment(item, null, attachmentCallback);
|
let newItem = yield this._saveAttachment(item, null, attachmentCallback);
|
||||||
if (newItem) newItems.push(newItem);
|
if (newItem) newItems.push(newItem);
|
||||||
}
|
}
|
||||||
// Save attachments afterwards, since we want to signal completion as soon as the main items
|
// Save child attachments afterwards, since we want to signal completion as soon as the main
|
||||||
// are saved
|
// items are saved
|
||||||
var promise = Zotero.Promise.delay(1);
|
var promise = Zotero.Promise.delay(1);
|
||||||
for (let a of childAttachments) {
|
for (let a of childAttachments) {
|
||||||
// Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=449811 (fixed in Fx51?)
|
// Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=449811 (fixed in Fx51?)
|
||||||
let [item, parentItemID] = a;
|
let [item, parentItemID] = a;
|
||||||
promise = promise.then(() => this._saveAttachment(item, parentItemID, attachmentCallback));
|
promise = promise.then(() => this._saveAttachment(item, parentItemID, attachmentCallback));
|
||||||
}
|
}
|
||||||
|
|
||||||
return newItems;
|
return newItems;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
@ -235,6 +239,37 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
return item;
|
return item;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
_canSaveAttachment: function (attachment) {
|
||||||
|
if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_DOWNLOAD) {
|
||||||
|
if (!attachment.url && !attachment.document) {
|
||||||
|
Zotero.debug("Translate: Not adding attachment: no URL specified");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (attachment.snapshot !== false) {
|
||||||
|
if (attachment.document || Zotero.MIME.isWebPageType(attachment.mimeType)) {
|
||||||
|
if (!Zotero.Prefs.get("automaticSnapshots")) {
|
||||||
|
Zotero.debug("Translate: Not adding attachment: automatic snapshots are disabled");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!Zotero.Prefs.get("downloadAssociatedFiles")) {
|
||||||
|
Zotero.debug("Translate: Not adding attachment: automatic file attachments are disabled");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (this.attachmentMode == Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE');
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves a translator attachment to the database
|
* Saves a translator attachment to the database
|
||||||
*
|
*
|
||||||
|
@ -258,8 +293,6 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
newAttachment = yield this._saveAttachmentFile.apply(this, arguments);
|
newAttachment = yield this._saveAttachmentFile.apply(this, arguments);
|
||||||
} else {
|
} else {
|
||||||
Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE');
|
Zotero.debug('Translate: Ignoring attachment due to ATTACHMENT_MODE_IGNORE');
|
||||||
attachmentCallback(attachment, false);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!newAttachment) return false; // attachmentCallback should not have been called in this case
|
if (!newAttachment) return false; // attachmentCallback should not have been called in this case
|
||||||
|
@ -503,26 +536,6 @@ Zotero.Translate.ItemSaver.prototype = {
|
||||||
_saveAttachmentDownload: Zotero.Promise.coroutine(function* (attachment, parentItemID, attachmentCallback) {
|
_saveAttachmentDownload: Zotero.Promise.coroutine(function* (attachment, parentItemID, attachmentCallback) {
|
||||||
Zotero.debug("Translate: Adding attachment", 4);
|
Zotero.debug("Translate: Adding attachment", 4);
|
||||||
|
|
||||||
if(!attachment.url && !attachment.document) {
|
|
||||||
Zotero.debug("Translate: Not adding attachment: no URL specified");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine whether to save an attachment
|
|
||||||
if(attachment.snapshot !== false) {
|
|
||||||
if(attachment.document || Zotero.MIME.isWebPageType(attachment.mimeType)) {
|
|
||||||
if(!Zotero.Prefs.get("automaticSnapshots")) {
|
|
||||||
Zotero.debug("Translate: Not adding attachment: automatic snapshots are disabled");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(!Zotero.Prefs.get("downloadAssociatedFiles")) {
|
|
||||||
Zotero.debug("Translate: Not adding attachment: automatic file attachments are disabled");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let doc = undefined;
|
let doc = undefined;
|
||||||
if(attachment.document) {
|
if(attachment.document) {
|
||||||
doc = new XPCNativeWrapper(Zotero.Translate.DOMWrapper.unwrap(attachment.document));
|
doc = new XPCNativeWrapper(Zotero.Translate.DOMWrapper.unwrap(attachment.document));
|
||||||
|
|
Loading…
Add table
Reference in a new issue