2019-09-07 17:50:04 +00:00
|
|
|
const musicMetadata = require("music-metadata-browser");
|
2020-01-21 11:03:41 +00:00
|
|
|
const util = require("./util");
|
2020-02-10 16:34:26 +00:00
|
|
|
import * as mask from "./qmcmask"
|
2019-07-05 07:05:11 +00:00
|
|
|
|
2020-01-27 04:50:24 +00:00
|
|
|
const OriginalExtMap = {
|
|
|
|
"qmc0": "mp3",
|
|
|
|
"qmc3": "mp3",
|
|
|
|
"qmcogg": "ogg",
|
|
|
|
"qmcflac": "flac",
|
|
|
|
"bkcmp3": "mp3",
|
2020-01-27 09:36:20 +00:00
|
|
|
"bkcflac": "flac",
|
|
|
|
"tkm": "m4a"
|
2020-01-27 04:50:24 +00:00
|
|
|
};
|
2020-01-21 11:03:41 +00:00
|
|
|
|
2020-02-10 16:34:26 +00:00
|
|
|
//todo: use header to detect media type
|
|
|
|
export async function Decrypt(file, raw_filename, raw_ext) {
|
2019-07-05 07:05:11 +00:00
|
|
|
// 获取扩展名
|
2020-01-27 04:50:24 +00:00
|
|
|
if (!(raw_ext in OriginalExtMap)) {
|
|
|
|
return {status: false, message: "File type is incorrect!"}
|
2019-07-05 07:05:11 +00:00
|
|
|
}
|
2020-02-06 08:18:40 +00:00
|
|
|
const new_ext = OriginalExtMap[raw_ext];
|
2020-01-21 11:03:41 +00:00
|
|
|
const mime = util.AudioMimeType[new_ext];
|
2019-07-05 07:05:11 +00:00
|
|
|
// 读取文件
|
2020-01-21 11:03:41 +00:00
|
|
|
const fileBuffer = await util.GetArrayBuffer(file);
|
2019-07-05 07:05:11 +00:00
|
|
|
const audioData = new Uint8Array(fileBuffer);
|
|
|
|
// 转换数据
|
2020-02-10 16:34:26 +00:00
|
|
|
const seed = mask.QmcMaskGetDefault();
|
|
|
|
const dec = seed.Decrypt(audioData);
|
2019-07-05 07:05:11 +00:00
|
|
|
// 导出
|
2020-02-10 16:34:26 +00:00
|
|
|
const musicData = new Blob([dec], {type: mime});
|
2019-07-05 07:05:11 +00:00
|
|
|
// 读取Meta
|
2020-02-06 08:18:40 +00:00
|
|
|
const tag = await musicMetadata.parseBlob(musicData);
|
|
|
|
const info = util.GetFileInfo(tag.common.artist, tag.common.title, raw_filename);
|
2019-07-05 07:05:11 +00:00
|
|
|
|
|
|
|
// 返回
|
|
|
|
return {
|
2020-01-21 11:03:41 +00:00
|
|
|
status: true,
|
|
|
|
title: info.title,
|
|
|
|
artist: info.artist,
|
2020-02-04 10:24:53 +00:00
|
|
|
ext: new_ext,
|
2019-09-08 05:40:32 +00:00
|
|
|
album: tag.common.album,
|
2020-02-06 08:18:40 +00:00
|
|
|
picture: util.GetCoverURL(tag),
|
|
|
|
file: URL.createObjectURL(musicData),
|
2019-07-05 07:05:11 +00:00
|
|
|
mime: mime
|
|
|
|
}
|
|
|
|
}
|