added rendering HEIC logic
This commit is contained in:
parent
2d78aa93fc
commit
8dbf35c360
1 changed files with 23 additions and 4 deletions
|
@ -3,12 +3,15 @@ import { file } from './fileService';
|
|||
import HTTPService from './HTTPService';
|
||||
import { getEndpoint } from 'utils/common/apiUtil';
|
||||
import * as Comlink from 'comlink';
|
||||
import { getFileExtension } from 'utils/common/utilFunctions';
|
||||
|
||||
const ENDPOINT = getEndpoint();
|
||||
const CryptoWorker: any =
|
||||
typeof window !== 'undefined' &&
|
||||
Comlink.wrap(new Worker('worker/crypto.worker.js', { type: 'module' }));
|
||||
|
||||
const TYPE_HEIC = 'heic';
|
||||
|
||||
class DownloadManager {
|
||||
private fileDownloads = new Map<number, Promise<string>>();
|
||||
private thumbnailDownloads = new Map<number, Promise<string>>();
|
||||
|
@ -50,7 +53,7 @@ class DownloadManager {
|
|||
}
|
||||
return await this.thumbnailDownloads.get(file.id);
|
||||
} catch (e) {
|
||||
console.log('get preview Failed' , e );
|
||||
console.log('get preview Failed', e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,20 +68,36 @@ class DownloadManager {
|
|||
{ responseType: 'arraybuffer' }
|
||||
);
|
||||
const worker = await new CryptoWorker();
|
||||
const decrypted: any = await worker.decryptFile(
|
||||
const decryptedFile: any = await worker.decryptFile(
|
||||
new Uint8Array(resp.data),
|
||||
await worker.fromB64(file.file.decryptionHeader),
|
||||
file.key
|
||||
);
|
||||
return URL.createObjectURL(new Blob([decrypted]));
|
||||
let decryptedFileBlob = new Blob([decryptedFile]);
|
||||
|
||||
if (getFileExtension(file.metadata.title) === TYPE_HEIC) {
|
||||
decryptedFileBlob = await this.convertHEIC2JPEG(
|
||||
decryptedFileBlob
|
||||
);
|
||||
}
|
||||
return URL.createObjectURL(decryptedFileBlob);
|
||||
} catch (e) {
|
||||
console.log('get file failed ' , e );
|
||||
console.log('get file failed ', e);
|
||||
}
|
||||
})();
|
||||
this.fileDownloads.set(file.id, download);
|
||||
}
|
||||
return await this.fileDownloads.get(file.id);
|
||||
};
|
||||
|
||||
private async convertHEIC2JPEG(fileBlob): Promise<Blob> {
|
||||
const heic2any = require('heic2any');
|
||||
return await heic2any({
|
||||
blob: fileBlob,
|
||||
toType: 'image/jpeg',
|
||||
quality: 1,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new DownloadManager(getToken());
|
||||
|
|
Loading…
Add table
Reference in a new issue