commit f2034eec290ddddbb90df9f1b548b17a3802ec63
parent deffa464e38881342d42d0a55f04fd052791de94
Author: Dan Stillman <dstillman@zotero.org>
Date: Sun, 17 Nov 2013 19:06:19 -0500
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
Diffstat:
1 file changed, 39 insertions(+), 16 deletions(-)
diff --git a/chrome/content/zotero/xpcom/file.js b/chrome/content/zotero/xpcom/file.js
@@ -245,24 +245,47 @@ 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) {
- // 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);
+ 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;
+ });
}
-
- var deferred = Q.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;
+ 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);
}
- deferred.resolve();
- });
- return deferred.promise;
+
+ var deferred = Q.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;
+ }
};