Commit 225bec75 authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

[piexwasm] test.html: implement file system create and file write api

Implement createFileSystem(): assume the images are up to 30M each in
size when creating the HTML file system.

Implement writeToFileSystem(): fetch the image from the server, store
it in the file system. Strip the leading 'image/' from the test image
name when storing the image (repetative, and not needed).

Bug: 935285
Change-Id: Id62e37b05c15b305377c0bccc149e260f6d911d4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1562254
Commit-Queue: Noel Gordon <noel@chromium.org>
Reviewed-by: default avatarAlex Danilo <adanilo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#650573}
parent 2c5ac00d
...@@ -86,6 +86,53 @@ piex wasm raw image preview / thumbnail test page ...@@ -86,6 +86,53 @@ piex wasm raw image preview / thumbnail test page
} }
} }
function createFileSystem(images) {
return new Promise((resolve, reject) => {
document.title = 'createFileSystem';
function failed(error) {
reject(new Error('Creating file system: ' + error.name));
}
function createdFileSystem(fileSystem) {
console.log('test: created file system', fileSystem.name);
window.fileSystem = fileSystem;
resolve();
}
const bytes = images * 30 * 1024 * 1024; // 30M per image.
window.webkitRequestFileSystem(
window.TEMPORARY, bytes, createdFileSystem, failed);
});
}
function writeToFileSystem(image) {
return new Promise(async (resolve, reject) => {
document.title = image;
const buffer = await fetch(image).then((response) => {
if (!response.ok)
throw new Error('Failed to fetch image: ' + image);
return response.arrayBuffer();
}).catch(reject);
function failure(error) {
reject(new Error('Writing file system: ' + error.name));
}
function writeEntry(fileEntry) {
fileEntry.createWriter((writer) => {
writer.onerror = failure;
writer.onwrite = resolve;
writer.write(new Blob([buffer]));
}, failure);
}
window.fileSystem.root.getFile(
image.replace('images/', ''), {create: true}, writeEntry, failure);
});
}
function hashUint8Array(data, hash = ~0) { function hashUint8Array(data, hash = ~0) {
for (let i = 0; i < data.byteLength; ++i) for (let i = 0; i < data.byteLength; ++i)
hash = (hash << 5) - hash + data[i]; hash = (hash << 5) - hash + data[i];
......
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