optional conversion of heic if needed for preview

This commit is contained in:
Abhinav-grd 2021-07-21 11:38:41 +05:30
parent 063797df55
commit 4b420b2144
2 changed files with 13 additions and 14 deletions

View file

@ -285,7 +285,7 @@ const PhotoFrame = ({
if (galleryContext.files.has(item.id)) {
url = galleryContext.files.get(item.id);
} else {
url = await DownloadManager.getFile(item);
url = await DownloadManager.getFile(item, true);
galleryContext.files.set(item.id, url);
}
updateSrcUrl(item.dataIndex, url);

View file

@ -7,7 +7,7 @@ import { File } from './fileService';
import { logError } from 'utils/sentry';
class DownloadManager {
private fileDownloads = new Map<number, string>();
private fileDownloads = new Map<string, string>();
private thumbnailDownloads = new Map<number, string>();
@ -55,14 +55,19 @@ class DownloadManager {
return URL.createObjectURL(new Blob([decrypted]));
}
getFile = async (file: File) => {
getFile = async (file: File, forPreview=false) => {
try {
if (!this.fileDownloads.get(file.id)) {
if (!this.fileDownloads.get(`${file.id}_${forPreview}`)) {
const fileStream = await this.downloadFile(file);
const download = URL.createObjectURL(await new Response(fileStream).blob());
this.fileDownloads.set(file.id, download);
let fileBlob= await new Response(fileStream).blob();
if (forPreview) {
if (fileIsHEIC(file.metadata.title)) {
fileBlob = await convertHEIC2JPEG(fileBlob);
}
return await this.fileDownloads.get(file.id);
}
this.fileDownloads.set(`${file.id}_${forPreview}`, URL.createObjectURL(fileBlob));
}
return this.fileDownloads.get(`${file.id}_${forPreview}`);
} catch (e) {
logError(e, 'Failed to get File');
}
@ -86,16 +91,10 @@ class DownloadManager {
await worker.fromB64(file.file.decryptionHeader),
file.key,
);
let decryptedBlob = new Blob([decrypted]);
if (fileIsHEIC(file.metadata.title)) {
decryptedBlob = await convertHEIC2JPEG(decryptedBlob);
}
return new ReadableStream({
async start(controller: ReadableStreamDefaultController) {
controller.enqueue(
new Uint8Array(await decryptedBlob.arrayBuffer()),
);
controller.enqueue(decrypted);
controller.close();
},
});