2023-05-09 00:22:00 +00:00
|
|
|
import { WorkerServerBus } from '~/util/WorkerEventBus';
|
2023-05-08 16:36:10 +00:00
|
|
|
import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
|
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
import type { CryptoFactory } from './crypto/CryptoBase';
|
|
|
|
import { XiamiCrypto } from './crypto/xiami/xiami';
|
2023-05-13 14:55:02 +00:00
|
|
|
import { QMC1Crypto } from './crypto/qmc/qmc_v1';
|
2023-05-09 00:23:35 +00:00
|
|
|
|
2023-05-08 16:36:10 +00:00
|
|
|
const bus = new WorkerServerBus();
|
|
|
|
onmessage = bus.onmessage;
|
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
const decryptorFactories: CryptoFactory[] = [
|
|
|
|
// Xiami (*.xm)
|
|
|
|
() => new XiamiCrypto(),
|
2023-05-13 19:04:55 +00:00
|
|
|
|
|
|
|
// QMCv1 (*.qmcflac)
|
2023-05-13 14:55:02 +00:00
|
|
|
() => new QMC1Crypto(),
|
2023-05-09 00:23:35 +00:00
|
|
|
];
|
|
|
|
|
2023-05-08 16:36:10 +00:00
|
|
|
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
|
2023-05-09 00:23:35 +00:00
|
|
|
const blob = await fetch(blobURI).then((r) => r.blob());
|
|
|
|
|
|
|
|
for (const factory of decryptorFactories) {
|
|
|
|
const decryptor = factory();
|
|
|
|
if (await decryptor.isSupported(blob)) {
|
|
|
|
const decrypted = await decryptor.decrypt(blob);
|
|
|
|
return { decrypted: URL.createObjectURL(decrypted) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('could not decrypt file: no working decryptor found');
|
2023-05-08 16:36:10 +00:00
|
|
|
});
|