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:
parent
deffa464e3
commit
f2034eec29
1 changed files with 39 additions and 16 deletions
|
@ -245,24 +245,47 @@ Zotero.File = new function(){
|
||||||
* @return {Promise} A Q promise that is resolved when the file has been written
|
* @return {Promise} A Q promise that is resolved when the file has been written
|
||||||
*/
|
*/
|
||||||
this.putContentsAsync = function putContentsAsync(file, data, charset) {
|
this.putContentsAsync = function putContentsAsync(file, data, charset) {
|
||||||
// Create a stream for async stream copying
|
if (typeof data == 'string'
|
||||||
if(!(data instanceof Components.interfaces.nsIInputStream)) {
|
&& Zotero.platformMajorVersion >= 19
|
||||||
var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
|
&& (!charset || charset.toLowerCase() == 'utf-8')) {
|
||||||
createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
|
let encoder = new TextEncoder();
|
||||||
converter.charset = charset ? Zotero.CharacterSets.getName(charset) : "UTF-8";
|
let array = encoder.encode(data);
|
||||||
data = converter.convertToInputStream(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 {
|
||||||
var deferred = Q.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();
|
|
||||||
});
|
var deferred = Q.defer(),
|
||||||
return deferred.promise;
|
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…
Add table
Add a link
Reference in a new issue