Commit 1f5a68ed authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

[piexwasm] Add piex loader readFromFileSystem helper

Raw image preview requests contain the source file as a DOM fileSystem
URL. Add a helper to resolve the fileEntry associated with the URL and
read its content into an ArrayBuffer with the DOM fileSystem API.

Add closure markup / comments, add externs/platform.js to BUILD.gn for
the DOM fileSystem API declarations, and Closure compile the lot.

Bug: 904630
Change-Id: I80d3f695dfce939b92d42932db666c3ab272ac69
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1611624Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660692}
parent 3e91f2c1
......@@ -94,7 +94,10 @@ js_library("piex_loader") {
":load_image_request",
"../file_manager/foreground/js/metadata:image_orientation",
]
externs_list = [ "$externs_path/chrome_extensions.js" ]
externs_list = [
"$externs_path/chrome_extensions.js",
"../externs/platform.js",
]
}
js_unittest("piex_loader_unittest") {
......
......@@ -381,6 +381,53 @@ PiexLoader.prototype.simulateIdleTimeoutPassedForTests = function() {
}
};
/**
* Resolves the file entry associated with DOM filesystem |url| and returns
* the file content in an ArrayBuffer.
* @param {string} url - DOM filesystem URL of the file.
* @returns {!Promise<!ArrayBuffer>}
*/
function readFromFileSystem(url) {
return new Promise((resolve, reject) => {
/**
* Reject the Promise on fileEntry URL resolve or file read failures.
*/
function failure(error) {
reject(new Error('Reading file system: ' + error));
}
/**
* Returns true if the fileEntry file size is within sensible limits.
* @param {number} size - file size.
* @return {boolean}
*/
function valid(size) {
return size > 0 && size < Math.pow(2, 30);
}
/**
* Reads the fileEntry's content into an ArrayBuffer: resolve Promise
* with the ArrayBuffer result or reject the Promise on failure.
* @param {!Entry} entry - file system entry of |url|.
*/
function readEntry(entry) {
const fileEntry = /** @type {!FileEntry} */ (entry);
fileEntry.file((file) => {
if (valid(file.size)) {
const reader = new FileReader();
reader.onerror = failure;
reader.onload = (_) => resolve(reader.result);
reader.readAsArrayBuffer(file);
} else {
failure('invalid file size: ' + file.size);
}
}, failure);
}
window.webkitResolveLocalFileSystemURL(url, readEntry, failure);
});
}
/**
* Starts to load RAW image.
* @param {string} url
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment