2021-12-18 13:55:31 +00:00
|
|
|
import { AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt } from '@/decrypt/utils';
|
2021-05-23 13:40:43 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import { DecryptResult } from '@/decrypt/entity';
|
2020-01-21 11:03:41 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
import { parseBlob as metaParseBlob } from 'music-metadata-browser';
|
2021-05-23 14:29:34 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
export async function Decrypt(
|
|
|
|
file: Blob,
|
|
|
|
raw_filename: string,
|
|
|
|
raw_ext: string,
|
|
|
|
detect: boolean = true,
|
|
|
|
): Promise<DecryptResult> {
|
|
|
|
let ext = raw_ext;
|
|
|
|
if (detect) {
|
|
|
|
const buffer = new Uint8Array(await GetArrayBuffer(file));
|
|
|
|
ext = SniffAudioExt(buffer, raw_ext);
|
|
|
|
if (ext !== raw_ext) file = new Blob([buffer], { type: AudioMimeType[ext] });
|
|
|
|
}
|
|
|
|
const tag = await metaParseBlob(file);
|
2022-11-20 14:30:56 +00:00
|
|
|
const { title, artist } = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artists == undefined ? tag.common.artist : tag.common.artists.toString());
|
2021-05-23 15:06:21 +00:00
|
|
|
|
2021-12-18 13:55:31 +00:00
|
|
|
return {
|
|
|
|
title,
|
|
|
|
artist,
|
|
|
|
ext,
|
|
|
|
album: tag.common.album,
|
|
|
|
picture: GetCoverFromFile(tag),
|
|
|
|
file: URL.createObjectURL(file),
|
|
|
|
blob: file,
|
|
|
|
mime: AudioMimeType[ext],
|
|
|
|
};
|
2020-01-21 11:03:41 +00:00
|
|
|
}
|