2021-06-03 05:00:35 +00:00
|
|
|
import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts";
|
|
|
|
|
|
|
|
import {DecryptResult} from "@/decrypt/entity";
|
|
|
|
|
|
|
|
import {parseBlob as metaParseBlob} from "music-metadata-browser";
|
|
|
|
|
2021-06-03 04:47:51 +00:00
|
|
|
export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string)
|
2021-06-03 05:00:35 +00:00
|
|
|
: Promise<DecryptResult> {
|
2021-06-03 04:47:51 +00:00
|
|
|
const buffer = new Uint8Array(await GetArrayBuffer(file));
|
|
|
|
let length = buffer.length
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
buffer[i] ^= 163
|
2021-06-03 05:00:35 +00:00
|
|
|
}
|
2021-06-03 04:47:51 +00:00
|
|
|
const ext = SniffAudioExt(buffer, raw_ext);
|
|
|
|
if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]})
|
2021-06-03 05:00:35 +00:00
|
|
|
const tag = await metaParseBlob(file);
|
|
|
|
const {title, artist} = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist)
|
|
|
|
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
ext,
|
|
|
|
album: tag.common.album,
|
|
|
|
picture: GetCoverFromFile(tag),
|
|
|
|
file: URL.createObjectURL(file),
|
|
|
|
blob: file,
|
|
|
|
mime: AudioMimeType[ext]
|
|
|
|
}
|
|
|
|
}
|