166 lines
4.8 KiB
JavaScript
166 lines
4.8 KiB
JavaScript
'use strict';
|
|
|
|
function onMountRequested(successCallback, errorCallback) {
|
|
console.log("mount requested");
|
|
getMountInfo(function(mountInfo) { mount(mountInfo, successCallback, errorCallback); });
|
|
}
|
|
|
|
|
|
function getMountInfo(callback) {
|
|
let windowCallback = function(appWindow) {
|
|
let listener = function(message) {
|
|
if(message.senderId == appWindow.id) {
|
|
callback(message.mountInfo);
|
|
} else {
|
|
console.log(message);
|
|
console.log(appWindow);
|
|
console.log(message.senderId);
|
|
console.log(appWindow.id);
|
|
}
|
|
chrome.runtime.onMessage.removeListener(listener);
|
|
};
|
|
chrome.runtime.onMessage.addListener(listener);
|
|
};
|
|
|
|
let windowOptions = { outerBounds : { width: 800, height: 600 }, resizable : false, id: Date.now().toString() };
|
|
chrome.app.window.create("mount-info.html", windowOptions, windowCallback);
|
|
}
|
|
|
|
function mount(mountInfo, successCallback, errorCallback) {
|
|
console.log("mount()");
|
|
let request = new XMLHttpRequest();
|
|
request.open("GET", mountInfo.url);
|
|
request.onreadystatechange = function() {
|
|
if(request.readyState != XMLHttpRequest.DONE) return;
|
|
|
|
try {
|
|
JSON.parse(request.responseText);
|
|
|
|
let options = { fileSystemId: mountInfo.url, displayName: mountInfo.name, writable: false };
|
|
chrome.fileSystemProvider.mount(options);
|
|
|
|
console.log("mount succeeded");
|
|
successCallback();
|
|
} catch(exception) {
|
|
console.log(exception);
|
|
console.log("mount failed");
|
|
errorCallback();
|
|
//TODO: Properly show error (e.g. notification)
|
|
}
|
|
};
|
|
request.send();
|
|
}
|
|
|
|
function readDirectory(options, successCallback, errorCallback) {
|
|
let request = new XMLHttpRequest();
|
|
request.open("GET", options.fileSystemId + "/" + options.directoryPath);
|
|
request.onreadystatechange = function() {
|
|
if(request.readyState != XMLHttpRequest.DONE) return;
|
|
|
|
try {
|
|
let json = JSON.parse(request.responseText);
|
|
let entries = json.map(function(item) {
|
|
let entry = {};
|
|
if(options.isDirectory) {
|
|
entry.isDirectory = item.type == "directory";
|
|
}
|
|
if(options.name) {
|
|
entry.name = item.name;
|
|
}
|
|
if(options.size) {
|
|
entry.size = item.size;
|
|
}
|
|
if(options.modificationTime) {
|
|
entry.modificationTime = new Date(item.mtime);
|
|
}
|
|
return entry;
|
|
});
|
|
|
|
successCallback(entries, false);
|
|
} catch(exception) {
|
|
console.log(exception);
|
|
console.log("readDirectory failed");
|
|
errorCallback("FAILED");
|
|
//TODO: Properly show error (e.g. notification)
|
|
}
|
|
};
|
|
request.send();
|
|
}
|
|
|
|
function getMetadata(options, successCallback, errorCallback) {
|
|
if(options.entryPath == "/") {
|
|
successCallback({ isDirectory: true, name: "", size: 0, modificationTime: new Date() });
|
|
} else {
|
|
let parentDirectory = options.entryPath.substring(0, options.entryPath.lastIndexOf('/') + 1);
|
|
let baseName = options.entryPath.substr(options.entryPath.lastIndexOf('/') + 1);
|
|
options.directoryPath = parentDirectory;
|
|
let nameRequested = options.name;
|
|
options.name = true;
|
|
readDirectory(options, function(entries, more) {
|
|
for(let entry of entries) {
|
|
if(entry.name == baseName) {
|
|
if(!nameRequested) {
|
|
delete entry.name;
|
|
options.name = false;
|
|
}
|
|
successCallback(entry);
|
|
return;
|
|
}
|
|
}
|
|
errorCallback("NOT_FOUND");
|
|
}, 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);
|