feat: added audio ext detection

This commit is contained in:
鲁树人
2023-05-14 21:57:18 +01:00
parent c734a83854
commit 77a3eba036
7 changed files with 67 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ import { DECRYPTION_WORKER_ACTION_NAME } from './constants';
import type { CryptoFactory } from './crypto/CryptoBase';
import { XiamiCrypto } from './crypto/xiami/xiami';
import { QMC1Crypto } from './crypto/qmc/qmc_v1';
import { fetchParakeet } from '@jixun/libparakeet';
const bus = new WorkerServerBus();
onmessage = bus.onmessage;
@@ -16,14 +17,27 @@ const decryptorFactories: CryptoFactory[] = [
() => new QMC1Crypto(),
];
// Use first 4MiB of the file to perform check.
const TEST_FILE_HEADER_LEN = 1024 * 1024 * 4;
bus.addEventHandler(DECRYPTION_WORKER_ACTION_NAME.DECRYPT, async (blobURI) => {
const blob = await fetch(blobURI).then((r) => r.blob());
const parakeet = await fetchParakeet();
for (const factory of decryptorFactories) {
const decryptor = factory();
if (await decryptor.isSupported(blob)) {
const decrypted = await decryptor.decrypt(blob);
return { decrypted: URL.createObjectURL(decrypted) };
const decryptedBlob = await decryptor.decrypt(blob);
// Check if we had a successful decryption
const header = await decryptedBlob.slice(0, TEST_FILE_HEADER_LEN).arrayBuffer();
const audioExt = parakeet.detectAudioExtension(header);
if (!decryptor.hasSignature() && audioExt === 'bin') {
// skip this decryptor result
continue;
}
return { decrypted: URL.createObjectURL(decryptedBlob), ext: audioExt };
}
}