refactor(typescript): utils.GetArrayBuffer

This commit is contained in:
Emmm Monster 2021-05-23 21:59:33 +08:00
parent f3f6f9ef40
commit ec711990a1
No known key found for this signature in database
GPG Key ID: C98279C83FB50DB9
10 changed files with 30 additions and 43 deletions

View File

@ -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 = [

View File

@ -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 = [

View File

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

View File

@ -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');

View File

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

View File

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

View File

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

View File

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

View File

@ -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<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,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]