Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7 KiB

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;