2021-05-24 14:19:37 +00:00
|
|
|
import {DecryptResult} from "@/decrypt/entity";
|
|
|
|
|
2021-05-24 15:48:52 +00:00
|
|
|
export enum FilenamePolicy {
|
|
|
|
ArtistAndTitle,
|
|
|
|
TitleOnly,
|
|
|
|
TitleAndArtist,
|
|
|
|
SameAsOriginal,
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FilenamePolicies: { key: FilenamePolicy, text: string }[] = [
|
|
|
|
{key: FilenamePolicy.ArtistAndTitle, text: "歌手-歌曲名"},
|
|
|
|
{key: FilenamePolicy.TitleOnly, text: "歌曲名"},
|
|
|
|
{key: FilenamePolicy.TitleAndArtist, text: "歌曲名-歌手"},
|
|
|
|
{key: FilenamePolicy.SameAsOriginal, text: "同源文件名"},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
export function DownloadBlobMusic(data: DecryptResult, policy: FilenamePolicy) {
|
2020-02-06 08:01:35 +00:00
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = data.file;
|
2021-05-24 15:48:52 +00:00
|
|
|
switch (policy) {
|
2020-02-23 05:45:30 +00:00
|
|
|
default:
|
2021-05-24 15:48:52 +00:00
|
|
|
case FilenamePolicy.ArtistAndTitle:
|
2020-02-23 05:45:30 +00:00
|
|
|
a.download = data.artist + " - " + data.title + "." + data.ext;
|
2020-02-06 08:01:35 +00:00
|
|
|
break;
|
2021-05-24 15:48:52 +00:00
|
|
|
case FilenamePolicy.TitleOnly:
|
2020-02-23 05:45:30 +00:00
|
|
|
a.download = data.title + "." + data.ext;
|
2020-02-06 08:01:35 +00:00
|
|
|
break;
|
2021-05-24 15:48:52 +00:00
|
|
|
case FilenamePolicy.TitleAndArtist:
|
2020-02-06 08:01:35 +00:00
|
|
|
a.download = data.title + " - " + data.artist + "." + data.ext;
|
|
|
|
break;
|
2021-05-24 15:48:52 +00:00
|
|
|
case FilenamePolicy.SameAsOriginal:
|
2020-02-23 05:45:30 +00:00
|
|
|
a.download = data.rawFilename + "." + data.ext;
|
|
|
|
break;
|
2020-02-06 08:01:35 +00:00
|
|
|
}
|
|
|
|
document.body.append(a);
|
|
|
|
a.click();
|
|
|
|
a.remove();
|
|
|
|
}
|
|
|
|
|
2021-05-24 14:19:37 +00:00
|
|
|
export function RemoveBlobMusic(data: DecryptResult) {
|
2020-02-06 08:01:35 +00:00
|
|
|
URL.revokeObjectURL(data.file);
|
2021-05-24 14:19:37 +00:00
|
|
|
if (data.picture?.startsWith("blob:")) {
|
2020-04-05 04:28:59 +00:00
|
|
|
URL.revokeObjectURL(data.picture);
|
|
|
|
}
|
2021-05-24 14:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DecryptQueue {
|
|
|
|
private readonly pending: (() => Promise<void>)[];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.pending = []
|
|
|
|
}
|
|
|
|
|
|
|
|
queue(fn: () => Promise<void>) {
|
|
|
|
this.pending.push(fn)
|
|
|
|
this.consume()
|
|
|
|
}
|
2020-04-05 04:28:59 +00:00
|
|
|
|
2021-05-24 14:19:37 +00:00
|
|
|
private consume() {
|
|
|
|
const fn = this.pending.shift()
|
|
|
|
if (fn) fn().then(() => this.consume).catch(console.error)
|
|
|
|
}
|
2020-02-06 08:01:35 +00:00
|
|
|
}
|