fix: slugify uploaded GPX filename before sending to API
This commit is contained in:
@@ -54,6 +54,14 @@ function formatSize(bytes) {
|
||||
return (bytes / 1024).toFixed(0) + ' KB';
|
||||
}
|
||||
|
||||
function slugifyFilename(filename) {
|
||||
const lastDot = filename.lastIndexOf('.');
|
||||
const name = lastDot > 0 ? filename.slice(0, lastDot) : filename;
|
||||
const ext = lastDot > 0 ? filename.slice(lastDot).toLowerCase() : '';
|
||||
const slug = name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');
|
||||
return slug + ext;
|
||||
}
|
||||
|
||||
function formatDate(iso) {
|
||||
return new Date(iso).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
|
||||
}
|
||||
@@ -131,7 +139,7 @@ function initUpload(formEl) {
|
||||
btn.disabled = true;
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('file', new File([file], slugifyFilename(file.name), { type: file.type }));
|
||||
|
||||
const res = await apiFetch(`${API}/pages${route}/media`, { method: 'POST', body: fd });
|
||||
btn.disabled = false;
|
||||
|
||||
Reference in New Issue
Block a user