refactor(typescript): Use ES6 import & use interface

This commit is contained in:
Emmm Monster 2021-05-23 23:47:01 +08:00
parent 3c0a9e92f9
commit a1eddb230f
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
10 changed files with 66 additions and 48 deletions

View File

@ -1,35 +1,47 @@
const NcmDecrypt = require("./ncm"); import {Decrypt as NcmDecrypt} from "./ncm";
const KwmDecrypt = require("./kwm"); import {Decrypt as XmDecrypt} from "./xm";
const XmDecrypt = require("./xm"); import {Decrypt as QmcDecrypt} from "./qmc";
const QmcDecrypt = require("./qmc"); import {Decrypt as KgmDecrypt} from "./kgm";
const RawDecrypt = require("./raw"); import {Decrypt as KwmDecrypt} from "@/decrypt/kwm";
const TmDecrypt = require("./tm"); import {Decrypt as RawDecrypt} from "@/decrypt/raw";
const KgmDecrypt = require("./kgm"); 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_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; let rt_data: Partial<DecryptResult>;
switch (raw_ext) { switch (raw_ext) {
case "ncm":// Netease Mp3/Flac 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; break;
case "kwm":// Kuwo Mp3/Flac 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 break
case "xm": // Xiami Wav/M4a/Mp3/Flac case "xm": // Xiami Wav/M4a/Mp3/Flac
case "wav":// Xiami/Raw Wav case "wav":// Xiami/Raw Wav
case "mp3":// Xiami/Raw Mp3 case "mp3":// Xiami/Raw Mp3
case "flac":// Xiami/Raw Flac case "flac":// Xiami/Raw Flac
case "m4a":// Xiami/Raw M4a 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; break;
case "ogg":// Raw Ogg 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; break;
case "tm0":// QQ Music IOS Mp3 case "tm0":// QQ Music IOS Mp3
case "tm3":// 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; break;
case "qmc3"://QQ Music Android Mp3 case "qmc3"://QQ Music Android Mp3
case "qmc2"://QQ Music Android Ogg case "qmc2"://QQ Music Android Ogg
@ -46,16 +58,16 @@ export async function CommonDecrypt(file) {
case "6f6767"://QQ Music Weiyun Ogg case "6f6767"://QQ Music Weiyun Ogg
case "6d3461"://QQ Music Weiyun M4a case "6d3461"://QQ Music Weiyun M4a
case "776176"://QQ Music Weiyun Wav 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; break;
case "tm2":// QQ Music IOS M4a case "tm2":// QQ Music IOS M4a
case "tm6":// 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; break;
case "vpr": case "vpr":
case "kgm": case "kgm":
case "kgma": case "kgma":
rt_data = await KgmDecrypt.Decrypt(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: "不支持此文件格式",} rt_data = {status: false, message: "不支持此文件格式",}

18
src/decrypt/entity.ts Normal file
View File

@ -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
}

View File

@ -6,8 +6,8 @@ import {
GetMetaFromFile, GetMetaFromFile,
SniffAudioExt SniffAudioExt
} from "@/decrypt/utils.ts"; } from "@/decrypt/utils.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser";
const musicMetadata = require("music-metadata-browser");
const VprHeader = [ const VprHeader = [
0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43, 0x05, 0x28, 0xBC, 0x96, 0xE9, 0xE4, 0x5A, 0x43,
0x91, 0xAA, 0xBD, 0xD0, 0x7A, 0xF5, 0x36, 0x31] 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 ext = SniffAudioExt(audioData);
const mime = AudioMimeType[ext]; const mime = AudioMimeType[ext];
let musicBlob = new Blob([audioData], {type: mime}); 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) const {title, artist} = GetMetaFromFile(raw_filename, musicMeta.common.title, musicMeta.common.artist)
return { return {
status: true, status: true,

View File

@ -9,6 +9,7 @@ import {
import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts"; import {Decrypt as RawDecrypt} from "@/decrypt/raw.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser"; import {parseBlob as metaParseBlob} from "music-metadata-browser";
import {DecryptResult} from "@/decrypt/entity";
const MagicHeader = [ const MagicHeader = [
0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D, 0x79, 0x65, 0x65, 0x6C, 0x69, 0x6F, 0x6E, 0x2D,
@ -16,13 +17,13 @@ const MagicHeader = [
] ]
const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
export async function Decrypt(file: File, raw_filename: string, _: string) { export async function Decrypt(file: File, raw_filename: string, _: string): Promise<DecryptResult> {
const oriData = new Uint8Array(await GetArrayBuffer(file)); const oriData = new Uint8Array(await GetArrayBuffer(file));
if (!BytesHasPrefix(oriData, MagicHeader)) { if (!BytesHasPrefix(oriData, MagicHeader)) {
if (SniffAudioExt(oriData) === "aac") { 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) let fileKey = oriData.slice(0x18, 0x20)

View File

@ -1,11 +1,12 @@
import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser";
const CryptoJS = require("crypto-js"); const CryptoJS = require("crypto-js");
const MetaFlac = require('metaflac-js'); const MetaFlac = require('metaflac-js');
const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857");
const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728");
const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D]; const MagicHeader = [0x43, 0x54, 0x45, 0x4E, 0x46, 0x44, 0x41, 0x4D];
const musicMetadata = require("music-metadata-browser");
import jimp from 'jimp'; import jimp from 'jimp';
import { import {
@ -49,7 +50,7 @@ export async function Decrypt(file, raw_filename, _) {
const mime = AudioMimeType[musicMeta.format] const mime = AudioMimeType[musicMeta.format]
try { try {
let musicBlob = new Blob([audioData], {type: mime}); 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 let shouldWrite = !originalMeta.common.album && !originalMeta.common.artists && !originalMeta.common.title
if (musicMeta.format === "mp3") { if (musicMeta.format === "mp3") {
audioData = await WriteMp3Meta( audioData = await WriteMp3Meta(

View File

@ -6,6 +6,7 @@ import {
import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask";
import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js'
import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts";
import {parseBlob as metaParseBlob} from "music-metadata-browser";
const MetaFlac = require('metaflac-js'); const MetaFlac = require('metaflac-js');
@ -13,8 +14,6 @@ const MetaFlac = require('metaflac-js');
const iconv = require('iconv-lite'); const iconv = require('iconv-lite');
const decode = iconv.decode const decode = iconv.decode
const musicMetadata = require("music-metadata-browser");
const HandlerMap = { const HandlerMap = {
"mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true}, "mgg": {handler: QmcMaskDetectMgg, ext: "ogg", detect: true},
"mflac": {handler: QmcMaskDetectMflac, ext: "flac", 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}); let musicBlob = new Blob([musicDecoded], {type: mime});
const musicMeta = await musicMetadata.parseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
for (let metaIdx in musicMeta.native) { for (let metaIdx in musicMeta.native) {
if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) { if (musicMeta.native[metaIdx].some(item => item.id === "TCON" && item.value === "(12)")) {
console.warn("The metadata is using gbk encoding") console.warn("The metadata is using gbk encoding")

View File

@ -1,9 +1,11 @@
import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts"; import {AudioMimeType, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile, SniffAudioExt} from "@/decrypt/utils.ts";
import {DecryptResult} from "@/decrypt/entity";
import {parseBlob as metaParseBlob} from "music-metadata-browser"; 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<DecryptResult> {
let ext = raw_ext; let ext = raw_ext;
if (detect) { if (detect) {
const buffer = new Uint8Array(await GetArrayBuffer(file)); const buffer = new Uint8Array(await GetArrayBuffer(file));

View File

@ -1,9 +1,10 @@
import {Decrypt as RawDecrypt} from "./raw"; import {Decrypt as RawDecrypt} from "./raw";
import {GetArrayBuffer} from "@/decrypt/utils.ts"; import {GetArrayBuffer} from "@/decrypt/utils.ts";
import {DecryptResult} from "@/decrypt/entity";
const TM_HEADER = [0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70]; 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<DecryptResult> {
const audioData = new Uint8Array(await GetArrayBuffer(file)); const audioData = new Uint8Array(await GetArrayBuffer(file));
for (let cur = 0; cur < 8; ++cur) { for (let cur = 0; cur < 8; ++cur) {
audioData[cur] = TM_HEADER[cur]; audioData[cur] = TM_HEADER[cur];

View File

@ -4,23 +4,6 @@ const ID3Writer = require("browser-id3-writer");
export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis" 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) { export async function GetWebImage(pic_url) {
try { try {
let resp = await fetch(pic_url); let resp = await fetch(pic_url);

View File

@ -1,7 +1,8 @@
import {Decrypt as RawDecrypt} from "./raw"; import {Decrypt as RawDecrypt} from "./raw";
import {AudioMimeType, BytesHasPrefix, GetArrayBuffer, GetCoverFromFile, GetMetaFromFile} from "@/decrypt/utils.ts"; 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 MagicHeader = [0x69, 0x66, 0x6D, 0x74]
const MagicHeader2 = [0xfe, 0xfe, 0xfe, 0xfe] const MagicHeader2 = [0xfe, 0xfe, 0xfe, 0xfe]
const FileTypeMap = { const FileTypeMap = {
@ -37,7 +38,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
const mime = AudioMimeType[ext]; const mime = AudioMimeType[ext];
let musicBlob = new Blob([audioData], {type: mime}); let musicBlob = new Blob([audioData], {type: mime});
const musicMeta = await musicMetadata.parseBlob(musicBlob); const musicMeta = await metaParseBlob(musicBlob);
if (ext === "wav") { if (ext === "wav") {
//todo:未知的编码方式 //todo:未知的编码方式
console.log(musicMeta.common) console.log(musicMeta.common)