2017-04-17 18:50:18 +00:00
|
|
|
class FileCache {
|
2017-05-15 18:19:32 +00:00
|
|
|
private static DB: IDBDatabase = null;
|
2017-04-18 04:59:38 +00:00
|
|
|
|
2017-05-15 18:19:32 +00:00
|
|
|
public static Initialize(success: ()=>void, error: (error: string)=>void): void {
|
|
|
|
var request = window.indexedDB.open("fileCache", 2);
|
2017-04-19 12:10:47 +00:00
|
|
|
|
2017-05-15 18:19:32 +00:00
|
|
|
request.onupgradeneeded = (event: any) => {
|
2017-04-19 21:05:28 +00:00
|
|
|
var db: IDBDatabase = event.target.result;
|
2017-05-15 18:19:32 +00:00
|
|
|
if(db.objectStoreNames.contains("hashes"))
|
|
|
|
db.deleteObjectStore("hashes");
|
|
|
|
|
|
|
|
if(!db.objectStoreNames.contains("files"))
|
|
|
|
db.createObjectStore("files", {keyPath: "Name", autoIncrement: false});
|
|
|
|
|
|
|
|
if(!db.objectStoreNames.contains("metadata"))
|
|
|
|
db.createObjectStore("metadata", {keyPath: "Name", autoIncrement: false});
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onerror = (event: any) => {
|
|
|
|
error("Could not upgrade the client database to the most recent version.");
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onsuccess = (event: any) => {
|
|
|
|
FileCache.DB = request.result;
|
|
|
|
success();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GetHash(fileName: string, success: (hash: string)=>void, error: (error: string)=>void): void {
|
|
|
|
var query = FileCache.DB.transaction("metadata");
|
|
|
|
var store = query.objectStore("metadata");
|
|
|
|
var request = store.get(fileName);
|
|
|
|
|
|
|
|
request.onsuccess = () => {
|
|
|
|
success(request.result);
|
2017-04-19 12:10:47 +00:00
|
|
|
};
|
2017-04-21 02:12:55 +00:00
|
|
|
|
2017-05-15 18:19:32 +00:00
|
|
|
request.onerror = (event: any) => {
|
|
|
|
error(event);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SetHash(fileName: string, hash: string) {
|
|
|
|
var query = FileCache.DB.transaction("metadata", "readwrite");
|
|
|
|
var store = query.objectStore("metadata");
|
|
|
|
store.put({Name: fileName, Hash: hash});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static GetFile(fileName: string, success: (data: Uint8Array)=>void, error: (error: string)=>void): void {
|
|
|
|
var query = FileCache.DB.transaction("files");
|
|
|
|
var store = query.objectStore("files");
|
|
|
|
var request = store.get(fileName);
|
|
|
|
|
|
|
|
request.onsuccess = () => {
|
|
|
|
success(request.result);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onerror = (event: any) => {
|
|
|
|
error(event);
|
|
|
|
};
|
2017-04-17 18:50:18 +00:00
|
|
|
}
|
2017-04-19 21:05:28 +00:00
|
|
|
|
2017-05-15 18:19:32 +00:00
|
|
|
public static SetFile(fileName: string, data: Uint8Array) {
|
|
|
|
var query = FileCache.DB.transaction("files", "readwrite");
|
|
|
|
var store = query.objectStore("files");
|
|
|
|
store.put(data, fileName);
|
|
|
|
}
|
2017-04-17 18:50:18 +00:00
|
|
|
}
|