Skip to content

Commit

Permalink
Add shareable extension-to-MIME-type mapping (#1355)
Browse files Browse the repository at this point in the history
## What is this PR doing?

This PR switches from a hard-coded MIME type list to a JSON based
extention-to-MIME-type mapping.

## What problem is it solving?

PHPRequestHandler and the WP Cloud PHP request handler use different
MIME type mappings.

## How is the problem addressed?

This PR adds a JSON file that can be used by both. Both lists contained
file extensions the other did not, so the new mapping combines both.

GitHub co-pilot was handy for:
- quickly converting the existing MIME type switch statement to JSON
- sorting and removing duplicate keys from the combined mapping in JSON

The WP Cloud PHP request handler will be updated to use the mapping JSON
in a separate PR.

## Testing Instructions

- Smoke test Playground locally
- Make sure e2e tests pass
  • Loading branch information
brandonpayton authored May 2, 2024
1 parent 784e807 commit 9b2debf
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 71 deletions.
96 changes: 96 additions & 0 deletions packages/php-wasm/universal/src/lib/mime-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"_default": "application-octet-stream",
"3gpp": "video/3gpp",
"7z": "application/x-7z-compressed",
"asx": "video/x-ms-asf",
"atom": "application/atom+xml",
"avi": "video/x-msvideo",
"avif": "image/avif",
"bin": "application/octet-stream",
"bmp": "image/x-ms-bmp",
"cco": "application/x-cocoa",
"css": "text/css",
"data": "application/octet-stream",
"deb": "application/octet-stream",
"der": "application/x-x509-ca-cert",
"dmg": "application/octet-stream",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"eot": "application/vnd.ms-fontobject",
"flv": "video/x-flv",
"gif": "image/gif",
"gz": "application/gzip",
"hqx": "application/mac-binhex40",
"htc": "text/x-component",
"html": "text/html",
"ico": "image/x-icon",
"iso": "application/octet-stream",
"jad": "text/vnd.sun.j2me.app-descriptor",
"jar": "application/java-archive",
"jardiff": "application/x-java-archive-diff",
"jng": "image/x-jng",
"jnlp": "application/x-java-jnlp-file",
"jpg": "image/jpeg",
"jpeg": "image/jpeg",
"js": "application/javascript",
"json": "application/json",
"kml": "application/vnd.google-earth.kml+xml",
"kmz": "application/vnd.google-earth.kmz",
"m3u8": "application/vnd.apple.mpegurl",
"m4a": "audio/x-m4a",
"m4v": "video/x-m4v",
"md": "text/plain",
"mid": "audio/midi",
"mml": "text/mathml",
"mng": "video/x-mng",
"mov": "video/quicktime",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"msi": "application/octet-stream",
"odg": "application/vnd.oasis.opendocument.graphics",
"odp": "application/vnd.oasis.opendocument.presentation",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"odt": "application/vnd.oasis.opendocument.text",
"ogg": "audio/ogg",
"otf": "font/otf",
"pdf": "application/pdf",
"pl": "application/x-perl",
"png": "image/png",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"prc": "application/x-pilot",
"ps": "application/postscript",
"ra": "audio/x-realaudio",
"rar": "application/x-rar-compressed",
"rpm": "application/x-redhat-package-manager",
"rss": "application/rss+xml",
"rtf": "application/rtf",
"run": "application/x-makeself",
"sea": "application/x-sea",
"sit": "application/x-stuffit",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tcl": "application/x-tcl",
"tar": "application/x-tar",
"tif": "image/tiff",
"ts": "video/mp2t",
"ttf": "font/ttf",
"txt": "text/plain",
"wasm": "application/wasm",
"wbmp": "image/vnd.wap.wbmp",
"webm": "video/webm",
"webp": "image/webp",
"wml": "text/vnd.wap.wml",
"wmlc": "application/vnd.wap.wmlc",
"wmv": "video/x-ms-wmv",
"woff": "font/woff",
"woff2": "font/woff2",
"xhtml": "application/xhtml+xml",
"xls": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xml": "text/xml",
"xpi": "application/x-xpinstall",
"xspf": "application/xspf+xml",
"zip": "application/zip"
}
74 changes: 3 additions & 71 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SpawnedPHP,
} from './php-process-manager';
import { HttpCookieStore } from './http-cookie-store';
import mimeTypes from './mime-types.json';

export type RewriteRule = {
match: RegExp;
Expand Down Expand Up @@ -499,77 +500,8 @@ export class PHPRequestHandler<PHP extends BasePHP> {
* @returns The inferred mime type.
*/
function inferMimeType(path: string): string {
const extension = path.split('.').pop();
switch (extension) {
case 'css':
return 'text/css';
case 'js':
return 'application/javascript';
case 'png':
return 'image/png';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'gif':
return 'image/gif';
case 'svg':
return 'image/svg+xml';
case 'woff':
return 'font/woff';
case 'woff2':
return 'font/woff2';
case 'ttf':
return 'font/ttf';
case 'otf':
return 'font/otf';
case 'eot':
return 'font/eot';
case 'ico':
return 'image/x-icon';
case 'html':
return 'text/html';
case 'json':
return 'application/json';
case 'xml':
return 'application/xml';
case 'txt':
case 'md':
return 'text/plain';
case 'pdf':
return 'application/pdf';
case 'webp':
return 'image/webp';
case 'mp3':
return 'audio/mpeg';
case 'mp4':
return 'video/mp4';
case 'csv':
return 'text/csv';
case 'xls':
return 'application/vnd.ms-excel';
case 'xlsx':
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
case 'doc':
return 'application/msword';
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
case 'ppt':
return 'application/vnd.ms-powerpoint';
case 'pptx':
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
case 'zip':
return 'application/zip';
case 'rar':
return 'application/x-rar-compressed';
case 'tar':
return 'application/x-tar';
case 'gz':
return 'application/gzip';
case '7z':
return 'application/x-7z-compressed';
default:
return 'application-octet-stream';
}
const extension = path.split('.').pop() as keyof typeof mimeTypes;
return mimeTypes[extension] || mimeTypes['_default'];
}

/**
Expand Down

0 comments on commit 9b2debf

Please sign in to comment.