From ec711990a19da136a993091e22a621833bac5658 Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sun, 23 May 2021 21:59:33 +0800 Subject: [PATCH] refactor(typescript): utils.GetArrayBuffer --- src/decrypt/kgm.js | 4 ++-- src/decrypt/kwm.js | 4 ++-- src/decrypt/ncm.js | 3 +-- src/decrypt/qmc.js | 3 +-- src/decrypt/qmcMask.js | 3 +-- src/decrypt/raw.js | 4 ++-- src/decrypt/tm.js | 5 ++--- src/decrypt/util.js | 27 +-------------------------- src/decrypt/utils.ts | 16 ++++++++++++++++ src/decrypt/xm.js | 4 ++-- 10 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/decrypt/kgm.js b/src/decrypt/kgm.js index 8e77a2c..d2c1d9d 100644 --- a/src/decrypt/kgm.js +++ b/src/decrypt/kgm.js @@ -1,5 +1,5 @@ -import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; -import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; +import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const VprHeader = [ diff --git a/src/decrypt/kwm.js b/src/decrypt/kwm.js index 74f1a18..793a81e 100644 --- a/src/decrypt/kwm.js +++ b/src/decrypt/kwm.js @@ -1,5 +1,5 @@ -import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; -import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; +import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; +import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [ diff --git a/src/decrypt/ncm.js b/src/decrypt/ncm.js index 57d61f0..1680861 100644 --- a/src/decrypt/ncm.js +++ b/src/decrypt/ncm.js @@ -1,4 +1,4 @@ -import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; +import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const CryptoJS = require("crypto-js"); const MetaFlac = require('metaflac-js'); @@ -10,7 +10,6 @@ import jimp from 'jimp'; import { AudioMimeType, - GetArrayBuffer, GetFileInfo, GetWebImage, WriteMp3Meta diff --git a/src/decrypt/qmc.js b/src/decrypt/qmc.js index 27f670e..fdf42b9 100644 --- a/src/decrypt/qmc.js +++ b/src/decrypt/qmc.js @@ -1,6 +1,5 @@ import { AudioMimeType, - GetArrayBuffer, GetFileInfo, GetMetaCoverURL, GetWebImage, @@ -9,7 +8,7 @@ import { } from "./util"; import {QmcMaskCreate58, QmcMaskDetectMflac, QmcMaskDetectMgg, QmcMaskGetDefault} from "./qmcMask"; import {fromByteArray as Base64Encode, toByteArray as Base64Decode} from 'base64-js' -import {SniffAudioExt} from "@/decrypt/utils.ts"; +import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const MetaFlac = require('metaflac-js'); diff --git a/src/decrypt/qmcMask.js b/src/decrypt/qmcMask.js index 6b1a312..16cd14b 100644 --- a/src/decrypt/qmcMask.js +++ b/src/decrypt/qmcMask.js @@ -1,5 +1,4 @@ -import {FLAC_HEADER, OGG_HEADER} from "./util" -import {BytesEquals, BytesHasPrefix} from "@/decrypt/utils.ts"; +import {BytesEquals, BytesHasPrefix, FLAC_HEADER, OGG_HEADER} from "@/decrypt/utils.ts"; const QMOggPublicHeader1 = [ 0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, diff --git a/src/decrypt/raw.js b/src/decrypt/raw.js index e34d3c4..5a0872f 100644 --- a/src/decrypt/raw.js +++ b/src/decrypt/raw.js @@ -1,7 +1,7 @@ -import {SniffAudioExt} from "@/decrypt/utils.ts"; +import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); -import {AudioMimeType, GetArrayBuffer, GetMetaCoverURL, GetFileInfo} from "./util"; +import {AudioMimeType, GetMetaCoverURL, GetFileInfo} from "./util"; export async function Decrypt(file, raw_filename, raw_ext, detect = true) { let ext = raw_ext; diff --git a/src/decrypt/tm.js b/src/decrypt/tm.js index 172db5f..63b1ca2 100644 --- a/src/decrypt/tm.js +++ b/src/decrypt/tm.js @@ -1,11 +1,10 @@ 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]; export async function Decrypt(file, raw_filename) { - const fileBuffer = await GetArrayBuffer(file); - const audioData = new Uint8Array(fileBuffer); + 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 302a6d2..50cdec6 100644 --- a/src/decrypt/util.js +++ b/src/decrypt/util.js @@ -1,13 +1,5 @@ const ID3Writer = require("browser-id3-writer"); -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 = { mp3: "audio/mpeg", flac: "audio/flac", @@ -18,16 +10,6 @@ export const AudioMimeType = { }; 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 = "-") { let newArtist = "", newTitle = ""; @@ -56,13 +38,6 @@ export function GetMetaCoverURL(metadata) { 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]; - }) -} - export async function GetWebImage(pic_url) { try { diff --git a/src/decrypt/utils.ts b/src/decrypt/utils.ts index 39c816f..d76c530 100644 --- a/src/decrypt/utils.ts +++ b/src/decrypt/utils.ts @@ -34,3 +34,19 @@ export function SniffAudioExt(data: Uint8Array, fallback_ext: string = "mp3"): s if (BytesHasPrefix(data, AAC_HEADER)) return ".aac" return fallback_ext; } + +export function GetArrayBuffer(obj: Blob): Promise { + 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); + }); +} diff --git a/src/decrypt/xm.js b/src/decrypt/xm.js index 740938a..279e8a6 100644 --- a/src/decrypt/xm.js +++ b/src/decrypt/xm.js @@ -1,7 +1,7 @@ -import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; +import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util"; import {Decrypt as RawDecrypt} from "./raw"; -import {BytesHasPrefix} from "@/decrypt/utils.ts"; +import {BytesHasPrefix, GetArrayBuffer} from "@/decrypt/utils.ts"; const musicMetadata = require("music-metadata-browser"); const MagicHeader = [0x69, 0x66, 0x6D, 0x74]