Use OS.File.writeAtomic() for Zotero.File.putContentsAsync() if UTF-8

It might be possible to write non-UTF-8 data by passing another charset
to TextEncoder, but I haven't tried it.

Firefox 19+ only, and for now, at least, only if data is passed as
string rather than input stream
This commit is contained in:
Dan Stillman 2013-11-17 19:06:19 -05:00
parent deffa464e3
commit f2034eec29

View file

@ -245,6 +245,28 @@ Zotero.File = new function(){
* @return {Promise} A Q promise that is resolved when the file has been written
*/
this.putContentsAsync = function putContentsAsync(file, data, charset) {
if (typeof data == 'string'
&& Zotero.platformMajorVersion >= 19
&& (!charset || charset.toLowerCase() == 'utf-8')) {
let encoder = new TextEncoder();
let array = encoder.encode(data);
return Q(OS.File.writeAtomic(
file.path,
array,
{
tmpPath: OS.Path.join(Zotero.getTempDirectory().path, file.leafName + ".tmp")
}
))
.catch(function (e) {
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"].
@ -263,6 +285,7 @@ Zotero.File = new function(){
deferred.resolve();
});
return deferred.promise;
}
};