Skip to content

Commit ceefa58

Browse files
Derive MIME types for PHP served files from shared JSON (#1360)
## What is this PR doing? This PR eliminates duplication in the code base where we maintained separate MIME type mappings for PHPRequestHandler and for files served via PHP on WP Cloud. Related to #1197 ## How is the problem addressed? This PR generates `mime-types.php` from shared JSON when deploying the website to WP Cloud. ## Testing Instructions - Temporarily adjust WP Cloud deploy workflow to be tested under this PR - Confirm the workflow completes without error and that playground-dot-wordpress-dot-net.atomicsites.blog loads properly
1 parent 0d37d79 commit ceefa58

File tree

5 files changed

+49
-99
lines changed

5 files changed

+49
-99
lines changed

Diff for: .github/workflows/deploy-website-next.yml

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
- uses: actions/checkout@v4
2828
with:
2929
sparse-checkout: |
30+
packages/php-wasm/universal/src/lib/mime-types.json
3031
packages/playground/website-deployment
3132
- name: Wait for latest build artifact
3233
shell: bash
@@ -79,6 +80,11 @@ jobs:
7980
packages/playground/website-deployment/ \
8081
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }}:'~/website-deployment'
8182
83+
# Copy MIME types for use with PHP-served files
84+
scp -i ~/.ssh/id_ed25519 \
85+
packages/php-wasm/universal/src/lib/mime-types.json \
86+
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }}:'~/website-deployment/'
87+
8288
# Apply update
8389
ssh -i ~/.ssh/id_ed25519 \
8490
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }} \

Diff for: packages/php-wasm/universal/src/lib/mime-types.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"_default": "application-octet-stream",
2+
"_default": "application/octet-stream",
33
"3gpp": "video/3gpp",
44
"7z": "application/x-7z-compressed",
55
"asx": "video/x-ms-asf",

Diff for: packages/playground/website-deployment/apply-update.sh

+39
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,45 @@ echo Copy supporting files for WP Cloud
1919
cp -r ~/website-deployment/__wp__ ~/website-update/
2020
cp ~/website-deployment/custom-redirects-lib.php ~/website-update/
2121
cp ~/website-deployment/custom-redirects.php ~/website-update/
22+
23+
# Generate mime-types.php from mime-types.json in case the PHP can be opcached
24+
echo Generating mime-types.php
25+
cat ~/website-deployment/mime-types.json | "$SITE_PHP" -r '
26+
$raw_json = file_get_contents( "php://stdin" );
27+
if ( false === $raw_json ) {
28+
fprintf( STDERR, "Unable to read MIME type JSON\n" );
29+
die( -1 );
30+
}
31+
$types = json_decode( $raw_json, JSON_OBJECT_AS_ARRAY );
32+
if ( null === $types ) {
33+
fprintf( STDERR, "Unable to decode MIME type JSON\n" );
34+
die( -1 );
35+
}
36+
37+
echo "<?php\n";
38+
echo "\$mime_types = array(\n";
39+
foreach ( $types as $extension => $mime_type ) {
40+
echo " \"$extension\" => \"$mime_type\",\n";
41+
}
42+
echo ");\n";
43+
' > ~/website-deployment/mime-types.php
44+
45+
echo Checking mime-types.php
46+
(
47+
cd ~/website-deployment
48+
"$SITE_PHP" -r '
49+
require( "mime-types.php" );
50+
if (
51+
$mime_types["_default"] !== "application/octet-stream" ||
52+
$mime_types["html"] !== "text/html" ||
53+
$mime_types["jpeg"] !== "image/jpeg"
54+
) {
55+
fprintf( STDERR, "mime-types.php looks broken\n" );
56+
die( -1 );
57+
}
58+
'
59+
)
60+
echo Adding mime-types.php to updated website files
2261
cp ~/website-deployment/mime-types.php ~/website-update/
2362

2463
function match_files_to_serve_via_php() (

Diff for: packages/playground/website-deployment/custom-redirects-lib.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ function playground_handle_request() {
124124
: false;
125125

126126
require_once __DIR__ . '/mime-types.php';
127-
if ( isset( $mime_types[ $extension ] ) ) {
128-
$content_type = $mime_types[ $extension ];
129-
$log( "Setting Content-Type to '$content_type'" );
130-
header( "Content-Type: $content_type" );
131-
}
127+
$content_type = $mime_types[ $extension ] ?? $mime_types['_default'];
128+
$log( "Setting Content-Type to '$content_type'" );
129+
header( "Content-Type: $content_type" );
132130

133131
$custom_response_headers = playground_get_custom_response_headers( $filename );
134132
if ( ! empty( $custom_response_headers ) ) {

Diff for: packages/playground/website-deployment/mime-types.php

-93
This file was deleted.

0 commit comments

Comments
 (0)