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:
parent
d8f3be4bee
commit
e6ccca3230
|
@ -321,52 +321,49 @@ 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
|
|
||||||
if(!(data instanceof Components.interfaces.nsIInputStream)) {
|
|
||||||
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
||||||
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
|
|
||||||
converter.charset = charset ? Zotero.CharacterSets.getName(charset) : "UTF-8";
|
|
||||||
data = converter.convertToInputStream(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
var deferred = Zotero.Promise.defer(),
|
// Create a stream for async stream copying
|
||||||
ostream = FileUtils.openSafeFileOutputStream(file);
|
if(!(data instanceof Components.interfaces.nsIInputStream)) {
|
||||||
NetUtil.asyncCopy(data, ostream, function(inputStream, status) {
|
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
|
||||||
if (!Components.isSuccessCode(status)) {
|
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
|
||||||
deferred.reject(new Components.Exception("File write operation failed", status));
|
converter.charset = charset ? Zotero.CharacterSets.getName(charset) : "UTF-8";
|
||||||
return;
|
data = converter.convertToInputStream(data);
|
||||||
}
|
|
||||||
deferred.resolve();
|
|
||||||
});
|
|
||||||
return deferred.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var deferred = Zotero.Promise.defer(),
|
||||||
|
ostream = FileUtils.openSafeFileOutputStream(file);
|
||||||
|
NetUtil.asyncCopy(data, ostream, function(inputStream, status) {
|
||||||
|
if (!Components.isSuccessCode(status)) {
|
||||||
|
deferred.reject(new Components.Exception("File write operation failed", status));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
deferred.resolve();
|
||||||
|
});
|
||||||
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user