1
0
forked from um/web

Merge pull request #157 from unlock-music/add-typescript

Add typescript support
This commit is contained in:
EmmmX 2021-05-23 22:08:23 +08:00 committed by GitHub
commit 9ae860cb11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 1247 additions and 702 deletions

1709
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -18,7 +18,7 @@
"dependencies": { "dependencies": {
"base64-js": "^1.5.1", "base64-js": "^1.5.1",
"browser-id3-writer": "^4.4.0", "browser-id3-writer": "^4.4.0",
"core-js": "^3.10.1", "core-js": "^3.12.1",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"element-ui": "^2.15.1", "element-ui": "^2.15.1",
"iconv-lite": "^0.6.2", "iconv-lite": "^0.6.2",
@ -26,16 +26,20 @@
"metaflac-js": "^1.0.5", "metaflac-js": "^1.0.5",
"music-metadata-browser": "^2.2.6", "music-metadata-browser": "^2.2.6",
"register-service-worker": "^1.7.2", "register-service-worker": "^1.7.2",
"vue": "^2.6.12" "vue": "^2.6.12",
"vue-class-component": "^7.2.3",
"vue-property-decorator": "^9.1.2"
}, },
"devDependencies": { "devDependencies": {
"@vue/cli-plugin-babel": "^4.5.12", "@vue/cli-plugin-babel": "^4.5.13",
"@vue/cli-plugin-pwa": "^4.5.12", "@vue/cli-plugin-pwa": "^4.5.13",
"@vue/cli-service": "^4.5.12", "@vue/cli-plugin-typescript": "^4.5.13",
"@vue/cli-service": "^4.5.13",
"babel-plugin-component": "^1.1.1", "babel-plugin-component": "^1.1.1",
"node-sass": "^5.0.0", "node-sass": "^5.0.0",
"sass-loader": "^10.1.1", "sass-loader": "^10.2.0",
"semver": "^7.3.5", "semver": "^7.3.5",
"typescript": "~4.1.5",
"vue-cli-plugin-element": "^1.0.1", "vue-cli-plugin-element": "^1.0.1",
"vue-template-compiler": "^2.6.12", "vue-template-compiler": "^2.6.12",
"workerize-loader": "^1.3.0" "workerize-loader": "^1.3.0"

View File

@ -1,4 +1,5 @@
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util";
import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
const VprHeader = [ const VprHeader = [
@ -23,10 +24,10 @@ export async function Decrypt(file, raw_filename, raw_ext) {
} }
const oriData = new Uint8Array(await GetArrayBuffer(file)); const oriData = new Uint8Array(await GetArrayBuffer(file));
if (raw_ext === "vpr") { if (raw_ext === "vpr") {
if (!IsBytesEqual(VprHeader, oriData.slice(0, 0x10))) if (!BytesHasPrefix(oriData, VprHeader))
return {status: false, message: "Not a valid vpr file!"} return {status: false, message: "Not a valid vpr file!"}
} else { } else {
if (!IsBytesEqual(KgmHeader, oriData.slice(0, 0x10))) if (!BytesHasPrefix(oriData, KgmHeader))
return {status: false, message: "Not a valid kgm/kgma file!"} return {status: false, message: "Not a valid kgm/kgma file!"}
} }
let bHeaderLen = new DataView(oriData.slice(0x10, 0x14).buffer) let bHeaderLen = new DataView(oriData.slice(0x10, 0x14).buffer)
@ -61,7 +62,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
for (let i = 0; i < dataLen; i++) audioData[i] ^= VprMaskDiff[i % 17] for (let i = 0; i < dataLen; i++) audioData[i] ^= VprMaskDiff[i % 17]
} }
const ext = DetectAudioExt(audioData, "mp3"); 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 musicMetadata.parseBlob(musicBlob);
@ -71,11 +72,11 @@ export async function Decrypt(file, raw_filename, raw_ext) {
status: true, status: true,
title: info.title, title: info.title,
artist: info.artist, artist: info.artist,
ext: ext,
album: musicMeta.common.album, album: musicMeta.common.album,
picture: imgUrl, picture: imgUrl,
file: URL.createObjectURL(musicBlob), file: URL.createObjectURL(musicBlob),
mime: mime ext,
mime
} }
} }

View File

