feat: add user prefix to file exporter

main
spacemeowx2 2022-10-22 18:40:20 +08:00
parent a5120fbfef
commit 1f16903aa1
2 changed files with 27 additions and 10 deletions

View File

@ -1,6 +1,7 @@
import { BattleExporter, VsBattle } from "../types.ts"; import { BattleExporter, VsBattle } from "../types.ts";
import { base64, path } from "../../deps.ts"; import { path } from "../../deps.ts";
import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts"; import { NSOAPP_VERSION, S3SI_VERSION } from "../constant.ts";
import { parseVsHistoryDetailId } from "../utils.ts";
export type FileExporterType = { export type FileExporterType = {
type: "VS" | "COOP"; type: "VS" | "COOP";
@ -30,16 +31,9 @@ export class FileExporter implements BattleExporter<VsBattle> {
constructor(private exportPath: string) { constructor(private exportPath: string) {
} }
getFilenameById(id: string) { getFilenameById(id: string) {
const fullId = base64.decode(id); const { uid, timestamp } = parseVsHistoryDetailId(id);
const ts = new TextDecoder().decode(
fullId.slice(fullId.length - 52, fullId.length - 37),
);
if (!/\d{8}T\d{6}/.test(ts)) { return `${uid}_${timestamp}Z.json`;
throw new Error("Invalid battle ID");
}
return `${ts}Z.json`;
} }
async exportBattle(battle: VsBattle) { async exportBattle(battle: VsBattle) {
await Deno.mkdir(this.exportPath, { recursive: true }); await Deno.mkdir(this.exportPath, { recursive: true });

View File

@ -82,6 +82,11 @@ export async function showError(p: Promise<void>) {
} }
} }
/**
* @param id id of VeVsHistoryDetail
* @param namespace uuid namespace
* @returns
*/
export function battleId( export function battleId(
id: string, id: string,
namespace = S3SI_NAMESPACE, namespace = S3SI_NAMESPACE,
@ -90,3 +95,21 @@ export function battleId(
const tsUuid = fullId.slice(fullId.length - 52, fullId.length); const tsUuid = fullId.slice(fullId.length - 52, fullId.length);
return uuid.v5.generate(namespace, tsUuid); return uuid.v5.generate(namespace, tsUuid);
} }
export function parseVsHistoryDetailId(id: string) {
const plainText = new TextDecoder().decode(base64.decode(id));
const re = /VsHistoryDetail-([a-z0-9-]+):(\w+):(\d{8}T\d{6})_([0-9a-f-]{36})/;
if (!re.test(plainText)) {
throw new Error(`Invalid battle ID: ${plainText}`);
}
const [, uid, listType, timestamp, uuid] = plainText.match(re)!;
return {
uid,
listType,
timestamp,
uuid,
};
}