From a1eddb230f3cb00e4abe5b1cb1800d40fb5660ff Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 23:47:01 +0800 Subject: [PATCH] refactor(typescript): Use ES6 import & use interface --- src/decrypt/{common.js => common.ts} | 46 ++++++++++++++++++---------- src/decrypt/entity.ts | 18 +++++++++++ src/decrypt/kgm.js | 4 +-- src/decrypt/kwm.ts | 7 +++-- src/decrypt/ncm.js | 5 +-- src/decrypt/qmc.js | 5 ++- src/decrypt/raw.ts | 4 ++- src/decrypt/{tm.js => tm.ts} | 3 +- src/decrypt/util.js | 17 ---------- src/decrypt/xm.js | 5 +-- 10 files changed, 66 insertions(+), 48 deletions(-) rename src/decrypt/{common.js => common.ts} (60%) create mode 100644 src/decrypt/entity.ts rename src/decrypt/{tm.js => tm.ts} (76%) diff --git a/src/decrypt/common.js b/src/decrypt/common.ts similarity index 60% rename from src/decrypt/common.js rename to src/decrypt/common.ts index 0b026ab..0081088 100644 --- a/src/decrypt/common.js +++ b/src/decrypt/common.ts @@ -1,35 +1,47 @@ -const NcmDecrypt = require("./ncm"); -const KwmDecrypt = require("./kwm"); -const XmDecrypt = require("./xm"); -const QmcDecrypt = require("./qmc"); -const RawDecrypt = require("./raw"); -const TmDecrypt = require("./tm"); -const KgmDecrypt = require("./kgm"); +import {Decrypt as NcmDecrypt} from "./ncm"; +import {Decrypt as XmDecrypt} from "./xm"; +import {Decrypt as QmcDecrypt} from "./qmc"; +import {Decrypt as KgmDecrypt} from "./kgm"; +import {Decrypt as KwmDecrypt} from "@/decrypt/kwm"; +import {Decrypt as RawDecrypt} from "@/decrypt/raw"; +import {Decrypt as TmDecrypt} from "@/decrypt/tm"; +import {DecryptResult} from "@/decrypt/entity"; -export async function CommonDecrypt(file) { + +interface FileInfo { + status: string + name: string, + size: number, + percentage: number, + uid: number, + raw: File +} + + +export async function CommonDecrypt(file: FileInfo) { 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 rt_data; + let rt_data: Partial; switch (raw_ext) { case "ncm":// Netease Mp3/Flac - rt_data = await NcmDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await NcmDecrypt(file.raw, raw_filename, raw_ext); break; case "kwm":// Kuwo Mp3/Flac - rt_data = await KwmDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await KwmDecrypt(file.raw, raw_filename, raw_ext); break case "xm": // Xiami Wav/M4a/Mp3/Flac case "wav":// Xiami/Raw Wav case "mp3":// Xiami/Raw Mp3 case "flac":// Xiami/Raw Flac case "m4a":// Xiami/Raw M4a - rt_data = await XmDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await XmDecrypt(file.raw, raw_filename, raw_ext); break; case "ogg":// Raw Ogg - rt_data = await RawDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await RawDecrypt(file.raw, raw_filename, raw_ext); break; case "tm0":// QQ Music IOS Mp3 case "tm3":// QQ Music IOS Mp3 - rt_data = await RawDecrypt.Decrypt(file.raw, raw_filename, "mp3"); + rt_data = await RawDecrypt(file.raw, raw_filename, "mp3"); break; case "qmc3"://QQ Music Android Mp3 case "qmc2"://QQ Music Android Ogg @@ -46,16 +58,16 @@ export async function CommonDecrypt(file) { case "6f6767"://QQ Music Weiyun Ogg case "6d3461"://QQ Music Weiyun M4a case "776176"://QQ Music Weiyun Wav - rt_data = await QmcDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await QmcDecrypt(file.raw, raw_filename, raw_ext); break; case "tm2":// QQ Music IOS M4a case "tm6":// QQ Music IOS M4a - rt_data = await TmDecrypt.Decrypt(file.raw, raw_filename); + rt_data = await TmDecrypt(file.raw, raw_filename); break; case "vpr": case "kgm": case "kgma": - rt_data = await KgmDecrypt.Decrypt(file.raw, raw_filename, raw_ext); + rt_data = await KgmDecrypt(file.raw, raw_filename, raw_ext); break default: rt_data = {status: false, message: "不支持此文件格式",} diff --git a/src/decrypt/entity.ts b/src/decrypt/entity.ts new file mode 100644 index 0000000..55faead --- /dev/null +++ b/src/decrypt/entity.ts @@ -0,0 +1,18 @@ +export interface DecryptResult { + status: boolean,//todo: remove & use Exception + + title: string + album?: string + artist?: string + + mime: string + ext: string + + file: string + picture: string + + message?: string + rawExt?: string + rawFilename?: string + +} diff --git a/src/decrypt/kgm.js b/src/decrypt/kgm.js index dd7f968..a6320b8 100644 --- a/src/decrypt/kgm.js +++ b/src/decrypt/kgm.js @@ -6,8 +6,8 @@ import { GetMetaFromFile, SniffAudioExt } from "@/decrypt/utils.ts"; +import {parseBlob as metaParseBlob} from "music-metadata-browser"; -const musicMetadata = require("music-metadata-browser"); const VprHeader = [ 0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43, 0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31] @@ -71,7 +71,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { const ext = SniffAudioExt(audioData); const mime = AudioMimeType[ext]; let musicBlob = new Blob([audioData], {type: mime}); - const musicMeta = await musicMetadata.parseBlob(musicBlob); + const musicMeta = await metaParseBlob(musicBlob); const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist) return { status: true, diff --git a/src/decrypt/kwm.ts b/src/decrypt/kwm.ts index 82a9ed9..54b52f1 100644 --- a/src/decrypt/kwm.ts +++ b/src/decrypt/kwm.ts @@ -9,6 +9,7 @@ import { import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts"; import {parseBlob as metaParseBlob} from "music-metadata-browser"; +import {DecryptResult} from "@/decrypt/entity"; const MagicHeader = [ 0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D, @@ -16,13 +17,13 @@ const MagicHeader = [ ] const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" -export async function Decrypt(file: File, raw_filename: string, _: string) { +export async function Decrypt(file: File, raw_filename: string, _: string): Promise { const oriData = new Uint8Array(await GetArrayBuffer(file)); if (!BytesHasPrefix(oriData, MagicHeader)) { if (SniffAudioExt(oriData) === "aac") { - return await RawDecrypt(file, raw_filename, "aac", true) + return await RawDecrypt(file, raw_filename, "aac", false) } - return {status: false, message: "Not a valid kwm file!"} + throw Error("not a valid kwm file") } let fileKey = oriData.slice(0x18, 0x20) diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 63fafc8..eb3934e 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -1,11 +1,12 @@ import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; +import {parseBlob as metaParseBlob} from "music-metadata-browser"; const CryptoJS = require("crypto-js"); const MetaFlac = require('metaflac-js'); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D]; -const musicMetadata = require("music-metadata-browser"); + import jimp from 'jimp'; import { @@ -49,7 +50,7 @@ export async function Decrypt(file, raw_filename, _) { const mime = AudioMimeType[musicMeta.format] try { let musicBlob = new Blob([audioData], {type: mime}); - const originalMeta = await musicMetadata.parseBlob(musicBlob); + const originalMeta = await metaParseBlob(musicBlob); 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 e3adc7c..30cbd6b 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -6,6 +6,7 @@ import { import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; +import {parseBlob as metaParseBlob} from "music-metadata-browser"; const MetaFlac = require('metaflac-js'); @@ -13,8 +14,6 @@ const MetaFlac = require('metaflac-js'); const iconv = require('iconv-lite'); const decode = iconv.decode -const musicMetadata = require("music-metadata-browser"); - const HandlerMap = { "mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true}, "mflac": {handler: QmcMaskDetectMflac, ext: "flac", detect: true}, @@ -58,7 +57,7 @@ export async function Decrypt(file, raw_filename, raw_ext) { let musicBlob = new Blob([musicDecoded], {type: mime}); - const musicMeta = await musicMetadata.parseBlob(musicBlob); + const musicMeta = await metaParseBlob(musicBlob); for (let metaIdx in musicMeta.native) { if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) { console.warn("The metadata is using gbk encoding") diff --git a/src/decrypt/raw.ts b/src/decrypt/raw.ts index a59d643..fa7e0ab 100644 --- a/src/decrypt/raw.ts +++ b/src/decrypt/raw.ts @@ -1,9 +1,11 @@ import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; +import {DecryptResult} from "@/decrypt/entity"; import {parseBlob as metaParseBlob} from "music-metadata-browser"; -export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string, detect: boolean = true) { +export async function Decrypt(file: Blob, raw_filename: string, raw_ext: string, detect: boolean = true) + : Promise { let ext = raw_ext; if (detect) { const buffer = new Uint8Array(await GetArrayBuffer(file)); diff --git a/src/decrypt/tm.js b/src/decrypt/tm.ts similarity index 76% rename from src/decrypt/tm.js rename to src/decrypt/tm.ts index 63b1ca2..654ccaf 100644 --- a/src/decrypt/tm.js +++ b/src/decrypt/tm.ts @@ -1,9 +1,10 @@ import {Decrypt as RawDecrypt} from "./raw"; import {GetArrayBuffer} from "@/decrypt/utils.ts"; +import {DecryptResult} from "@/decrypt/entity"; const TM_HEADER = [0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70]; -export async function Decrypt(file, raw_filename) { +export async function Decrypt(file: File, raw_filename: string): Promise { const audioData = new Uint8Array(await GetArrayBuffer(file)); for (let cur = 0; cur < 8; ++cur) { audioData[cur] = TM_HEADER[cur]; diff --git a/src/decrypt/util.js b/src/decrypt/util.js index 2145d6b..f5a77d5 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -4,23 +4,6 @@ const ID3Writer = require("browser-id3-writer"); export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis" -export function GetFileInfo(artist, title, filenameNoExt, separator = "-") { - let newArtist = "", newTitle = ""; - let filenameArray = filenameNoExt.split(separator); - if (filenameArray.length > 1) { - newArtist = filenameArray[0].trim(); - newTitle = filenameArray[1].trim(); - } else if (filenameArray.length === 1) { - newTitle = filenameArray[0].trim(); - } - - if (typeof artist == "string" && artist !== "") newArtist = artist; - if (typeof title == "string" && title !== "") newTitle = title; - return {artist: newArtist, title: newTitle}; -} - - - export async function GetWebImage(pic_url) { try { let resp = await fetch(pic_url); diff --git a/src/decrypt/xm.js b/src/decrypt/xm.js index 7b563af..5443c47 100644 --- a/src/decrypt/xm.js +++ b/src/decrypt/xm.js @@ -1,7 +1,8 @@ import {Decrypt as RawDecrypt} from "./raw"; import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile} from "@/decrypt/utils.ts"; -const musicMetadata = require("music-metadata-browser"); +import {parseBlob as metaParseBlob} from "music-metadata-browser"; + const MagicHeader = [0x69, 0x66, 0x6D, 0x74] const MagicHeader2 = [0xfe, 0xfe, 0xfe, 0xfe] const FileTypeMap = { @@ -37,7 +38,7 @@ 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 musicMeta = await metaParseBlob(musicBlob); if (ext === "wav") { //todo:未知的编码方式 console.log(musicMeta.common)