Remove tmp-* directories when attachment download fails. Closes #1128

This commit is contained in:
Adomas Venčkauskas 2017-01-06 17:06:21 +02:00 committed by Dan Stillman
parent 43dad62150
commit 74d358bd19

View File

@ -326,54 +326,52 @@ Zotero.Attachments = new function(){
yield deferred.promise; yield deferred.promise;
let sample = yield Zotero.File.getSample(tmpFile); let sample = yield Zotero.File.getSample(tmpFile);
if (contentType == 'application/pdf' && try {
if (contentType == 'application/pdf' &&
Zotero.MIME.sniffForMIMEType(sample) != 'application/pdf') { Zotero.MIME.sniffForMIMEType(sample) != 'application/pdf') {
let errString = "Downloaded PDF did not have MIME type " let errString = "Downloaded PDF did not have MIME type "
+ "'application/pdf' in Attachments.importFromURL()"; + "'application/pdf' in Attachments.importFromURL()";
Zotero.debug(errString, 2); Zotero.debug(errString, 2);
Zotero.debug(sample, 3); Zotero.debug(sample, 3);
throw(new Error(errString)); throw(new Error(errString));
}
// Create DB item
var attachmentItem;
var destDir;
yield Zotero.DB.executeTransaction(function* () {
// Create a new attachment
attachmentItem = new Zotero.Item('attachment');
if (libraryID) {
attachmentItem.libraryID = libraryID;
} }
else if (parentItemID) {
let {libraryID: parentLibraryID, key: parentKey} = // Create DB item
Zotero.Items.getLibraryAndKeyFromID(parentItemID); var attachmentItem;
attachmentItem.libraryID = parentLibraryID; var destDir;
} yield Zotero.DB.executeTransaction(function*() {
attachmentItem.setField('title', title ? title : fileName); // Create a new attachment
attachmentItem.setField('url', url); attachmentItem = new Zotero.Item('attachment');
attachmentItem.setField('accessDate', "CURRENT_TIMESTAMP"); if (libraryID) {
attachmentItem.parentID = parentItemID; attachmentItem.libraryID = libraryID;
attachmentItem.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_URL; }
attachmentItem.attachmentContentType = contentType; else if (parentItemID) {
if (collections) { let {libraryID: parentLibraryID, key: parentKey} =
attachmentItem.setCollections(collections); Zotero.Items.getLibraryAndKeyFromID(parentItemID);
} attachmentItem.libraryID = parentLibraryID;
var itemID = yield attachmentItem.save(saveOptions); }
attachmentItem.setField('title', title ? title : fileName);
// Create a new folder for this item in the storage directory attachmentItem.setField('url', url);
destDir = this.getStorageDirectory(attachmentItem); attachmentItem.setField('accessDate', "CURRENT_TIMESTAMP");
yield OS.File.move(tmpDir.path, destDir.path); attachmentItem.parentID = parentItemID;
var destFile = destDir.clone(); attachmentItem.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_URL;
destFile.append(fileName); attachmentItem.attachmentContentType = contentType;
if (collections) {
// Refetch item to update path attachmentItem.setCollections(collections);
attachmentItem.attachmentPath = destFile.path; }
yield attachmentItem.save(saveOptions); var itemID = yield attachmentItem.save(saveOptions);
}.bind(this))
.catch(function (e) { // Create a new folder for this item in the storage directory
Zotero.debug(e, 1); destDir = this.getStorageDirectory(attachmentItem);
yield OS.File.move(tmpDir.path, destDir.path);
// Clean up var destFile = destDir.clone();
destFile.append(fileName);
// Refetch item to update path
attachmentItem.attachmentPath = destFile.path;
yield attachmentItem.save(saveOptions);
}.bind(this));
} catch (e) {
try { try {
if (tmpDir && tmpDir.exists()) { if (tmpDir && tmpDir.exists()) {
tmpDir.remove(true); tmpDir.remove(true);
@ -385,9 +383,8 @@ Zotero.Attachments = new function(){
catch (e) { catch (e) {
Zotero.debug(e, 1); Zotero.debug(e, 1);
} }
throw e; throw e;
}); }
// We don't have any way of knowing that the file is flushed to disk, // We don't have any way of knowing that the file is flushed to disk,
// so we just wait a second before indexing and hope for the best. // so we just wait a second before indexing and hope for the best.