PLEC Update
- Append timestamp to avoid filename conflicts - Start on workspace/dist/ folder or workspace/
This commit is contained in:
@@ -20,6 +20,7 @@ export function openPlecUpload(_context: vscode.ExtensionContext) {
|
||||
canSelectMany: false,
|
||||
filters: { HTML: ['html', 'htm'] },
|
||||
openLabel: 'Select',
|
||||
defaultUri: getPickerDefaultUri(),
|
||||
});
|
||||
if (picked && picked[0]) {
|
||||
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 uploadUrl = cfg.get<string>('uploadUrl')!;
|
||||
const originUrl = cfg.get<string>('originUrl')!;
|
||||
const previewBase = cfg.get<string>('previewBase')!;
|
||||
const skipExistsCheck = cfg.get<boolean>('skipExistsCheck')!;
|
||||
|
||||
const valid: UploadRow[] = [];
|
||||
for (const r of rows) {
|
||||
@@ -77,22 +76,8 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
|
||||
return;
|
||||
}
|
||||
|
||||
panel.webview.postMessage({ type: 'status', message: 'Checking filename availability...' });
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
const stamp = timestampSuffix();
|
||||
const stampedNames = valid.map((r) => appendTimestamp(path.basename(r.path), stamp));
|
||||
|
||||
panel.webview.postMessage({ type: 'status', message: 'Uploading...' });
|
||||
|
||||
@@ -101,7 +86,7 @@ async function handleUpload(panel: vscode.WebviewPanel, rows: UploadRow[]) {
|
||||
const r = valid[i];
|
||||
const idx = i + 1;
|
||||
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(`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 });
|
||||
}
|
||||
|
||||
async function checkFileExists(url: string): Promise<boolean | null> {
|
||||
try {
|
||||
const res = await fetch(url, { method: 'HEAD' });
|
||||
if (res.status === 200) return true;
|
||||
if (res.status === 404) return false;
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
function timestampSuffix(d: Date = new Date()): string {
|
||||
const pad = (n: number, w = 2) => String(n).padStart(w, '0');
|
||||
return (
|
||||
d.getFullYear().toString() +
|
||||
pad(d.getMonth() + 1) +
|
||||
pad(d.getDate()) +
|
||||
pad(d.getHours()) +
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user