Clean up extra files from Mendeley imports

Follow-up to cdee741a6
This commit is contained in:
Dan Stillman 2018-06-16 03:07:46 -04:00
parent cdee741a6d
commit 72481b072e
4 changed files with 73 additions and 1 deletions

View File

@ -1156,3 +1156,55 @@ Zotero_Import_Mendeley.prototype._updateItemCollectionKeys = function (json, old
}
}
}
//
// Clean up extra files created <5.0.51
//
Zotero_Import_Mendeley.prototype.hasImportedFiles = async function () {
return !!(await Zotero.DB.valueQueryAsync(
"SELECT itemID FROM itemRelations JOIN relationPredicates USING (predicateID) "
+ "WHERE predicate='mendeleyDB:fileHash' LIMIT 1"
));
};
Zotero_Import_Mendeley.prototype.queueFileCleanup = async function () {
await Zotero.DB.queryAsync("INSERT INTO settings VALUES ('mImport', 'cleanup', 1)");
};
Zotero_Import_Mendeley.prototype.deleteNonPrimaryFiles = async function () {
var rows = await Zotero.DB.queryAsync(
"SELECT key, path FROM itemRelations "
+ "JOIN relationPredicates USING (predicateID) "
+ "JOIN items USING (itemID) "
+ "JOIN itemAttachments USING (itemID) "
+ "WHERE predicate='mendeleyDB:fileHash' AND linkMode=1" // imported_url
);
for (let row of rows) {
let dir = (Zotero.Attachments.getStorageDirectoryByLibraryAndKey(1, row.key)).path;
if (!row.path.startsWith('storage:')) {
Zotero.logError(row.path + " does not start with 'storage:'");
continue;
}
let filename = row.path.substr(8);
Zotero.debug(`Checking for extra files in ${dir}`);
await Zotero.File.iterateDirectory(dir, function* (iterator) {
while (true) {
let entry = yield iterator.next();
if (entry.name.startsWith('.zotero') || entry.name == filename) {
continue;
}
Zotero.debug(`Deleting ${entry.path}`);
try {
yield OS.File.remove(entry.path);
}
catch (e) {
Zotero.logError(e);
}
}
});
}
await Zotero.DB.queryAsync("DELETE FROM settings WHERE setting='mImport' AND key='cleanup'");
};

View File

@ -2433,6 +2433,14 @@ Zotero.Schema = new function(){
}
}
else if (i == 101) {
Components.utils.import("chrome://zotero/content/import/mendeley/mendeleyImport.js");
let importer = new Zotero_Import_Mendeley();
if (yield importer.hasImportedFiles()) {
yield importer.queueFileCleanup();
}
}
// If breaking compatibility or doing anything dangerous, clear minorUpdateFrom
}

View File

@ -260,6 +260,18 @@ var ZoteroPane = new function()
ZoteroPane_Local.show();
}, 0);
}
// TEMP: Clean up extra files from Mendeley imports <5.0.51
setTimeout(async function () {
var needsCleanup = await Zotero.DB.valueQueryAsync(
"SELECT COUNT(*) FROM settings WHERE setting='mImport' AND key='cleanup'"
)
if (!needsCleanup) return;
Components.utils.import("chrome://zotero/content/import/mendeley/mendeleyImport.js");
var importer = new Zotero_Import_Mendeley();
importer.deleteNonPrimaryFiles();
}, 10000)
}

View File

@ -1,4 +1,4 @@
-- 100
-- 101
-- Copyright (c) 2009 Center for History and New Media
-- George Mason University, Fairfax, Virginia, USA