From 72481b072e7a2d95a98eba6425c463b90fbb2574 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 16 Jun 2018 03:07:46 -0400 Subject: [PATCH] Clean up extra files from Mendeley imports Follow-up to cdee741a6 --- .../zotero/import/mendeley/mendeleyImport.js | 52 +++++++++++++++++++ chrome/content/zotero/xpcom/schema.js | 8 +++ chrome/content/zotero/zoteroPane.js | 12 +++++ resource/schema/userdata.sql | 2 +- 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/import/mendeley/mendeleyImport.js b/chrome/content/zotero/import/mendeley/mendeleyImport.js index 6cfa67352..07b2fb265 100644 --- a/chrome/content/zotero/import/mendeley/mendeleyImport.js +++ b/chrome/content/zotero/import/mendeley/mendeleyImport.js @@ -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'"); +}; diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index d493de0bf..860274ce9 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -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 } diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index e06d5ff08..478f70a83 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -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) } diff --git a/resource/schema/userdata.sql b/resource/schema/userdata.sql index 7e40eca6a..9685169fb 100644 --- a/resource/schema/userdata.sql +++ b/resource/schema/userdata.sql @@ -1,4 +1,4 @@ --- 100 +-- 101 -- Copyright (c) 2009 Center for History and New Media -- George Mason University, Fairfax, Virginia, USA