From 7f17f93767ea646837026c43ff29f831e2b29279 Mon Sep 17 00:00:00 2001 From: SiriusXT <1160925501@qq.com> Date: Fri, 1 Nov 2024 21:43:09 +0800 Subject: [PATCH 1/2] Crop fileName and prevent cutting into the extension. --- src/services/export/zip.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/services/export/zip.ts b/src/services/export/zip.ts index c98c226c8..5e30e6297 100644 --- a/src/services/export/zip.ts +++ b/src/services/export/zip.ts @@ -58,8 +58,15 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h function getDataFileName(type: string | null, mime: string, baseFileName: string, existingFileNames: Record): string { let fileName = baseFileName.trim(); + + // Crop fileName to avoid its length exceeding 30 and prevent cutting into the extension. if (fileName.length > 30) { - fileName = fileName.substr(0, 30).trim(); + let match = fileName.match(/(\.[a-zA-Z0-9_.!#-]+)$/); + let ext = match ? match[0] : ''; + // Crop the extension if extension length exceeds 30 + const croppedExt = ext.slice(-30); + // Crop the file name section and append the cropped extension + fileName = fileName.slice(0, 30 - croppedExt.length) + croppedExt; } let existingExtension = path.extname(fileName).toLowerCase(); @@ -76,6 +83,9 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h else if (mime === 'application/x-javascript' || mime === 'text/javascript') { newExtension = 'js'; } + else if (type === 'canvas' || mime === 'application/json') { + newExtension = 'json'; + } else if (existingExtension.length > 0) { // if the page already has an extension, then we'll just keep it newExtension = null; } From c2baa4b752e4ce7189f081a8cbe0a832bb1f880d Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 1 Nov 2024 19:43:39 +0200 Subject: [PATCH 2/2] server: Add comment to clarify use of regex --- src/services/export/zip.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/export/zip.ts b/src/services/export/zip.ts index 5e30e6297..75feeebc6 100644 --- a/src/services/export/zip.ts +++ b/src/services/export/zip.ts @@ -61,6 +61,7 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h // Crop fileName to avoid its length exceeding 30 and prevent cutting into the extension. if (fileName.length > 30) { + // We use regex to match the extension to preserve multiple dots in extensions (e.g. .tar.gz). let match = fileName.match(/(\.[a-zA-Z0-9_.!#-]+)$/); let ext = match ? match[0] : ''; // Crop the extension if extension length exceeds 30