add new worker for conversion

This commit is contained in:
Abhinav 2022-02-01 18:54:44 +05:30
parent 8a0361b788
commit f76b1c3ee3
8 changed files with 43 additions and 23 deletions

View file

@ -1,13 +1,18 @@
import QueueProcessor from 'services/queueProcessor';
import { ConvertWorker } from 'utils/comlink';
import { CustomError } from 'utils/error';
import { logError } from 'utils/sentry';
class HEICConverter {
private convertProcessor = new QueueProcessor<Blob>(5);
private convertProcessor = new QueueProcessor<Blob>(2);
private worker = null;
async convert(worker, fileBlob: Blob, format = 'JPEG'): Promise<Blob> {
async convert(fileBlob: Blob, format = 'JPEG'): Promise<Blob> {
if (!this.worker) {
this.worker = await new ConvertWorker();
}
const response = this.convertProcessor.queueUpRequest(
async () => await worker.convertHEIC(format, fileBlob)
async () => await this.worker.convertHEIC(fileBlob, format)
);
try {
return await response.promise;

View file

@ -85,11 +85,7 @@ export async function generateImageThumbnail(
let timeout = null;
if (isHEIC) {
file = new File(
[await HEICConverter.convert(worker, file)],
null,
null
);
file = new File([await HEICConverter.convert(file)], null, null);
}
let image = new Image();
imageURL = URL.createObjectURL(file);

View file

@ -0,0 +1,19 @@
import * as Comlink from 'comlink';
import { runningInBrowser } from 'utils/common';
export interface ComlinkWorker {
comlink: any;
worker: Worker;
}
export const getDedicatedConvertWorker = (): ComlinkWorker => {
if (runningInBrowser()) {
const worker = new Worker(
new URL('worker/convert.worker.js', import.meta.url),
{ name: 'ente-convert-worker' }
);
const comlink = Comlink.wrap(worker);
return { comlink, worker };
}
};
export const ConvertWorker: any = getDedicatedConvertWorker()?.comlink;

View file

@ -6,11 +6,7 @@ import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { getActualKey, getToken } from 'utils/common/key';
import { setRecoveryKey } from 'services/userService';
import { logError } from 'utils/sentry';
export interface ComlinkWorker {
comlink: any;
worker: Worker;
}
import { ComlinkWorker } from 'utils/comlink';
export interface B64EncryptionResult {
encryptedData: string;
@ -22,7 +18,7 @@ export const getDedicatedCryptoWorker = (): ComlinkWorker => {
if (runningInBrowser()) {
const worker = new Worker(
new URL('worker/crypto.worker.js', import.meta.url),
{ name: 'ente-worker' }
{ name: 'ente-crypto-worker' }
);
const comlink = Comlink.wrap(worker);
return { comlink, worker };

View file

@ -1,8 +1,8 @@
import * as HeicConvert from 'heic-convert';
export async function convertHEIC(
format: string,
fileBlob: Blob
fileBlob: Blob,
format: string
): Promise<Blob> {
const filedata = new Uint8Array(await fileBlob.arrayBuffer());
const result = await HeicConvert({ buffer: filedata, format });

View file

@ -323,13 +323,12 @@ export async function convertForPreview(file: EnteFile, fileBlob: Blob) {
}
const typeFromExtension = getFileExtension(file.metadata.title);
const worker = await new CryptoWorker();
const reader = new FileReader();
const mimeType =
(await getMimeTypeFromBlob(reader, fileBlob)) ?? typeFromExtension;
if (isFileHEIC(mimeType)) {
fileBlob = await HEICConverter.convert(worker, fileBlob);
fileBlob = await HEICConverter.convert(fileBlob);
}
return fileBlob;
}

View file

@ -0,0 +1,10 @@
import * as Comlink from 'comlink';
import { convertHEIC } from 'utils/file/convertHEIC';
export class Convert {
async convertHEIC(format, file) {
return convertHEIC(format, file);
}
}
Comlink.expose(Convert);

View file

@ -1,6 +1,5 @@
import * as Comlink from 'comlink';
import * as libsodium from 'utils/crypto/libsodium';
import { convertHEIC } from 'utils/file/convertHEIC';
export class Crypto {
async decryptMetadata(encryptedMetadata, header, key) {
@ -148,10 +147,6 @@ export class Crypto {
async fromHex(string) {
return libsodium.fromHex(string);
}
async convertHEIC(format, file) {
return convertHEIC(format, file);
}
}
Comlink.expose(Crypto);