refactor(decrypt/*): change interface

This commit is contained in:
Emmm Monster 2021-05-24 15:05:14 +08:00
parent b3c6fe2f24
commit ca4ed149b2
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
9 changed files with 5 additions and 18 deletions

View File

@ -18,10 +18,10 @@ interface FileInfo {
} }
export async function CommonDecrypt(file: FileInfo) { export async function CommonDecrypt(file: FileInfo): Promise<DecryptResult> {
let raw_ext = file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length).toLowerCase(); let raw_ext = file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length).toLowerCase();
let raw_filename = file.name.substring(0, file.name.lastIndexOf(".")); let raw_filename = file.name.substring(0, file.name.lastIndexOf("."));
let rt_data: Partial<DecryptResult>; let rt_data: DecryptResult;
switch (raw_ext) { switch (raw_ext) {
case "ncm":// Netease Mp3/Flac case "ncm":// Netease Mp3/Flac
rt_data = await NcmDecrypt(file.raw, raw_filename, raw_ext); rt_data = await NcmDecrypt(file.raw, raw_filename, raw_ext);
@ -70,7 +70,7 @@ export async function CommonDecrypt(file: FileInfo) {
rt_data = await KgmDecrypt(file.raw, raw_filename, raw_ext); rt_data = await KgmDecrypt(file.raw, raw_filename, raw_ext);
break break
default: default:
rt_data = {status: false, message: "不支持此文件格式",} throw "不支持此文件格式"
} }
if (!rt_data.rawExt) rt_data.rawExt = raw_ext; if (!rt_data.rawExt) rt_data.rawExt = raw_ext;

View File

@ -1,6 +1,4 @@
export interface DecryptResult { export interface DecryptResult {
status: boolean,//todo: remove & use Exception
title: string title: string
album?: string album?: string
artist?: string artist?: string

View File

@ -65,7 +65,6 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string)
const musicMeta = await metaParseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
return { return {
status: true,
album: musicMeta.common.album, album: musicMeta.common.album,
picture: GetCoverFromFile(musicMeta), picture: GetCoverFromFile(musicMeta),
file: URL.createObjectURL(musicBlob), file: URL.createObjectURL(musicBlob),

View File

@ -41,7 +41,6 @@ export async function Decrypt(file: File, raw_filename: string, _: string): Prom
const musicMeta = await metaParseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
return { return {
status: true,
album: musicMeta.common.album, album: musicMeta.common.album,
picture: GetCoverFromFile(musicMeta), picture: GetCoverFromFile(musicMeta),
file: URL.createObjectURL(musicBlob), file: URL.createObjectURL(musicBlob),

View File

@ -203,7 +203,6 @@ class NcmDecrypt {
gatherResult(): DecryptResult { gatherResult(): DecryptResult {
if (!this.newMeta) throw Error("bad sequence") if (!this.newMeta) throw Error("bad sequence")
return { return {
status: true,
title: this.newMeta.title, title: this.newMeta.title,
artist: this.newMeta.artists?.join("; "), artist: this.newMeta.artists?.join("; "),
ext: this.format, ext: this.format,

View File

@ -12,6 +12,7 @@ import {parseBlob as metaParseBlob} from "music-metadata-browser";
import iconv from "iconv-lite"; import iconv from "iconv-lite";
import {DecryptResult} from "@/decrypt/entity";
interface Handler { interface Handler {
ext: string ext: string
@ -38,7 +39,7 @@ const HandlerMap: { [key: string]: Handler } = {
"776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false} "776176": {handler: QmcMaskGetDefault, ext: "wav", detect: false}
}; };
export async function Decrypt(file: File, raw_filename: string, raw_ext: string) { export async function Decrypt(file: File, raw_filename: string, raw_ext: string): Promise<DecryptResult> {
if (!(raw_ext in HandlerMap)) throw "File type is incorrect!"; if (!(raw_ext in HandlerMap)) throw "File type is incorrect!";
const handler = HandlerMap[raw_ext]; const handler = HandlerMap[raw_ext];
@ -103,7 +104,6 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string)
} }
} }
return { return {
status: true,
title: info.title, title: info.title,
artist: info.artist, artist: info.artist,
ext: ext, ext: ext,

View File

@ -16,7 +16,6 @@ export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string,
const {title, artist} = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist) const {title, artist} = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist)
return { return {
status: true,
title, title,
artist, artist,
ext, ext,

View File

@ -31,12 +31,6 @@ export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean {
}) })
} }
export function BytesEquals(data: Uint8Array, another: Uint8Array): boolean {
if (another.length != data.length) return false
return data.every((val, idx) => {
return val === another[idx];
})
}
export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): string { export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): string {
if (BytesHasPrefix(data, MP3_HEADER)) return "mp3" if (BytesHasPrefix(data, MP3_HEADER)) return "mp3"

View File

@ -52,7 +52,6 @@ export async function Decrypt(file: File, raw_filename: string, raw_ext: string)
raw_filename.indexOf("_") === -1 ? "-" : "_") raw_filename.indexOf("_") === -1 ? "-" : "_")
return { return {
status: true,
title, title,
artist, artist,
ext, ext,