Avoid double item save when adding attachment
This commit is contained in:
parent
6e1e2dcf76
commit
ff798d332b
|
@ -170,6 +170,7 @@ Zotero.Attachments = new function(){
|
||||||
Zotero.debug('Importing snapshot from file');
|
Zotero.debug('Importing snapshot from file');
|
||||||
|
|
||||||
var file = Zotero.File.pathToFile(options.file);
|
var file = Zotero.File.pathToFile(options.file);
|
||||||
|
var fileName = file.leafName;
|
||||||
var url = options.url;
|
var url = options.url;
|
||||||
var title = options.title;
|
var title = options.title;
|
||||||
var contentType = options.contentType;
|
var contentType = options.contentType;
|
||||||
|
@ -193,6 +194,7 @@ Zotero.Attachments = new function(){
|
||||||
attachmentItem.attachmentLinkMode = this.LINK_MODE_IMPORTED_URL;
|
attachmentItem.attachmentLinkMode = this.LINK_MODE_IMPORTED_URL;
|
||||||
attachmentItem.attachmentContentType = contentType;
|
attachmentItem.attachmentContentType = contentType;
|
||||||
attachmentItem.attachmentCharset = charset;
|
attachmentItem.attachmentCharset = charset;
|
||||||
|
attachmentItem.attachmentPath = 'storage:' + fileName;
|
||||||
|
|
||||||
// DEBUG: this should probably insert access date too so as to
|
// DEBUG: this should probably insert access date too so as to
|
||||||
// create a proper item, but at the moment this is only called by
|
// create a proper item, but at the moment this is only called by
|
||||||
|
@ -207,9 +209,6 @@ Zotero.Attachments = new function(){
|
||||||
// Point to copied file
|
// Point to copied file
|
||||||
newFile = destDir.clone();
|
newFile = destDir.clone();
|
||||||
newFile.append(file.leafName);
|
newFile.append(file.leafName);
|
||||||
|
|
||||||
attachmentItem.attachmentPath = newFile.path;
|
|
||||||
yield attachmentItem.save();
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
yield _postProcessFile(attachmentItem, newFile, contentType, charset);
|
yield _postProcessFile(attachmentItem, newFile, contentType, charset);
|
||||||
}
|
}
|
||||||
|
@ -323,9 +322,8 @@ Zotero.Attachments = new function(){
|
||||||
// Create a temporary directory to save to within the storage directory.
|
// Create a temporary directory to save to within the storage directory.
|
||||||
// We don't use the normal temp directory because people might have 'storage'
|
// We don't use the normal temp directory because people might have 'storage'
|
||||||
// symlinked to another volume, which makes moving complicated.
|
// symlinked to another volume, which makes moving complicated.
|
||||||
var tmpDir = yield this.createTemporaryStorageDirectory();
|
var tmpDir = (yield this.createTemporaryStorageDirectory()).path;
|
||||||
var tmpFile = tmpDir.clone();
|
var tmpFile = OS.Path.join(tmpDir, fileName);
|
||||||
tmpFile.append(fileName);
|
|
||||||
|
|
||||||
// Save to temp dir
|
// Save to temp dir
|
||||||
var deferred = Zotero.Promise.defer();
|
var deferred = Zotero.Promise.defer();
|
||||||
|
@ -373,25 +371,20 @@ Zotero.Attachments = new function(){
|
||||||
if (collections) {
|
if (collections) {
|
||||||
attachmentItem.setCollections(collections);
|
attachmentItem.setCollections(collections);
|
||||||
}
|
}
|
||||||
|
attachmentItem.attachmentPath = 'storage:' + fileName;
|
||||||
var itemID = yield attachmentItem.save(saveOptions);
|
var itemID = yield attachmentItem.save(saveOptions);
|
||||||
|
|
||||||
// Create a new folder for this item in the storage directory
|
// DEBUG: Does this fail if 'storage' is symlinked to another drive?
|
||||||
destDir = this.getStorageDirectory(attachmentItem);
|
destDir = this.getStorageDirectory(attachmentItem).path;
|
||||||
yield OS.File.move(tmpDir.path, destDir.path);
|
yield OS.File.move(tmpDir, destDir);
|
||||||
var destFile = destDir.clone();
|
|
||||||
destFile.append(fileName);
|
|
||||||
|
|
||||||
// Refetch item to update path
|
|
||||||
attachmentItem.attachmentPath = destFile.path;
|
|
||||||
yield attachmentItem.save(saveOptions);
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
try {
|
try {
|
||||||
if (tmpDir && tmpDir.exists()) {
|
if (tmpDir) {
|
||||||
tmpDir.remove(true);
|
yield OS.File.removeDir(tmpDir, { ignoreAbsent: true });
|
||||||
}
|
}
|
||||||
if (destDir && destDir.exists()) {
|
if (destDir) {
|
||||||
destDir.remove(true);
|
yield OS.File.removeDir(destDir, { ignoreAbsent: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -580,11 +573,10 @@ Zotero.Attachments = new function(){
|
||||||
contentType = "application/pdf";
|
contentType = "application/pdf";
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmpDir = yield this.createTemporaryStorageDirectory();
|
var tmpDir = (yield this.createTemporaryStorageDirectory()).path;
|
||||||
try {
|
try {
|
||||||
var tmpFile = tmpDir.clone();
|
|
||||||
var fileName = Zotero.File.truncateFileName(_getFileNameFromURL(url, contentType), 100);
|
var fileName = Zotero.File.truncateFileName(_getFileNameFromURL(url, contentType), 100);
|
||||||
tmpFile.append(fileName);
|
var tmpFile = OS.Path.join(tmpDir, fileName);
|
||||||
|
|
||||||
// If we're using the title from the document, make some adjustments
|
// If we're using the title from the document, make some adjustments
|
||||||
if (!options.title) {
|
if (!options.title) {
|
||||||
|
@ -601,7 +593,7 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
if (contentType === 'text/html' || contentType === 'application/xhtml+xml') {
|
if (contentType === 'text/html' || contentType === 'application/xhtml+xml') {
|
||||||
Zotero.debug('Saving document with saveDocument()');
|
Zotero.debug('Saving document with saveDocument()');
|
||||||
yield Zotero.Utilities.Internal.saveDocument(document, tmpFile.path);
|
yield Zotero.Utilities.Internal.saveDocument(document, tmpFile);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Zotero.debug("Saving file with saveURI()");
|
Zotero.debug("Saving file with saveURI()");
|
||||||
|
@ -643,16 +635,12 @@ Zotero.Attachments = new function(){
|
||||||
if (collections && collections.length) {
|
if (collections && collections.length) {
|
||||||
attachmentItem.setCollections(collections);
|
attachmentItem.setCollections(collections);
|
||||||
}
|
}
|
||||||
|
attachmentItem.attachmentPath = 'storage:' + fileName;
|
||||||
var itemID = yield attachmentItem.save();
|
var itemID = yield attachmentItem.save();
|
||||||
|
|
||||||
// Create a new folder for this item in the storage directory
|
// DEBUG: Does this fail if 'storage' is symlinked to another drive?
|
||||||
destDir = this.getStorageDirectory(attachmentItem);
|
destDir = this.getStorageDirectory(attachmentItem).path;
|
||||||
yield OS.File.move(tmpDir.path, destDir.path);
|
yield OS.File.move(tmpDir, destDir);
|
||||||
var destFile = destDir.clone();
|
|
||||||
destFile.append(fileName);
|
|
||||||
|
|
||||||
attachmentItem.attachmentPath = destFile.path;
|
|
||||||
yield attachmentItem.save();
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -660,11 +648,11 @@ Zotero.Attachments = new function(){
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
try {
|
try {
|
||||||
if (tmpDir && tmpDir.exists()) {
|
if (tmpDir) {
|
||||||
tmpDir.remove(true);
|
yield OS.File.removeDir(tmpDir, { ignoreAbsent: true });
|
||||||
}
|
}
|
||||||
if (destDir && destDir.exists()) {
|
if (destDir) {
|
||||||
destDir.remove(true);
|
yield OS.File.removeDir(destDir, { ignoreAbsent: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user