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,6 +326,7 @@ Zotero.Attachments = new function(){
yield deferred.promise; yield deferred.promise;
let sample = yield Zotero.File.getSample(tmpFile); let sample = yield Zotero.File.getSample(tmpFile);
try {
if (contentType == 'application/pdf' && 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 "
@ -338,7 +339,7 @@ Zotero.Attachments = new function(){
// Create DB item // Create DB item
var attachmentItem; var attachmentItem;
var destDir; var destDir;
yield Zotero.DB.executeTransaction(function* () { yield Zotero.DB.executeTransaction(function*() {
// Create a new attachment // Create a new attachment
attachmentItem = new Zotero.Item('attachment'); attachmentItem = new Zotero.Item('attachment');
if (libraryID) { if (libraryID) {
@ -369,11 +370,8 @@ Zotero.Attachments = new function(){
// Refetch item to update path // Refetch item to update path
attachmentItem.attachmentPath = destFile.path; attachmentItem.attachmentPath = destFile.path;
yield attachmentItem.save(saveOptions); yield attachmentItem.save(saveOptions);
}.bind(this)) }.bind(this));
.catch(function (e) { } catch (e) {
Zotero.debug(e, 1);
// Clean up
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.