Take either nsIFile or string path in Zotero.File.putContentsAsync()

And throw an OS.File.Error directly instead of catching it
This commit is contained in:
Dan Stillman 2015-03-22 16:03:26 -04:00
parent d8f3be4bee
commit e6ccca3230

View File

@ -321,33 +321,31 @@ Zotero.File = new function(){
/** /**
* Write data to a file asynchronously * Write data to a file asynchronously
* *
* @param {nsIFile} - The file to write to * @param {String|nsIFile} - String path or nsIFile to write to
* @param {String|nsIInputStream} data - The string or nsIInputStream to write to the file * @param {String|nsIInputStream} data - The string or nsIInputStream to write to the file
* @param {String} [charset] - The character set; defaults to UTF-8 * @param {String} [charset] - The character set; defaults to UTF-8
* @return {Promise} - A promise that is resolved when the file has been written * @return {Promise} - A promise that is resolved when the file has been written
*/ */
this.putContentsAsync = function putContentsAsync(file, data, charset) { this.putContentsAsync = function putContentsAsync(path, data, charset) {
if (path instanceof Ci.nsIFile) {
path = this.fileToPath(path);
}
if (typeof data == 'string' && (!charset || charset.toLowerCase() == 'utf-8')) { if (typeof data == 'string' && (!charset || charset.toLowerCase() == 'utf-8')) {
let encoder = new TextEncoder(); let encoder = new TextEncoder();
let array = encoder.encode(data); let array = encoder.encode(data);
return Zotero.Promise.resolve(OS.File.writeAtomic( return Zotero.Promise.resolve(OS.File.writeAtomic(
file.path, path,
array, array,
{ {
tmpPath: OS.Path.join(Zotero.getTempDirectory().path, file.leafName + ".tmp") tmpPath: OS.Path.join(
Zotero.getTempDirectory().path,
OS.Path.basename(path) + ".tmp"
)
} }
)) ));
.catch(function (e) {
Zotero.debug(e); // TEMP
if (e instanceof OS.File.Error) {
Zotero.debug(e);
Zotero.debug(e.toString());
throw new Error("Error for operation '" + e.operation + "' for " + file.path);
} }
throw e;
});
}
else {
// Create a stream for async stream copying // Create a stream for async stream copying
if(!(data instanceof Components.interfaces.nsIInputStream)) { if(!(data instanceof Components.interfaces.nsIInputStream)) {
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]. var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
@ -366,7 +364,6 @@ Zotero.File = new function(){
deferred.resolve(); deferred.resolve();
}); });
return deferred.promise; return deferred.promise;
}
}; };