refactor(typescript): utils.GetArrayBuffer
This commit is contained in:
parent
f3f6f9ef40
commit
ec711990a1
@ -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 = [
|
||||||
|
@ -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 = [
|
||||||
|
@ -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
|
||||||
|
@ -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');
|
||||||
|
|
||||||
|
@ -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,
|
||||||
|
@ -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;
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -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…
Reference in New Issue
Block a user