From 7f2d83a602b0fbb2fc03c330da0045d98a8bfdb7 Mon Sep 17 00:00:00 2001 From: Sylvester Keil Date: Thu, 2 Aug 2018 13:02:28 +0200 Subject: [PATCH] Move data dire getters to Z.DataDirectory Add Z.DataDirectory.getSubdirectory which, optionally, creates the directory. Add async Z.DataDirectory.removeSubdirectory and use it for Z.removeTempDirectory (was sync call before!). --- chrome/content/zotero/xpcom/dataDirectory.js | 22 ++++++++ chrome/content/zotero/xpcom/zotero.js | 57 +++++--------------- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/chrome/content/zotero/xpcom/dataDirectory.js b/chrome/content/zotero/xpcom/dataDirectory.js index 136adc19b4..af8e2275cf 100644 --- a/chrome/content/zotero/xpcom/dataDirectory.js +++ b/chrome/content/zotero/xpcom/dataDirectory.js @@ -1228,5 +1228,27 @@ Zotero.DataDirectory = { ext = ext ? '.' + ext : ''; return OS.Path.join(this.dir, name + ext); + }, + + /** + * @param {String} name - the name of the subdirectory + * @param {Boolean} createIfMissing - ensure that the directory exists + * @return {String} the path to the subdirectory + */ + getSubdirectory: function (name, createIfMissing = false) { + let dir = OS.Path.join(this.dir, name); + if (createIfMissing) { + Zotero.File.createDirectoryIfMissing(dir); + } + return dir; + }, + + /** + * @param {String} name - the name of the subdirectory + * @return {Promise} true if the subdirectory was deleted, + * or false if it did not exist + */ + removeSubdirectory: function (name) { + return Zotero.File.removeIfExists(OS.Path.join(this.dir, name)); } }; diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 70f3df27e8..3de48b9139 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -941,7 +941,7 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); } // remove temp directory - Zotero.removeTempDirectory(); + yield Zotero.removeTempDirectory(); if (Zotero.DB) { // close DB @@ -964,63 +964,34 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js"); return Zotero.File.pathToFile(Zotero.Profile.dir); } - this.getZoteroDirectory = function () { Zotero.warn("Zotero.getZoteroDirectory() is deprecated -- use Zotero.DataDirectory.dir"); return Zotero.File.pathToFile(Zotero.DataDirectory.dir); } - - function getStorageDirectory(){ - var file = OS.Path.join(Zotero.DataDirectory.dir, 'storage'); - file = Zotero.File.pathToFile(file); - Zotero.File.createDirectoryIfMissing(file); - return file; - } - - this.getZoteroDatabase = function (name, ext) { Zotero.warn("Zotero.getZoteroDatabase() is deprecated -- use Zotero.DataDirectory.getDatabase()"); return Zotero.File.pathToFile(Zotero.DataDirectory.getDatabase(name, ext)); } - - /** - * @return {nsIFile} - */ - this.getTempDirectory = function () { - var tmp = Zotero.File.pathToFile(Zotero.DataDirectory.dir); - tmp.append('tmp'); - Zotero.File.createDirectoryIfMissing(tmp); - return tmp; + function getStorageDirectory() { + return Zotero.DataDirectory.getSubdirectory('storage', true); } - - - this.removeTempDirectory = function () { - var tmp = Zotero.File.pathToFile(Zotero.DataDirectory.dir); - tmp.append('tmp'); - if (tmp.exists()) { - try { - tmp.remove(true); - } - catch (e) {} - } - } - - + this.getStylesDirectory = function () { - var dir = Zotero.File.pathToFile(Zotero.DataDirectory.dir); - dir.append('styles'); - Zotero.File.createDirectoryIfMissing(dir); - return dir; + return Zotero.DataDirectory.getSubdirectory('styles', true); } - this.getTranslatorsDirectory = function () { - var dir = Zotero.File.pathToFile(Zotero.DataDirectory.dir); - dir.append('translators'); - Zotero.File.createDirectoryIfMissing(dir); - return dir; + return Zotero.DataDirectory.getSubdirectory('translators', true); + } + + this.getTempDirectory = function () { + return Zotero.DataDirectory.getSubdirectory('tmp', true); + } + + this.removeTempDirectory = function () { + return Zotero.DataDirectory.removeSubdirectory('tmp'); }