PLEC Update

- Append timestamp to avoid filename conflicts
- Start on workspace/dist/ folder or workspace/
This commit is contained in:
2026-05-08 10:36:44 +08:00
parent b87ba579a5
commit e42002f5de
3 changed files with 31 additions and 28 deletions

View File

@@ -2,7 +2,7 @@
"name": "hpl-toolbox", "name": "hpl-toolbox",
"displayName": "HPL Toolbox", "displayName": "HPL Toolbox",
"description": "Bundled tools: PLEC Upload, AppLovin Demo Upload, Base64 Asset Scanner, Daily Update.", "description": "Bundled tools: PLEC Upload, AppLovin Demo Upload, Base64 Asset Scanner, Daily Update.",
"version": "0.1.0", "version": "0.1.1",
"publisher": "hesukastro", "publisher": "hesukastro",
"engines": { "engines": {
"vscode": "^1.85.0" "vscode": "^1.85.0"

View File

@@ -20,6 +20,7 @@ export function openPlecUpload(_context: vscode.ExtensionContext) {
canSelectMany: false, canSelectMany: false,
filters: { HTML: ['html', 'htm'] }, filters: { HTML: ['html', 'htm'] },
openLabel: 'Select', openLabel: 'Select',
defaultUri: getPickerDefaultUri(),
}); });
if (picked && picked[0]) { if (picked && picked[0]) {
const fp = picked[0].fsPath; const fp = picked[0].fsPath;
@@ -53,8 +54,6 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
const cfg = vscode.workspace.getConfiguration('hplToolbox.plec'); const cfg = vscode.workspace.getConfiguration('hplToolbox.plec');
const uploadUrl = cfg.get<string>('uploadUrl')!; const uploadUrl = cfg.get<string>('uploadUrl')!;
const originUrl = cfg.get<string>('originUrl')!; const originUrl = cfg.get<string>('originUrl')!;
const previewBase = cfg.get<string>('previewBase')!;
const skipExistsCheck = cfg.get<boolean>('skipExistsCheck')!;
const valid: UploadRow[] = []; const valid: UploadRow[] = [];
for (const r of rows) { for (const r of rows) {
@@ -77,22 +76,8 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
return; return;
} }
panel.webview.postMessage({ type: 'status', message: 'Checking filename availability...' }); const stamp = timestampSuffix();
const stampedNames = valid.map((r) => appendTimestamp(path.basename(r.path), stamp));
if (!skipExistsCheck) {
for (const r of valid) {
const fname = path.basename(r.path);
const exists = await checkFileExists(previewBase + encodeURIComponent(fname));
if (exists === true) {
panel.webview.postMessage({
type: 'rowError',
rowId: r.id,
message: `Filename "${fname}" already exists on preview server. Rename and retry.`,
});
return;
}
}
}
panel.webview.postMessage({ type: 'status', message: 'Uploading...' }); panel.webview.postMessage({ type: 'status', message: 'Uploading...' });
@@ -101,7 +86,7 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
const r = valid[i]; const r = valid[i];
const idx = i + 1; const idx = i + 1;
const buf = fs.readFileSync(r.path); const buf = fs.readFileSync(r.path);
const file = new File([buf], path.basename(r.path), { type: 'text/html' }); const file = new File([buf], stampedNames[i], { type: 'text/html' });
form.append(`fileUpload_${idx}`, file as any); form.append(`fileUpload_${idx}`, file as any);
form.append(`iterationNameUpload_${idx}`, encodeURI(r.iteration.replace(/\s+/g, ''))); form.append(`iterationNameUpload_${idx}`, encodeURI(r.iteration.replace(/\s+/g, '')));
} }
@@ -147,15 +132,33 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
panel.webview.postMessage({ type: 'result', preview, raw: json, rawText: text }); panel.webview.postMessage({ type: 'result', preview, raw: json, rawText: text });
} }
async function checkFileExists(url: string): Promise<boolean | null> { function timestampSuffix(d: Date = new Date()): string {
try { const pad = (n: number, w = 2) => String(n).padStart(w, '0');
const res = await fetch(url, { method: 'HEAD' }); return (
if (res.status === 200) return true; d.getFullYear().toString() +
if (res.status === 404) return false; pad(d.getMonth() + 1) +
return null; pad(d.getDate()) +
} catch { pad(d.getHours()) +
return null; pad(d.getMinutes()) +
pad(d.getSeconds())
);
} }
function appendTimestamp(filename: string, stamp: string): string {
const ext = path.extname(filename);
const base = ext ? filename.slice(0, -ext.length) : filename;
return `${base}_${stamp}${ext}`;
}
function getPickerDefaultUri(): vscode.Uri | undefined {
const folders = vscode.workspace.workspaceFolders;
if (!folders || folders.length === 0) return undefined;
const root = folders[0].uri.fsPath;
const dist = path.join(root, 'dist');
if (fs.existsSync(dist) && fs.statSync(dist).isDirectory()) {
return vscode.Uri.file(dist);
}
return vscode.Uri.file(root);
} }
function getHtml(): string { function getHtml(): string {