ASP*_*iRE 5 javascript mime-types typescript deno
为了便于可视化,下面是记录查找表。
我似乎在网上找不到任何地方可以告诉您其中哪些应该还包含charset=utf-8.
我应该假设它与文本类似吗?
看一看:
const MEDIA_TYPES: Record<string, string> = {
".md": "text/markdown",
".html": "text/html",
".htm": "text/html",
".json": "application/json",
".map": "application/json",
".txt": "text/plain",
".ts": "text/typescript",
".tsx": "text/tsx",
".js": "application/javascript",
".jsx": "text/jsx",
".gz": "application/gzip",
".css": "text/css",
".wasm": "application/wasm",
".mjs": "application/javascript",
".otf": "font/otf",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
".conf": "text/plain",
".list": "text/plain",
".log": "text/plain",
".ini": "text/plain",
".vtt": "text/vtt",
".yaml": "text/yaml",
".yml": "text/yaml",
".mid": "audio/midi",
".midi": "audio/midi",
".mp3": "audio/mp3",
".mp4a": "audio/mp4",
".m4a": "audio/mp4",
".ogg": "audio/ogg",
".spx": "audio/ogg",
".opus": "audio/ogg",
".wav": "audio/wav",
".webm": "audio/webm",
".aac": "audio/x-aac",
".flac": "audio/x-flac",
".mp4": "video/mp4",
".mp4v": "video/mp4",
".mkv": "video/x-matroska",
".mov": "video/quicktime",
".svg": "image/svg+xml",
".avif": "image/avif",
".bmp": "image/bmp",
".gif": "image/gif",
".heic": "image/heic",
".heif": "image/heif",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".tiff": "image/tiff",
".psd": "image/vnd.adobe.photoshop",
".ico": "image/vnd.microsoft.icon",
".webp": "image/webp",
".es": "application/ecmascript",
".epub": "application/epub+zip",
".jar": "application/java-archive",
".war": "application/java-archive",
".webmanifest": "application/manifest+json",
".doc": "application/msword",
".dot": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".dotx": "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
".cjs": "application/node",
".bin": "application/octet-stream",
".pkg": "application/octet-stream",
".dump": "application/octet-stream",
".exe": "application/octet-stream",
".deploy": "application/octet-stream",
".img": "application/octet-stream",
".msi": "application/octet-stream",
".pdf": "application/pdf",
".pgp": "application/pgp-encrypted",
".asc": "application/pgp-signature",
".sig": "application/pgp-signature",
".ai": "application/postscript",
".eps": "application/postscript",
".ps": "application/postscript",
".rdf": "application/rdf+xml",
".rss": "application/rss+xml",
".rtf": "application/rtf",
".apk": "application/vnd.android.package-archive",
".key": "application/vnd.apple.keynote",
".numbers": "application/vnd.apple.keynote",
".pages": "application/vnd.apple.pages",
".geo": "application/vnd.dynageo",
".gdoc": "application/vnd.google-apps.document",
".gslides": "application/vnd.google-apps.presentation",
".gsheet": "application/vnd.google-apps.spreadsheet",
".kml": "application/vnd.google-earth.kml+xml",
".mkz": "application/vnd.google-earth.kmz",
".icc": "application/vnd.iccprofile",
".icm": "application/vnd.iccprofile",
".xls": "application/vnd.ms-excel",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".xlm": "application/vnd.ms-excel",
".ppt": "application/vnd.ms-powerpoint",
".pot": "application/vnd.ms-powerpoint",
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".potx": "application/vnd.openxmlformats-officedocument.presentationml.template",
".xps": "application/vnd.ms-xpsdocument",
".odc": "application/vnd.oasis.opendocument.chart",
".odb": "application/vnd.oasis.opendocument.database",
".odf": "application/vnd.oasis.opendocument.formula",
".odg": "application/vnd.oasis.opendocument.graphics",
".odp": "application/vnd.oasis.opendocument.presentation",
".ods": "application/vnd.oasis.opendocument.spreadsheet",
".odt": "application/vnd.oasis.opendocument.text",
".rar": "application/vnd.rar",
".unityweb": "application/vnd.unity",
".dmg": "application/x-apple-diskimage",
".bz": "application/x-bzip",
".crx": "application/x-chrome-extension",
".deb": "application/x-debian-package",
".php": "application/x-httpd-php",
".iso": "application/x-iso9660-image",
".sh": "application/x-sh",
".sql": "application/x-sql",
".srt": "application/x-subrip",
".xml": "application/xml",
".zip": "application/zip",
};
/** Returns the content-type based on the extension of a path. */
function contentType(pathname: string): string | undefined {
return MEDIA_TYPES[pathname];
}
console.log(contentType("/dashboard/v1/index.html"));
console.log(contentType("/dashboard/v1/logo.svg"));
Run Code Online (Sandbox Code Playgroud)
MDN 说:
例如,对于任何主要类型为文本的 MIME 类型,您可以添加可选的 charset 参数来指定数据中的字符所使用的字符集。如果未指定字符集,则默认值为 ASCII (US-ASCII),除非被用户代理的设置覆盖。要指定 UTF-8 文本文件,请使用 MIME 类型 text/plain;charset=UTF-8。
因此,对于任何基于text/...您可以选择添加字符集的内容。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#struction_of_a_mime_type
以下contentType()功能更新演示了一种解决方案。
/** Returns the content-type based on the extension of a path. */
function contentType(ext: string): string | undefined {
const charsetUtf8 = "; charset=UTF-8";
// Content types that do not start with text/.. but usually contain charset=utf-8
const specialCase = [
"application/json",
"application/xml",
"application/rss+xml",
];
let outputContentType = MEDIA_TYPES[ext];
// Return undefined.
if(!outputContentType) return;
if(outputContentType.startsWith("text/") || specialCase.includes(outputContentType)) {
// Combine Content-Type with charset=utf-8
outputContentType = outputContentType + charsetUtf8;
// Return combined.
return outputContentType;
} else {
// Return for any other types or undefined.
return outputContentType;
}
}
Run Code Online (Sandbox Code Playgroud)
然后理论上,您可以返回422: Unprocessable EntityMIME 类型列表中不存在的任何内容类型。
let requestedFilePath = path.join("./", context.main.http.server.root, urlPathNameParsed.dir, urlPathNameParsed.base);
try {
// try as file...
const file = await Deno.readFile(requestedFilePath);
// If file extension is not on media list...
if(typeof(contentType(urlPathNameParsed.ext)) === 'undefined') {
// Set Content-Type header for this response
responseHeaders.set('Content-Type', 'text/plain; charset=UTF-8');
await e.respondWith(
new Response("HTTP 422: Unprocessable Entity", {
headers: responseHeaders,
status: 422,
statusText: 'Unprocessable Entity',
})
);
} else {
// Set Content-Type header for this response
responseHeaders.set('Content-Type', `${contentType(urlPathNameParsed.ext)}`);
// Build and send the response
await e.respondWith(
new Response(file, {
headers: responseHeaders,
status: 200,
statusText: 'OK'
})
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4900 次 |
| 最近记录: |