fx-compat: OS.File.read()
→ IOUtils.read()
in getContentsAsync()
https://firefox-source-docs.mozilla.org/dom/ioutils_migration.html This also fixes a bug when `getContentsAsync()` is passed an `nsIInputStream` or `nsIChannel` where raw bytes were returned instead of a string. Not sure if we're doing that anywhere. If we are, this would presumably break that code, but the function is supposed to return a decoded string.
This commit is contained in:
parent
2f6f2feade
commit
296c0142f2
2 changed files with 10 additions and 12 deletions
|
@ -38,6 +38,7 @@ Zotero.File = new function(){
|
||||||
this.getValidFileName = getValidFileName;
|
this.getValidFileName = getValidFileName;
|
||||||
this.truncateFileName = truncateFileName;
|
this.truncateFileName = truncateFileName;
|
||||||
|
|
||||||
|
this.REPLACEMENT_CHARACTER = "\uFFFD";
|
||||||
|
|
||||||
this.pathToFile = function (pathOrFile) {
|
this.pathToFile = function (pathOrFile) {
|
||||||
try {
|
try {
|
||||||
|
@ -210,7 +211,7 @@ Zotero.File = new function(){
|
||||||
* @param {Integer} [maxLength] Maximum length to fetch, in bytes
|
* @param {Integer} [maxLength] Maximum length to fetch, in bytes
|
||||||
* @return {Promise} A promise that is resolved with the contents of the file
|
* @return {Promise} A promise that is resolved with the contents of the file
|
||||||
*/
|
*/
|
||||||
this.getContentsAsync = Zotero.Promise.coroutine(function* (source, charset, maxLength) {
|
this.getContentsAsync = async function (source, charset, maxLength) {
|
||||||
Zotero.debug("Getting contents of "
|
Zotero.debug("Getting contents of "
|
||||||
+ (source instanceof Components.interfaces.nsIFile
|
+ (source instanceof Components.interfaces.nsIFile
|
||||||
? source.path
|
? source.path
|
||||||
|
@ -243,7 +244,6 @@ Zotero.File = new function(){
|
||||||
// The stream is closed automatically when end-of-file is reached,
|
// The stream is closed automatically when end-of-file is reached,
|
||||||
// so this throws for empty files
|
// so this throws for empty files
|
||||||
if (e.name == "NS_BASE_STREAM_CLOSED") {
|
if (e.name == "NS_BASE_STREAM_CLOSED") {
|
||||||
Zotero.debug("RESOLVING2");
|
|
||||||
deferred.resolve("");
|
deferred.resolve("");
|
||||||
}
|
}
|
||||||
deferred.reject(e);
|
deferred.reject(e);
|
||||||
|
@ -261,7 +261,10 @@ Zotero.File = new function(){
|
||||||
deferred.resolve(NetUtil.readInputStreamToString(
|
deferred.resolve(NetUtil.readInputStreamToString(
|
||||||
inputStream,
|
inputStream,
|
||||||
bytesToFetch,
|
bytesToFetch,
|
||||||
options
|
{
|
||||||
|
charset,
|
||||||
|
replacement: this.REPLACEMENT_CHARACTER
|
||||||
|
}
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -289,14 +292,9 @@ Zotero.File = new function(){
|
||||||
else {
|
else {
|
||||||
throw new Error(`Unsupported type '${typeof source}' for source`);
|
throw new Error(`Unsupported type '${typeof source}' for source`);
|
||||||
}
|
}
|
||||||
var options = {
|
var arr = await IOUtils.read(source, { maxBytes: maxLength || undefined });
|
||||||
encoding: charset ? charset : "utf-8"
|
return new TextDecoder(charset || 'utf-8').decode(arr)
|
||||||
};
|
};
|
||||||
if (maxLength) {
|
|
||||||
options.bytes = maxLength;
|
|
||||||
}
|
|
||||||
return OS.File.read(source, options);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -36,7 +36,7 @@ describe("Zotero.File", function () {
|
||||||
OS.Path.join(getTestDataDirectory().path, "charsets", "invalid.txt")
|
OS.Path.join(getTestDataDirectory().path, "charsets", "invalid.txt")
|
||||||
);
|
);
|
||||||
assert.lengthOf(contents, 3);
|
assert.lengthOf(contents, 3);
|
||||||
assert.equal(contents, "A\uFFFDB");
|
assert.equal(contents, "A" + Zotero.File.REPLACEMENT_CHARACTER + "B");
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should respect maxLength", function* () {
|
it("should respect maxLength", function* () {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue