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.

69 lines
1.7 KiB

import {blobConstruct} from './bin_utils';
export class FileManager {
public blobSupported = true;
constructor() {
try {
blobConstruct([], '');
} catch(e) {
this.blobSupported = false;
}
}
public isAvailable() {
return this.blobSupported;
}
4 years ago
public write(fileWriter: ReturnType<FileManager['getFakeFileWriter']>, bytes: Uint8Array | Blob | {file: any}): Promise<void> {
4 years ago
if('file' in bytes) {
return bytes.file((file: any) => {
return fileWriter.write(file);
});
} else if(bytes instanceof Blob) { // is file bytes
return new Promise((resolve, reject) => {
let fileReader = new FileReader();
fileReader.onload = function(event) {
let arrayBuffer = event.target.result as ArrayBuffer;
let arr = new Uint8Array(arrayBuffer);
fileWriter.write(arr).then(resolve, reject);
};
fileReader.readAsArrayBuffer(bytes);
});
} else {
return fileWriter.write(bytes);
}
}
4 years ago
public getFakeFileWriter(mimeType: string, saveFileCallback: (blob: Blob) => Promise<Blob>) {
let blobParts: Array<Uint8Array> = [];
const fakeFileWriter = {
4 years ago
write: async(blob: Uint8Array) => {
if(!this.blobSupported) {
throw false;
}
blobParts.push(blob);
},
truncate: () => {
blobParts = [];
},
finalize: () => {
4 years ago
const blob = blobConstruct(blobParts, mimeType) as Blob;
if(saveFileCallback) {
saveFileCallback(blob);
}
return blob;
}
};
return fakeFileWriter;
}
}
export default new FileManager();