@ -1,4 +1,5 @@
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util";
import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
const MagicHeader = [ const MagicHeader = [
@ -7,9 +8,9 @@ const MagicHeader = [
] ]
const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk" const PreDefinedKey = "MoOtOiTvINGwd2E6n0E1i7L5t2IoOoNk"
export async function Decrypt(file, raw_filename, raw_ext) { export async function Decrypt(file, raw_filename, _) {
const oriData = new Uint8Array(await GetArrayBuffer(file)); const oriData = new Uint8Array(await GetArrayBuffer(file));
if (!IsBytesEqual(MagicHeader, oriData.slice(0, 0x10))) if (!BytesHasPrefix(oriData, MagicHeader))
return {status: false, message: "Not a valid kwm file!"} return {status: false, message: "Not a valid kwm file!"}
let fileKey = oriData.slice(0x18, 0x20) let fileKey = oriData.slice(0x18, 0x20)
@ -20,7 +21,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
audioData[cur] ^= mask[cur % 0x20]; audioData[cur] ^= mask[cur % 0x20];
const ext = DetectAudioExt(audioData, "mp3"); 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});

View File

@ -1,3 +1,5 @@
import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
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");
@ -8,19 +10,16 @@ import jimp from 'jimp';
import { import {
AudioMimeType, AudioMimeType,
DetectAudioExt,
GetArrayBuffer,
GetFileInfo, GetFileInfo,
GetWebImage, GetWebImage,
IsBytesEqual,
WriteMp3Meta WriteMp3Meta
} from "./util" } from "./util"
export async function Decrypt(file, raw_filename, raw_ext) { export async function Decrypt(file, raw_filename, _) {
const fileBuffer = await GetArrayBuffer(file); const fileBuffer = await GetArrayBuffer(file);
const dataView = new DataView(fileBuffer); const dataView = new DataView(fileBuffer);
if (!IsBytesEqual(MagicHeader, new Uint8Array(fileBuffer, 0, 8))) if (!BytesHasPrefix(new Uint8Array(fileBuffer, 0, 8), MagicHeader))
return {status: false, message: "此ncm文件已损坏"}; return {status: false, message: "此ncm文件已损坏"};
const keyDataObj = getKeyData(dataView, fileBuffer, 10); const keyDataObj = getKeyData(dataView, fileBuffer, 10);
@ -41,7 +40,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
const info = GetFileInfo(artists.join("; "), musicMeta.musicName, raw_filename); const info = GetFileInfo(artists.join("; "), musicMeta.musicName, raw_filename);
if (artists.length === 0) artists.push(info.artist); if (artists.length === 0) artists.push(info.artist);
if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); if (musicMeta.format === undefined) musicMeta.format = SniffAudioExt(audioData);
console.log(musicMeta) console.log(musicMeta)
const imageInfo = await GetWebImage(musicMeta.albumPic); const imageInfo = await GetWebImage(musicMeta.albumPic);

View File

@ -1,7 +1,5 @@
import { import {
AudioMimeType, AudioMimeType,
DetectAudioExt,
GetArrayBuffer,
GetFileInfo, GetFileInfo,
GetMetaCoverURL, GetMetaCoverURL,
GetWebImage, GetWebImage,
@ -10,6 +8,7 @@ import {
} from "./util"; } from "./util";
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 {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const MetaFlac = require('metaflac-js'); const MetaFlac = require('metaflac-js');
@ -58,7 +57,7 @@ export async function Decrypt(file, raw_filename, raw_ext) {
} }
let musicDecoded = seed.Decrypt(audioData); let musicDecoded = seed.Decrypt(audioData);
const ext = DetectAudioExt(musicDecoded, handler.ext); const ext = SniffAudioExt(musicDecoded, handler.ext);
const mime = AudioMimeType[ext]; const mime = AudioMimeType[ext];
let musicBlob = new Blob([musicDecoded], {type: mime}); let musicBlob = new Blob([musicDecoded], {type: mime});

View File

@ -1,4 +1,4 @@
import {FLAC_HEADER, IsBytesEqual, OGG_HEADER} from "./util" import {BytesEquals, BytesHasPrefix, FLAC_HEADER, OGG_HEADER} from "@/decrypt/utils.ts";
const QMOggPublicHeader1 = [ const QMOggPublicHeader1 = [
0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
@ -84,7 +84,7 @@ class QmcMask {
} }
let rowLeft = this.Matrix128.slice(lenStart + 1, lenStart + 8); let rowLeft = this.Matrix128.slice(lenStart + 1, lenStart + 8);
let rowRight = this.Matrix128.slice(lenRightStart + 1, lenRightStart + 8).reverse(); let rowRight = this.Matrix128.slice(lenRightStart + 1, lenRightStart + 8).reverse();
if (IsBytesEqual(rowLeft, rowRight)) { if (BytesEquals(rowLeft, rowRight)) {
matrix58 = matrix58.concat(rowLeft); matrix58 = matrix58.concat(rowLeft);
} else { } else {
throw "decode mask-128 to mask-58 failed" throw "decode mask-128 to mask-58 failed"
@ -151,7 +151,9 @@ export function QmcMaskDetectMflac(data) {
for (let block_idx = 0; block_idx < search_len; block_idx += 128) { for (let block_idx = 0; block_idx < search_len; block_idx += 128) {
try { try {
mask = new QmcMask(data.slice(block_idx, block_idx + 128)); mask = new QmcMask(data.slice(block_idx, block_idx + 128));
if (IsBytesEqual(FLAC_HEADER, mask.Decrypt(data.slice(0, FLAC_HEADER.length)))) break; if (BytesHasPrefix(mask.Decrypt(data.slice(0, FLAC_HEADER.length)), FLAC_HEADER)) {
break;
}
} catch (e) { } catch (e) {
} }
} }
@ -186,8 +188,7 @@ export function QmcMaskDetectMgg(data) {
return; return;
} }
const mask = new QmcMask(matrix); const mask = new QmcMask(matrix);
let dx = mask.Decrypt(data.slice(0, OGG_HEADER.length)); if (!BytesHasPrefix(mask.Decrypt(data.slice(0, OGG_HEADER.length)), OGG_HEADER)) {
if (!IsBytesEqual(OGG_HEADER, dx)) {
return; return;
} }

View File

@ -1,11 +1,13 @@
import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util"; import {AudioMimeType, GetMetaCoverURL, GetFileInfo} from "./util";
export async function Decrypt(file, raw_filename, raw_ext, detect = true) { export async function Decrypt(file, raw_filename, raw_ext, detect = true) {
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));
ext = DetectAudioExt(buffer, raw_ext); ext = SniffAudioExt(buffer, raw_ext);
if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]}) if (ext !== raw_ext) file = new Blob([buffer], {type: AudioMimeType[ext]})
} }
const tag = await musicMetadata.parseBlob(file); const tag = await musicMetadata.parseBlob(file);

View File

@ -1,11 +1,10 @@
import {Decrypt as RawDecrypt} from "./raw"; import {Decrypt as RawDecrypt} from "./raw";
import {GetArrayBuffer} from "./util"; import {GetArrayBuffer} from "@/decrypt/utils.ts";
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, raw_filename) {
const fileBuffer = await GetArrayBuffer(file); const audioData = new Uint8Array(await GetArrayBuffer(file));
const audioData = new Uint8Array(fileBuffer);
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

@ -1,14 +1,5 @@
const ID3Writer = require("browser-id3-writer"); const ID3Writer = require("browser-id3-writer");
const musicMetadata = require("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];
export const M4A_HEADER = [0x66, 0x74, 0x79, 0x70];
export const WMA_HEADER = [
0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11,
0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C,
]
export const WAV_HEADER = [0x52, 0x49, 0x46, 0x46]
export const AudioMimeType = { export const AudioMimeType = {
mp3: "audio/mpeg", mp3: "audio/mpeg",
flac: "audio/flac", flac: "audio/flac",
@ -19,16 +10,6 @@ export const AudioMimeType = {
}; };
export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis" export const IXAREA_API_ENDPOINT = "https://stats.ixarea.com/apis"
// Also a new draft API: blob.arrayBuffer()
export async function GetArrayBuffer(blobObject) {
return await new Promise(resolve => {
const reader = new FileReader();
reader.onload = (e) => {
resolve(e.target.result);
};
reader.readAsArrayBuffer(blobObject);
});
}
export function GetFileInfo(artist, title, filenameNoExt, separator = "-") { export function GetFileInfo(artist, title, filenameNoExt, separator = "-") {
let newArtist = "", newTitle = ""; let newArtist = "", newTitle = "";
@ -57,26 +38,6 @@ export function GetMetaCoverURL(metadata) {
return pic_url; return pic_url;
} }
export function IsBytesEqual(first, second) {
// if want wholly check, should length first>=second
return first.every((val, idx) => {
return val === second[idx];
})
}
/**
* @return {string}
*/
export function DetectAudioExt(data, fallbackExt) {
if (IsBytesEqual(MP3_HEADER, data.slice(0, MP3_HEADER.length))) return "mp3";
if (IsBytesEqual(FLAC_HEADER, data.slice(0, FLAC_HEADER.length))) return "flac";
if (IsBytesEqual(OGG_HEADER, data.slice(0, OGG_HEADER.length))) return "ogg";
if (IsBytesEqual(M4A_HEADER, data.slice(4, 4 + M4A_HEADER.length))) return "m4a";
if (IsBytesEqual(WMA_HEADER, data.slice(0, WMA_HEADER.length))) return "wma";
if (IsBytesEqual(WAV_HEADER, data.slice(0, WAV_HEADER.length))) return "wav";
return fallbackExt;
}
export async function GetWebImage(pic_url) { export async function GetWebImage(pic_url) {
try { try {

52
src/decrypt/utils.ts Normal file
View File

@ -0,0 +1,52 @@
export const FLAC_HEADER = [0x66, 0x4C, 0x61, 0x43];
export const MP3_HEADER = [0x49, 0x44, 0x33];
export const OGG_HEADER = [0x4F, 0x67, 0x67, 0x53];
export const M4A_HEADER = [0x66, 0x74, 0x79, 0x70];
export const WMA_HEADER = [
0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11,
0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C,
]
export const WAV_HEADER = [0x52, 0x49, 0x46, 0x46]
export const AAC_HEADER = [0xFF, 0xF1]
export function BytesHasPrefix(data: Uint8Array, prefix: number[]): boolean {
if (prefix.length > data.length) return false
return prefix.every((val, idx) => {
return val === data[idx];
})
}
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 {
if (BytesHasPrefix(data, MP3_HEADER)) return ".mp3"
if (BytesHasPrefix(data, FLAC_HEADER)) return ".flac"
if (BytesHasPrefix(data, OGG_HEADER)) return ".ogg"
if (data.length >= 4 + M4A_HEADER.length &&
BytesHasPrefix(data.slice(4), M4A_HEADER)) return ".m4a"
if (BytesHasPrefix(data, WAV_HEADER)) return ".wav"
if (BytesHasPrefix(data, WMA_HEADER)) return ".wma"
if (BytesHasPrefix(data, AAC_HEADER)) return ".aac"
return fallback_ext;
}
export function GetArrayBuffer(obj: Blob): Promise<ArrayBuffer> {
if (!!obj.arrayBuffer) return obj.arrayBuffer()
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
const rs = e.target?.result
if (!rs) {
reject("read file failed")
} else {
resolve(rs as ArrayBuffer)
}
};
reader.readAsArrayBuffer(obj);
});
}

View File

@ -1,6 +1,7 @@
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL, IsBytesEqual} from "./util"; import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util";
import {Decrypt as RawDecrypt} from "./raw"; import {Decrypt as RawDecrypt} from "./raw";
import {BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
const MagicHeader = [0x69, 0x66, 0x6D, 0x74] const MagicHeader = [0x69, 0x66, 0x6D, 0x74]
@ -14,8 +15,7 @@ const FileTypeMap = {
export async function Decrypt(file, raw_filename, raw_ext) { export async function Decrypt(file, raw_filename, raw_ext) {
const oriData = new Uint8Array(await GetArrayBuffer(file)); const oriData = new Uint8Array(await GetArrayBuffer(file));
if (!IsBytesEqual(MagicHeader, oriData.slice(0, 4)) || if (!BytesHasPrefix(oriData, MagicHeader) || !BytesHasPrefix(oriData.slice(8, 12), MagicHeader2)) {
!IsBytesEqual(MagicHeader2, oriData.slice(8, 12))) {
if (raw_ext === "xm") { if (raw_ext === "xm") {
return {status: false, message: "此xm文件已损坏"} return {status: false, message: "此xm文件已损坏"}
} else { } else {

View File

@ -41,7 +41,6 @@ Vue.use(Progress);
Vue.prototype.$notify = Notification; Vue.prototype.$notify = Notification;
Vue.config.productionTip = false; Vue.config.productionTip = false;
document.getElementById("loader-source").remove()
new Vue({ new Vue({
render: h => h(App), render: h => h(App),
}).$mount('#app'); }).$mount('#app');

17
src/shims-tsx.d.ts vendored Normal file
View File

@ -0,0 +1,17 @@
import Vue, {VNode} from 'vue'
declare global {
namespace JSX {
// tslint:disable no-empty-interface
interface Element extends VNode {
}
// tslint:disable no-empty-interface
interface ElementClass extends Vue {
}
interface IntrinsicElements {
[elem: string]: any
}
}
}

4
src/shims-vue.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
declare module '*.vue' {
import Vue from 'vue'
export default Vue
}

41
tsconfig.json Normal file
View File

@ -0,0 +1,41 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}