1
0
forked from um/web
qqmusicunlock/src/decrypt/joox.ts
鲁树人 2855f72c03 chore: bump to 0.0.1-R4
(cherry picked from commit 2da37f984a8ed4ca369e2efecb2da5d71976c93e)
2022-01-09 10:44:52 +08:00

35 lines
1.1 KiB
TypeScript

import { DecryptResult } from './entity';
import { AudioMimeType, GetArrayBuffer, SniffAudioExt } from './utils';
import jooxFactory from '@unlock-music/joox-crypto';
import storage from '@/utils/storage';
import { MergeUint8Array } from '@/utils/MergeUint8Array';
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
const uuid = await storage.loadJooxUUID('');
if (!uuid || uuid.length !== 32) {
throw new Error('请在“解密设定”填写应用 Joox 应用的 UUID。');
}
const fileBuffer = new Uint8Array(await GetArrayBuffer(file));
const decryptor = jooxFactory(fileBuffer, uuid);
if (!decryptor) {
throw new Error('不支持的 joox 加密格式');
}
const musicDecoded = MergeUint8Array(decryptor.decryptFile(fileBuffer));
const ext = SniffAudioExt(musicDecoded);
const mime = AudioMimeType[ext];
const musicBlob = new Blob([musicDecoded], { type: mime });
return {
title: raw_filename.replace(/\.[^\.]+$/, ''),
artist: '未知',
album: '未知',
file: URL.createObjectURL(musicBlob),
blob: musicBlob,
mime: mime,
ext: ext,
};
}