[wp-trac] [WordPress Trac] #42560: mp4 files do not play in Safari
WordPress Trac
noreply at wordpress.org
Tue Feb 23 07:24:59 UTC 2021
#42560: mp4 files do not play in Safari
--------------------------+------------------------------
Reporter: cg923 | Owner: (none)
Type: defect (bug) | Status: reopened
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 3.0
Severity: normal | Resolution:
Keywords: | Focuses: multisite
--------------------------+------------------------------
Comment (by dd32):
Just noting that I've deployed a variant of [attachment:"ms-files.diff"]
as a sunrise.php filter on WordPress.org, which I'm offering below for
others:
{{{
// Add support for Range requests via ms-files.php
// See https://meta.trac.wordpress.org/ticket/5388
// See https://core.trac.wordpress.org/ticket/42560
if (
defined( 'SHORTINIT' ) && SHORTINIT &&
( ! defined( 'WPMU_ACCEL_REDIRECT' ) || ! WPMU_ACCEL_REDIRECT ) &&
( ! defined( 'WPMU_SENDFILE' ) || ! WPMU_SENDFILE ) &&
'ms-files.php' === basename( $_SERVER['SCRIPT_FILENAME'] )
) {
// Note support for byte-range requests.
header( 'Accept-Ranges: bytes' );
// If this is a range request, once Multisite is loaded, override
ms-files.php.
isset( $_SERVER['HTTP_RANGE'] ) && add_action( 'ms_loaded',
function() {
$upload = wp_upload_dir();
$file = path_join( $upload['basedir'], $_GET['file'] );
$size = file_exists( $file ) ? filesize( $file ) : 0;
// Bail if it doesn't exist, or is empty.
if ( ! $size ) {
return;
}
if ( !
preg_match('/^bytes=(?P<start>\d+)-(?P<end>\d*)$/i',
$_SERVER['HTTP_RANGE'], $m ) ) {
return;
}
$start = (int) $m['start'];
$end = (int) $m['end'] ? $m['end'] + 1 : $size;
// Validate the file is small/big enough.
if ( $start > $size || $end > $size || $end < $start ||
$start === $end ) {
status_header( 416 );
header( 'Content-Range: bytes */' . $size );
die( '416 - Request Range Not Satisfiable.' );
}
status_header( 206 );
header( sprintf( 'Content-Range: bytes %d-%d/%d', $start,
$end - 1, $size ) );
header( 'Content-Length: ' . ( $end - $start ) );
$mime = wp_check_filetype( $file );
if ( $mime['type'] ) {
header( 'Content-Type: ' . $mime['type'] );
} else {
header( 'Content-Type: image/' . substr( $file,
strrpos( $file, '.' ) + 1 ) );
}
// Micro-optimization to shortcut.
if ( $start === 0 && $end === $size ) {
readfile( $file );
die();
}
// Open the file and stream it.
$handle = fopen( $file, 'rb' );
fseek( $handle, $start );
$chunk_size = 8 * KB_IN_BYTES;
$size_left = $end - $start;
while (
$size_left &&
! feof( $handle ) &&
$data = fread( $handle, min( $chunk_size,
$size_left ) )
) {
$size_left -= strlen( $data );
echo $data;
}
fclose( $handle );
die();
}, 100 );
}
}}}
Despite my earlier reservations for not having to handle this in WordPress
core, fixing this is a win for any older sites still using
ms_files_rewriting.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42560#comment:18>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list