diff --git a/lib/object-preview.ts b/lib/object-preview.ts index ebcc165d..9bbf0ad9 100644 --- a/lib/object-preview.ts +++ b/lib/object-preview.ts @@ -17,13 +17,15 @@ const SAFE_TEXT_MIMES = [ "application/jsonl", "application/ndjson", "application/x-ndjson", + "application/sql", "application/xml", "text/plain", + "text/x-sql", "text/xml", "text/csv", "text/markdown", ] -const SAFE_TEXT_EXTENSIONS = [".txt", ".json", ".jsonl", ".ndjson", ".xml", ".csv", ".md", ".yml", ".yaml"] +const SAFE_TEXT_EXTENSIONS = [".txt", ".json", ".jsonl", ".ndjson", ".sql", ".xml", ".csv", ".md", ".yml", ".yaml"] const GENERIC_CONTENT_TYPES = ["", "application/octet-stream", "binary/octet-stream"] interface ObjectPreviewOptions { diff --git a/lib/upload-content-type.js b/lib/upload-content-type.js index aa376fea..ac5d59c4 100644 --- a/lib/upload-content-type.js +++ b/lib/upload-content-type.js @@ -44,6 +44,7 @@ function isTextualContentType(contentType) { mime.startsWith("text/") || mime === "application/json" || mime === "application/ld+json" || + mime === "application/sql" || mime === "application/xml" || mime.endsWith("+xml") || mime === "application/javascript" || diff --git a/lib/upload-content-type.ts b/lib/upload-content-type.ts index 647ebc6a..11608c71 100644 --- a/lib/upload-content-type.ts +++ b/lib/upload-content-type.ts @@ -44,6 +44,7 @@ function isTextualContentType(contentType: string): boolean { mime.startsWith("text/") || mime === "application/json" || mime === "application/ld+json" || + mime === "application/sql" || mime === "application/xml" || mime.endsWith("+xml") || mime === "application/javascript" || diff --git a/tests/lib/object-preview.test.ts b/tests/lib/object-preview.test.ts index ebb1354f..880b6992 100644 --- a/tests/lib/object-preview.test.ts +++ b/tests/lib/object-preview.test.ts @@ -86,6 +86,13 @@ test("existing text MIME and extension previews retain their size limit", () => assert.equal(isSafeTextPreview("text/plain", "fred", MAX_TEXT_PREVIEW_BYTES + 1), false) }) +test("SQL MIME types and extensions use the bounded text preview", () => { + assert.equal(isSafeTextPreview("application/sql", "schema.sql", 20), true) + assert.equal(isSafeTextPreview("text/x-sql", "schema.sql", 20), true) + assert.equal(isSafeTextPreview("application/octet-stream", "schema.sql", 20), true) + assert.equal(getObjectPreviewMode({ ...previewOptions, contentType: "application/sql", canRenderText: true }), "text") +}) + test("extensionless UTF-8 text is decoded completely, including non-ASCII content", async () => { const text = "this is a text file\n中文日志\t🙂\r\n" assert.equal(await readObjectPreviewText(new Response(text)), text) diff --git a/tests/lib/upload-content-type.test.js b/tests/lib/upload-content-type.test.js index 3916b7d7..becee035 100644 --- a/tests/lib/upload-content-type.test.js +++ b/tests/lib/upload-content-type.test.js @@ -65,6 +65,12 @@ test("getUploadContentType infers markdown MIME type and charset when browser ty assert.equal(await getUploadContentType(file, "readme.md"), "text/markdown; charset=utf-8") }) +test("getUploadContentType adds utf-8 charset for SQL uploads", async () => { + const file = uploadBlob(["SELECT 1;"], "schema.sql") + + assert.equal(await getUploadContentType(file, "schema.sql"), "application/sql; charset=utf-8") +}) + test("getUploadContentType falls back to octet-stream for unknown empty MIME types", async () => { const file = uploadBlob(["content"], "archive.unknownext")