From 3c0a9e92f9c6f0b9a50ee2034ba666bc39b18ae0 Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 23:06:21 +0800 Subject: [PATCH] refactor(typescript): utils.GetCoverFromFile & utils.GetMetaFromFile --- src/decrypt/kgm.js | 21 +++++++++++++-------- src/decrypt/kwm.ts | 25 ++++++++++++++----------- src/decrypt/ncm.js | 8 ++------ src/decrypt/qmc.js | 10 ++++------ src/decrypt/raw.ts | 14 +++++++------- src/decrypt/util.js | 11 ----------- src/decrypt/utils.ts | 31 +++++++++++++++++++++++++++++++ src/decrypt/xm.js | 21 +++++++++------------ 8 files changed, 80 insertions(+), 61 deletions(-) diff --git a/src/decrypt/kgm.js b/src/decrypt/kgm.js index 9c159f2..dd7f968 100644 --- a/src/decrypt/kgm.js +++ b/src/decrypt/kgm.js @@ -1,5 +1,11 @@ -import {GetFileInfo, GetMetaCoverURL} from "./util"; -import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import { + AudioMimeType, + BytesHasPrefix, + GetArrayBuffer, + GetCoverFromFile, + GetMetaFromFile, + SniffAudioExt +} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const VprHeader = [ @@ -66,17 +72,16 @@ export async function Decrypt(file, raw_filename, raw_ext) { const mime = AudioMimeType[ext]; let musicBlob = new Blob([audioData], {type: mime}); const musicMeta = await musicMetadata.parseBlob(musicBlob); - const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename); - const imgUrl = GetMetaCoverURL(musicMeta); + const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) return { status: true, - title: info.title, - artist: info.artist, album: musicMeta.common.album, - picture: imgUrl, + picture: GetCoverFromFile(musicMeta), file: URL.createObjectURL(musicBlob), ext, - mime + mime, + title, + artist } } diff --git a/src/decrypt/kwm.ts b/src/decrypt/kwm.ts index 1cd7a82..82a9ed9 100644 --- a/src/decrypt/kwm.ts +++ b/src/decrypt/kwm.ts @@ -1,5 +1,11 @@ -import {GetFileInfo, GetMetaCoverURL} from "./util"; -import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import { + AudioMimeType, + BytesHasPrefix, + GetArrayBuffer, + GetCoverFromFile, + GetMetaFromFile, + SniffAudioExt +} from "@/decrypt/utils.ts"; import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts"; import {parseBlob as metaParseBlob} from "music-metadata-browser"; @@ -32,19 +38,16 @@ export async function Decrypt(file: File, raw_filename: string, _: string) { let musicBlob = new Blob([audioData], {type: mime}); const musicMeta = await metaParseBlob(musicBlob); - const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename); - - const imgUrl = GetMetaCoverURL(musicMeta); - + const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) return { status: true, - title: info.title, - artist: info.artist, - ext: ext, album: musicMeta.common.album, - picture: imgUrl, + picture: GetCoverFromFile(musicMeta), file: URL.createObjectURL(musicBlob), - mime: mime + mime, + title, + artist, + ext } } diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index a76394e..63fafc8 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -1,4 +1,4 @@ -import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; const CryptoJS = require("crypto-js"); const MetaFlac = require('metaflac-js'); @@ -9,7 +9,6 @@ const musicMetadata = require("music-metadata-browser"); import jimp from 'jimp'; import { - GetFileInfo, GetWebImage, WriteMp3Meta } from "./util" @@ -36,11 +35,10 @@ export async function Decrypt(file, raw_filename, _) { const artists = []; if (!!musicMeta.artist) musicMeta.artist.forEach(arr => artists.push(arr[0])); - const info = GetFileInfo(artists.join("; "), musicMeta.musicName, raw_filename); + const info = GetMetaFromFile(raw_filename, musicMeta.musicName, artists.join("; ")) if (artists.length === 0) artists.push(info.artist); if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData); - console.log(musicMeta) const imageInfo = await GetWebImage(musicMeta.albumPic); while (!!imageInfo.buffer && imageInfo.buffer.byteLength >= 16 * 1024 * 1024) { @@ -48,12 +46,10 @@ export async function Decrypt(file, raw_filename, _) { await img.resize(Math.round(img.getHeight() / 2), jimp.AUTO) imageInfo.buffer = await img.getBufferAsync("image/jpeg") } - console.log(imageInfo) const mime = AudioMimeType[musicMeta.format] try { let musicBlob = new Blob([audioData], {type: mime}); const originalMeta = await musicMetadata.parseBlob(musicBlob); - console.log(originalMeta) let shouldWrite = !originalMeta.common.album && !originalMeta.common.artists && !originalMeta.common.title if (musicMeta.format === "mp3") { audioData = await WriteMp3Meta( diff --git a/src/decrypt/qmc.js b/src/decrypt/qmc.js index 0fd1e07..e3adc7c 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -1,13 +1,11 @@ import { - GetFileInfo, - GetMetaCoverURL, GetWebImage, IXAREA_API_ENDPOINT, WriteMp3Meta } from "./util"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' -import {AudioMimeType, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; const MetaFlac = require('metaflac-js'); @@ -70,12 +68,12 @@ export async function Decrypt(file, raw_filename, raw_ext) { } } - const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename); + const info = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) if (handler.detect) reportKeyUsage(keyData, seed.Matrix128, info.artist, info.title, musicMeta.common.album, raw_filename, raw_ext); - let imgUrl = GetMetaCoverURL(musicMeta); - if (imgUrl === "") { + let imgUrl = GetCoverFromFile(musicMeta); + if (!imgUrl) { imgUrl = await queryAlbumCoverImage(info.artist, info.title, musicMeta.common.album); if (imgUrl !== "") { const imageInfo = await GetWebImage(imgUrl); diff --git a/src/decrypt/raw.ts b/src/decrypt/raw.ts index 2cfbe59..a59d643 100644 --- a/src/decrypt/raw.ts +++ b/src/decrypt/raw.ts @@ -1,8 +1,7 @@ -import {AudioMimeType, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; import {parseBlob as metaParseBlob} from "music-metadata-browser"; -import {GetMetaCoverURL, GetFileInfo} from "./util"; export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string, detect: boolean = true) { let ext = raw_ext; @@ -12,14 +11,15 @@ export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string, if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]}) } const tag = await metaParseBlob(file); - const info = GetFileInfo(tag.common.artist, tag.common.title, raw_filename); + const {title, artist} = GetMetaFromFile(raw_filename, tag.common.title, tag.common.artist) + return { status: true, - title: info.title, - artist: info.artist, - ext: ext, + title, + artist, + ext, album: tag.common.album, - picture: GetMetaCoverURL(tag), + picture: GetCoverFromFile(tag), file: URL.createObjectURL(file), mime: AudioMimeType[ext] } diff --git a/src/decrypt/util.js b/src/decrypt/util.js index 13e076a..2145d6b 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -19,17 +19,6 @@ export function GetFileInfo(artist, title, filenameNoExt, separator = "-") { return {artist: newArtist, title: newTitle}; } -/** - * @return {string} - */ -export function GetMetaCoverURL(metadata) { - let pic_url = ""; - if (metadata.common.picture !== undefined && metadata.common.picture.length > 0) { - let pic = new Blob([metadata.common.picture[0].data], {type: metadata.common.picture[0].format}); - pic_url = URL.createObjectURL(pic); - } - return pic_url; -} export async function GetWebImage(pic_url) { diff --git a/src/decrypt/utils.ts b/src/decrypt/utils.ts index 74a3229..95e1b5a 100644 --- a/src/decrypt/utils.ts +++ b/src/decrypt/utils.ts @@ -1,3 +1,5 @@ +import {IAudioMetadata} from "music-metadata-browser"; + export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43]; export const MP3_HEADER = [0x49, 0x44, 0x33]; export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53]; @@ -58,3 +60,32 @@ export function GetArrayBuffer(obj: Blob): Promise { reader.readAsArrayBuffer(obj); }); } + +export function GetCoverFromFile(metadata: IAudioMetadata): string { + if (metadata.common?.picture && metadata.common.picture.length > 0) { + return URL.createObjectURL(new Blob( + [metadata.common.picture[0].data], + {type: metadata.common.picture[0].format} + )); + } + return ""; +} + +export interface IMusicMetaBasic { + title: string + artist?: string +} + +export function GetMetaFromFile(filename: string, exist_title?: string, exist_artist?: string, separator = "-") + : IMusicMetaBasic { + const meta: IMusicMetaBasic = {title: exist_title ?? "", artist: exist_artist} + + const items = filename.split(separator); + if (items.length > 1) { + if (!meta.artist) meta.artist = items[0].trim(); + if (!meta.title) meta.title = items[1].trim(); + } else if (items.length === 1) { + if (!meta.title) meta.title = items[0].trim(); + } + return meta +} diff --git a/src/decrypt/xm.js b/src/decrypt/xm.js index dbc20c1..7b563af 100644 --- a/src/decrypt/xm.js +++ b/src/decrypt/xm.js @@ -1,7 +1,5 @@ -import {GetFileInfo, GetMetaCoverURL} from "./util"; - import {Decrypt as RawDecrypt} from "./raw"; -import {AudioMimeType, BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts"; +import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [0x69, 0x66, 0x6D, 0x74] @@ -47,20 +45,19 @@ export async function Decrypt(file, raw_filename, raw_ext) { musicMeta.common.artist = ""; musicMeta.common.title = ""; } - let _sep = raw_filename.indexOf("_") === -1 ? "-" : "_" - const info = GetFileInfo(musicMeta.common.artist, musicMeta.common.title, raw_filename, _sep); - - const imgUrl = GetMetaCoverURL(musicMeta); + const {title, artist} = GetMetaFromFile(raw_filename, + musicMeta.common.title, musicMeta.common.artist, + raw_filename.indexOf("_") === -1 ? "-" : "_") return { status: true, - title: info.title, - artist: info.artist, - ext: ext, + title, + artist, + ext, + mime, album: musicMeta.common.album, - picture: imgUrl, + picture: GetCoverFromFile(musicMeta), file: URL.createObjectURL(musicBlob), - mime: mime, rawExt: "xm" } }