Mendeley import: Fix duplicate PDF copying for PDFs in Downloaded

For each PDF with an associated URL in the Downloaded directory, we were
copying all files in the directory (!) to the attachment's storage
directory. (Zotero imports always have files in separate directories,
and this was a function used to save both single files and HTML
snapshots.)

We'll clean up the extra files in a separate step.
This commit is contained in:
Dan Stillman 2018-06-16 01:46:51 -04:00
parent 68d03bc6c3
commit cdee741a6d
2 changed files with 24 additions and 9 deletions

View file

@ -983,6 +983,7 @@ Zotero_Import_Mendeley.prototype._saveFilesAndAnnotations = async function (file
options.title = file.title; options.title = file.title;
options.url = file.url; options.url = file.url;
options.contentType = file.contentType; options.contentType = file.contentType;
options.singleFile = true;
attachment = await Zotero.Attachments.importSnapshotFromFile(options); attachment = await Zotero.Attachments.importSnapshotFromFile(options);
} }
else { else {

View file

@ -176,14 +176,18 @@ Zotero.Attachments = new function(){
/** /**
* @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID' * @param {Object} options - 'file', 'url', 'title', 'contentType', 'charset', 'parentItemID', 'singleFile'
* @return {Promise<Zotero.Item>} * @return {Promise<Zotero.Item>}
*/ */
this.importSnapshotFromFile = Zotero.Promise.coroutine(function* (options) { this.importSnapshotFromFile = Zotero.Promise.coroutine(function* (options) {
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; // TODO: Fix main filename when copying directory, though in that case it's probably
// from our own export and already clean
var fileName = options.singleFile
? Zotero.File.getValidFileName(file.leafName)
: 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;
@ -194,7 +198,7 @@ Zotero.Attachments = new function(){
throw new Error("parentItemID not provided"); throw new Error("parentItemID not provided");
} }
var attachmentItem, itemID, destDir, newFile; var attachmentItem, itemID, destDir, newPath;
try { try {
yield Zotero.DB.executeTransaction(function* () { yield Zotero.DB.executeTransaction(function* () {
// Create a new attachment // Create a new attachment
@ -217,13 +221,23 @@ Zotero.Attachments = new function(){
var storageDir = Zotero.getStorageDirectory(); var storageDir = Zotero.getStorageDirectory();
destDir = this.getStorageDirectory(attachmentItem); destDir = this.getStorageDirectory(attachmentItem);
yield OS.File.removeDir(destDir.path); yield OS.File.removeDir(destDir.path);
file.parent.copyTo(storageDir, destDir.leafName); newPath = OS.Path.join(destDir.path, fileName);
// Copy single file to new directory
// Point to copied file if (options.singleFile) {
newFile = destDir.clone(); yield this.createDirectoryForItem(attachmentItem);
newFile.append(file.leafName); yield OS.File.copy(file.path, newPath);
}
// Copy entire parent directory (for HTML snapshots)
else {
file.parent.copyTo(storageDir, destDir.leafName);
}
}.bind(this)); }.bind(this));
yield _postProcessFile(attachmentItem, newFile, contentType, charset); yield _postProcessFile(
attachmentItem,
Zotero.File.pathToFile(newPath),
contentType,
charset
);
} }
catch (e) { catch (e) {
Zotero.logError(e); Zotero.logError(e);