tweb/src/lib/cacheStorage.ts
morethanwords 68c93396e8 Almost completed task #1
New storage (CacheStorage)
Safari fixes
Webpack configuration
New slider animation
2020-06-05 19:01:06 +03:00

65 lines
1.7 KiB
TypeScript

import {blobConstruct, bytesToBase64, blobSafeMimeType, dataUrlToBlob} from './bin_utils';
import FileManager from './filemanager';
import { logger } from './polyfill';
class CacheStorageController {
public dbName = 'cachedFiles';
public openDbPromise: Promise<Cache>;
private log: ReturnType<typeof logger> = logger('CS');
constructor() {
this.openDatabase();
}
public openDatabase(): Promise<Cache> {
if(this.openDbPromise) {
return this.openDbPromise;
}
return this.openDbPromise = caches.open(this.dbName);
}
public async deleteFile(fileName: string): Promise<void> {
const cache = await this.openDatabase();
const deleted = await cache.delete('/' + fileName);
}
public async saveFile(fileName: string, blob: Blob | Uint8Array): Promise<Blob> {
//return Promise.resolve(blobConstruct([blob]));
if(!(blob instanceof Blob)) {
blob = blobConstruct(blob) as Blob;
}
const cache = await this.openDatabase();
await cache.put('/' + fileName, new Response(blob));
return blob;
}
public getBlobSize(blob: any) {
return blob.size || blob.byteLength || blob.length;
}
public async getFile(fileName: string): Promise<Blob> {
//return Promise.reject();
const cache = await this.openDatabase();
const response = await cache.match('/' + fileName);
return response.blob();
}
public getFileWriter(fileName: string, mimeType: string) {
const fakeWriter = FileManager.getFakeFileWriter(mimeType, (blob: any) => {
this.saveFile(fileName, blob);
});
return Promise.resolve(fakeWriter);
}
}
const cacheStorage = new CacheStorageController();
(window as any).cacheStorage = cacheStorage;
export default cacheStorage;