From 3ad15f7b59482d1f0fb26665ba2a55c6450b1360 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 2 Jun 2015 14:55:29 -0400 Subject: [PATCH] Use Zotero.Utilities.Internal.exec() for PDF tool calls --- chrome/content/zotero/xpcom/fulltext.js | 29 ++++--------------- .../zotero/xpcom/utilities_internal.js | 8 +++-- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/chrome/content/zotero/xpcom/fulltext.js b/chrome/content/zotero/xpcom/fulltext.js index 98e790580..bce80884e 100644 --- a/chrome/content/zotero/xpcom/fulltext.js +++ b/chrome/content/zotero/xpcom/fulltext.js @@ -624,17 +624,10 @@ Zotero.Fulltext = new function(){ // Modified 3.02 version that can output a text file directly if (_pdfInfo && _pdfInfoVersion.startsWith('3.02')) { - var args = [filePath, infoFilePath]; - - Zotero.debug("Running " + _pdfInfo.path + ' ' - + args.map(arg => "'" + arg + "'").join(' ')); - - var proc = Components.classes["@mozilla.org/process/util;1"] - .createInstance(Components.interfaces.nsIProcess); - proc.init(_pdfInfo); + let args = [filePath, infoFilePath]; try { - proc.runw(true, args, args.length); + yield Zotero.Utilities.Internal.exec(_pdfInfo, args); var totalPages = yield getTotalPagesFromFile(itemID); } catch (e) { @@ -643,17 +636,10 @@ Zotero.Fulltext = new function(){ } // Use redirection script else if (_pdfInfoScript) { - var args = [_pdfInfo.path, filePath, infoFilePath]; - - Zotero.debug("Running " + _pdfInfoScript.path + ' ' - + args.map(arg => "'" + arg + "'").join(' ')); - - var proc = Components.classes["@mozilla.org/process/util;1"] - .createInstance(Components.interfaces.nsIProcess); - proc.init(_pdfInfoScript); + let args = [_pdfInfo.path, filePath, infoFilePath]; try { - proc.runw(true, args, args.length); + yield Zotero.Utilities.Internal.exec(_pdfInfoScript, args); var totalPages = yield getTotalPagesFromFile(itemID); } catch (e) { @@ -666,8 +652,6 @@ Zotero.Fulltext = new function(){ Zotero.debug(this.pdfInfoName + " is not available"); } - var proc = Components.classes["@mozilla.org/process/util;1"] - .createInstance(Components.interfaces.nsIProcess); var {exec, args} = this.getPDFConverterExecAndArgs(); args.push('-enc', 'UTF-8', '-nopgbrk'); @@ -682,11 +666,8 @@ Zotero.Fulltext = new function(){ } args.push(filePath, cacheFilePath); - Zotero.debug("Running " + exec.path + " " + args.map(arg => "'" + arg + "'").join(" ")); - try { - proc.init(exec); - proc.runw(true, args, args.length); + yield Zotero.Utilities.Internal.exec(exec, args); } catch (e) { Components.utils.reportError(e); diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index a8b8f4116..a507645e8 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -334,20 +334,22 @@ Zotero.Utilities.Internal = { * @param {String[]} args Arguments given * @return {Promise} Promise resolved to true if command succeeds, or an error otherwise */ - "exec":function(cmd, args) { + "exec": Zotero.Promise.method(function (cmd, args) { if (typeof cmd == 'string') { Components.utils.import("resource://gre/modules/FileUtils.jsm"); cmd = new FileUtils.File(cmd); } if(!cmd.isExecutable()) { - return Zotero.Promise.reject(cmd.path+" is not an executable"); + throw new Error(cmd.path + " is not an executable"); } var proc = Components.classes["@mozilla.org/process/util;1"]. createInstance(Components.interfaces.nsIProcess); proc.init(cmd); + Zotero.debug("Running " + cmd.path + " " + args.map(arg => "'" + arg + "'").join(" ")); + var deferred = Zotero.Promise.defer(); proc.runwAsync(args, args.length, {"observe":function(subject, topic) { if(topic !== "process-finished") { @@ -360,7 +362,7 @@ Zotero.Utilities.Internal = { }}); return deferred.promise; - }, + }), /** * Get string data from the clipboard