2021-02-17 05:01:31 +00:00
|
|
|
import { getToken } from 'utils/common/key';
|
|
|
|
import { file } from './fileService';
|
|
|
|
import HTTPService from './HTTPService';
|
|
|
|
import { getEndpoint } from 'utils/common/apiUtil';
|
2021-02-23 10:11:46 +00:00
|
|
|
import { getFileExtension } from 'utils/common/utilFunctions';
|
2021-02-25 16:31:49 +00:00
|
|
|
import CryptoWorker from 'utils/crypto/cryptoWorker';
|
2021-02-17 05:01:31 +00:00
|
|
|
|
|
|
|
const ENDPOINT = getEndpoint();
|
2021-02-25 16:31:49 +00:00
|
|
|
|
2021-02-17 05:01:31 +00:00
|
|
|
|
2021-02-24 04:41:26 +00:00
|
|
|
const heic2any = typeof window !== 'undefined' && require('heic2any');
|
2021-02-23 10:11:46 +00:00
|
|
|
const TYPE_HEIC = 'heic';
|
|
|
|
|
2021-02-17 05:01:31 +00:00
|
|
|
class DownloadManager {
|
2021-02-17 07:17:36 +00:00
|
|
|
private fileDownloads = new Map<number, Promise<string>>();
|
|
|
|
private thumbnailDownloads = new Map<number, Promise<string>>();
|
|
|
|
|
2021-02-27 22:37:42 +00:00
|
|
|
constructor(private token) { }
|
2021-02-17 05:01:31 +00:00
|
|
|
public async getPreview(file: file) {
|
|
|
|
try {
|
|
|
|
const cache = await caches.open('thumbs');
|
|
|
|
const cacheResp: Response = await cache.match(file.id.toString());
|
|
|
|
if (cacheResp) {
|
|
|
|
return URL.createObjectURL(await cacheResp.blob());
|
|
|
|
}
|
2021-02-17 07:17:36 +00:00
|
|
|
if (!this.thumbnailDownloads.get(file.id)) {
|
|
|
|
const download = (async () => {
|
|
|
|
const resp = await HTTPService.get(
|
2021-02-27 22:37:42 +00:00
|
|
|
`https://thumbnails.ente.workers.dev/?fileID=${file.id}`,
|
2021-02-17 07:17:36 +00:00
|
|
|
null,
|
|
|
|
{ 'X-Auth-Token': this.token },
|
|
|
|
{ responseType: 'arraybuffer' }
|
|
|
|
);
|
|
|
|
const worker = await new CryptoWorker();
|
|
|
|
const decrypted: any = await worker.decryptThumbnail(
|
|
|
|
new Uint8Array(resp.data),
|
|
|
|
await worker.fromB64(file.thumbnail.decryptionHeader),
|
|
|
|
file.key
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
await cache.put(
|
|
|
|
file.id.toString(),
|
|
|
|
new Response(new Blob([decrypted]))
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: handle storage full exception.
|
|
|
|
}
|
|
|
|
|
|
|
|
return URL.createObjectURL(new Blob([decrypted]));
|
|
|
|
})();
|
|
|
|
this.thumbnailDownloads.set(file.id, download);
|
2021-02-17 05:01:31 +00:00
|
|
|
}
|
2021-02-17 07:17:36 +00:00
|
|
|
return await this.thumbnailDownloads.get(file.id);
|
2021-02-17 05:01:31 +00:00
|
|
|
} catch (e) {
|
2021-02-23 10:11:46 +00:00
|
|
|
console.log('get preview Failed', e);
|
2021-02-17 05:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getFile = async (file: file) => {
|
2021-02-17 07:17:36 +00:00
|
|
|
if (!this.fileDownloads.get(file.id)) {
|
|
|
|
const download = (async () => {
|
|
|
|
try {
|
|
|
|
const resp = await HTTPService.get(
|
2021-02-27 22:37:42 +00:00
|
|
|
`https://files.ente.workers.dev/?fileID=${file.id}`,
|
2021-02-17 07:17:36 +00:00
|
|
|
null,
|
|
|
|
{ 'X-Auth-Token': this.token },
|
|
|
|
{ responseType: 'arraybuffer' }
|
|
|
|
);
|
|
|
|
const worker = await new CryptoWorker();
|
2021-02-23 10:11:46 +00:00
|
|
|
const decryptedFile: any = await worker.decryptFile(
|
2021-02-17 07:17:36 +00:00
|
|
|
new Uint8Array(resp.data),
|
|
|
|
await worker.fromB64(file.file.decryptionHeader),
|
|
|
|
file.key
|
|
|
|
);
|
2021-02-23 10:11:46 +00:00
|
|
|
let decryptedFileBlob = new Blob([decryptedFile]);
|
|
|
|
|
|
|
|
if (getFileExtension(file.metadata.title) === TYPE_HEIC) {
|
|
|
|
decryptedFileBlob = await this.convertHEIC2JPEG(
|
|
|
|
decryptedFileBlob
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return URL.createObjectURL(decryptedFileBlob);
|
2021-02-17 07:17:36 +00:00
|
|
|
} catch (e) {
|
2021-02-23 10:11:46 +00:00
|
|
|
console.log('get file failed ', e);
|
2021-02-17 07:17:36 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
this.fileDownloads.set(file.id, download);
|
2021-02-17 05:01:31 +00:00
|
|
|
}
|
2021-02-17 07:17:36 +00:00
|
|
|
return await this.fileDownloads.get(file.id);
|
2021-02-17 05:01:31 +00:00
|
|
|
};
|
2021-02-23 10:11:46 +00:00
|
|
|
|
|
|
|
private async convertHEIC2JPEG(fileBlob): Promise<Blob> {
|
|
|
|
return await heic2any({
|
|
|
|
blob: fileBlob,
|
|
|
|
toType: 'image/jpeg',
|
|
|
|
quality: 1,
|
|
|
|
});
|
|
|
|
}
|
2021-02-17 05:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new DownloadManager(getToken());
|