Implement file reading

This commit is contained in:
Les De Ridder 2017-05-20 06:00:18 +02:00
parent f57f38a41a
commit 13af3dbd2c
No known key found for this signature in database
GPG Key ID: 5EC132DFA85DB372
1 changed files with 48 additions and 0 deletions

View File

@ -112,6 +112,54 @@ function getMetadata(options, successCallback, errorCallback) {
} }
} }
let openFiles = {};
function openFile(options, successCallback, errorCallback) {
//TODO: Check if file exists (through getMetadata, or by sending HEAD for the file)
openFiles[options.requestId] = options.fileSystemId + options.filePath;
successCallback();
}
function closeFile(options, successCallback, errorCallback) {
if(options.openRequestId in openFiles) {
delete openFiles[options.openRequestId];
successCallback();
} else {
errorCallback("FAILED");
}
}
function readFile(options, successCallback, errorCallback) {
let url = openFiles[options.openRequestId];
let start = options.offset;
let end = start + options.length - 1;
let request = new XMLHttpRequest();
request.open("GET", url);
request.setRequestHeader("Range", "bytes=" + start + "-" + end);
request.responseType = "arraybuffer";
request.onreadystatechange = function() {
if(request.readyState != XMLHttpRequest.DONE) return;
successCallback(request.response, false);
}
try {
request.send();
} catch(exception) {
if(request.status == 404) {
errorCallback("NOT_FOUND");
} else {
console.log("readFile failed");
console.log(exception);
errorCallback("FAILED");
}
}
}
chrome.fileSystemProvider.onMountRequested.addListener(onMountRequested); chrome.fileSystemProvider.onMountRequested.addListener(onMountRequested);
chrome.fileSystemProvider.onReadDirectoryRequested.addListener(readDirectory); chrome.fileSystemProvider.onReadDirectoryRequested.addListener(readDirectory);
chrome.fileSystemProvider.onGetMetadataRequested.addListener(getMetadata); chrome.fileSystemProvider.onGetMetadataRequested.addListener(getMetadata);
chrome.fileSystemProvider.onOpenFileRequested.addListener(openFile);
chrome.fileSystemProvider.onCloseFileRequested.addListener(closeFile);
chrome.fileSystemProvider.onReadFileRequested.addListener(readFile);