Browse Source

refactor(typescript): utils.GetArrayBuffer

20230320
Emmm Monster 4 years ago
parent
commit
ec711990a1
No known key found for this signature in database GPG Key ID: C98279C83FB50DB9
  1. 4
      src/decrypt/kgm.js
  2. 4
      src/decrypt/kwm.js
  3. 3
      src/decrypt/ncm.js
  4. 3
      src/decrypt/qmc.js
  5. 3
      src/decrypt/qmcMask.js
  6. 4
      src/decrypt/raw.js
  7. 5
      src/decrypt/tm.js
  8. 27
      src/decrypt/util.js
  9. 16
      src/decrypt/utils.ts
  10. 4
      src/decrypt/xm.js

4
src/decrypt/kgm.js

@ -1,5 +1,5 @@
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util";
import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
const VprHeader = [ const VprHeader = [

4
src/decrypt/kwm.js

@ -1,5 +1,5 @@
import {AudioMimeType, GetArrayBuffer, GetFileInfo, GetMetaCoverURL} from "./util"; import {AudioMimeType, GetFileInfo, GetMetaCoverURL} from "./util";
import {BytesHasPrefix, SniffAudioExt} from "@/decrypt/utils.ts"; import {BytesHasPrefix, GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const musicMetadata = require("music-metadata-browser"); const musicMetadata = require("music-metadata-browser");
const MagicHeader = [ const MagicHeader = [

3
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 CryptoJS = require("crypto-js");
const MetaFlac = require('metaflac-js'); const MetaFlac = require('metaflac-js');
@ -10,7 +10,6 @@ import jimp from 'jimp';
import { import {
AudioMimeType, AudioMimeType,
GetArrayBuffer,
GetFileInfo, GetFileInfo,
GetWebImage, GetWebImage,
WriteMp3Meta WriteMp3Meta

3
src/decrypt/qmc.js

@ -1,6 +1,5 @@
import { import {
AudioMimeType, AudioMimeType,
GetArrayBuffer,
GetFileInfo, GetFileInfo,
GetMetaCoverURL, GetMetaCoverURL,
GetWebImage, GetWebImage,
@ -9,7 +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 {SniffAudioExt} from "@/decrypt/utils.ts"; import {GetArrayBuffer, SniffAudioExt} from "@/decrypt/utils.ts";
const MetaFlac = require('metaflac-js'); const MetaFlac = require('metaflac-js');

3
src/decrypt/qmcMask.js

@ -1,5 +1,4 @@
import {FLAC_HEADER, OGG_HEADER} from "./util" import {BytesEquals, BytesHasPrefix, FLAC_HEADER, OGG_HEADER} from "@/decrypt/utils.ts";
import {BytesEquals, BytesHasPrefix} 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,

4
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"); 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) { export async function Decrypt(file, raw_filename, raw_ext, detect = true) {
let ext = raw_ext; let ext = raw_ext;

5
src/decrypt/tm.js

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

27
src/decrypt/util.js

@ -1,13 +1,5 @@
const ID3Writer = require("browser-id3-writer"); 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 = { export const AudioMimeType = {
mp3: "audio/mpeg", mp3: "audio/mpeg",
flac: "audio/flac", flac: "audio/flac",
@ -18,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 = "";
@ -56,13 +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];
})
}
export async function GetWebImage(pic_url) { export async function GetWebImage(pic_url) {
try { try {

16
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" if (BytesHasPrefix(data, AAC_HEADER)) return ".aac"
return fallback_ext; 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);
});
}

4
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 {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 musicMetadata = require("music-metadata-browser");
const MagicHeader = [0x69, 0x66, 0x6D, 0x74] const MagicHeader = [0x69, 0x66, 0x6D, 0x74]

Loading…
Cancel
Save