Skip to content

Commit

Permalink
Derive MIME types for PHP served files from shared JSON (#1360)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
brandonpayton authored May 3, 2024
1 parent 0d37d79 commit ceefa58
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 99 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/deploy-website-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- uses: actions/checkout@v4
with:
sparse-checkout: |
packages/php-wasm/universal/src/lib/mime-types.json
packages/playground/website-deployment
- name: Wait for latest build artifact
shell: bash
Expand Down Expand Up @@ -79,6 +80,11 @@ jobs:
packages/playground/website-deployment/ \
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }}:'~/website-deployment'
# Copy MIME types for use with PHP-served files
scp -i ~/.ssh/id_ed25519 \
packages/php-wasm/universal/src/lib/mime-types.json \
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }}:'~/website-deployment/'
# Apply update
ssh -i ~/.ssh/id_ed25519 \
${{ secrets.DEPLOY_WEBSITE_TARGET_USER }}@${{ secrets.DEPLOY_WEBSITE_TARGET_HOST }} \
Expand Down
2 changes: 1 addition & 1 deletion packages/php-wasm/universal/src/lib/mime-types.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"_default": "application-octet-stream",
"_default": "application/octet-stream",
"3gpp": "video/3gpp",
"7z": "application/x-7z-compressed",
"asx": "video/x-ms-asf",
Expand Down
39 changes: 39 additions & 0 deletions packages/playground/website-deployment/apply-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ echo Copy supporting files for WP Cloud
cp -r ~/website-deployment/__wp__ ~/website-update/
cp ~/website-deployment/custom-redirects-lib.php ~/website-update/
cp ~/website-deployment/custom-redirects.php ~/website-update/

# Generate mime-types.php from mime-types.json in case the PHP can be opcached
echo Generating mime-types.php
cat ~/website-deployment/mime-types.json | "$SITE_PHP" -r '
$raw_json = file_get_contents( "php://stdin" );
if ( false === $raw_json ) {
fprintf( STDERR, "Unable to read MIME type JSON\n" );
die( -1 );
}
$types = json_decode( $raw_json, JSON_OBJECT_AS_ARRAY );
if ( null === $types ) {
fprintf( STDERR, "Unable to decode MIME type JSON\n" );
die( -1 );
}
echo "<?php\n";
echo "\$mime_types = array(\n";
foreach ( $types as $extension => $mime_type ) {
echo " \"$extension\" => \"$mime_type\",\n";
}
echo ");\n";
' > ~/website-deployment/mime-types.php

echo Checking mime-types.php
(
cd ~/website-deployment
"$SITE_PHP" -r '
require( "mime-types.php" );
if (
$mime_types["_default"] !== "application/octet-stream" ||
$mime_types["html"] !== "text/html" ||
$mime_types["jpeg"] !== "image/jpeg"
) {
fprintf( STDERR, "mime-types.php looks broken\n" );
die( -1 );
}
'
)
echo Adding mime-types.php to updated website files
cp ~/website-deployment/mime-types.php ~/website-update/

function match_files_to_serve_via_php() (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ function playground_handle_request() {
: false;

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

$custom_response_headers = playground_get_custom_response_headers( $filename );
if ( ! empty( $custom_response_headers ) ) {
Expand Down
93 changes: 0 additions & 93 deletions packages/playground/website-deployment/mime-types.php

This file was deleted.

0 comments on commit ceefa58

Please sign in to comment.