Implement file reading
This commit is contained in:
parent
f57f38a41a
commit
13af3dbd2c
|
@ -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.onReadDirectoryRequested.addListener(readDirectory);
|
||||
chrome.fileSystemProvider.onGetMetadataRequested.addListener(getMetadata);
|
||||
chrome.fileSystemProvider.onOpenFileRequested.addListener(openFile);
|
||||
chrome.fileSystemProvider.onCloseFileRequested.addListener(closeFile);
|
||||
chrome.fileSystemProvider.onReadFileRequested.addListener(readFile);
|
||||
|
|
Loading…
Reference in New Issue