Page MenuHomePhabricator

The thumb_handler.php endpoint should be able to stream webp with the correct Content-Type header
Closed, ResolvedPublic

Description

It's possible to serve WebP conditionally with some rewrite rules or reverse proxy, use 404 transform with media handlers overridden:

if ( MW_ENTRY_POINT === 'thumb_handler' ) {
	global $wgMediaHandlers;
	$wgMediaHandlers['image/webp'] = WebPThumb\WebPHandler::class;
	$wgMediaHandlers['image/png'] =  WebPThumb\PNGHandler::class;
}

(I created a PoC extension locally with the onMediaWikiServices hook, but LocalSettings.php etc. may also work.)

namespace WebPThumb;

class WebPHandler extends \WebPHandler {

	public function getThumbType( $ext, $mime, $params = null ) {
		return [ 'webp', 'image/webp' ];
	}
}

For incoming requests, strip .png postfix for webp files, add .webp postfix for png files, then pass the request to the thumb_handler.php endpoint.

The only issue is that the Content-Type header of the response is application/x-wiki.

This is because $wgTrivialMimeDetection is set to true by ThumbnailEntryPoint and the StreamFile::contentTypeFromPath() function doesn't consider webp as a valid extension in this case.

The thumb_handler.php endpoint should be able to stream webp with the correct Content-Type header image/webp.

Event Timeline

Change #1037502 had a related patch set uploaded (by Func; author: Func):

[mediawiki/core@master] StreamFile: Support streaming webp from thumb_handler.php

https://gerrit.wikimedia.org/r/1037502

I like this idea, but I’m wondering why it’s only hardcoding these file extensions to begin with…. We have a whole mime library, maybe we should be using that ? Although it might be doubling as an allowlist ? Will have to double check.

According to MainConfigSchema.php#2225 and the original commit 27105c21295dbc16532875feb3582a0555576e73 which added this config:

Switch for trivial mime detection. Used by thumb.php to disable all fance things,
because only a few types of images are needed and file extensions can be trusted.

Note that commit 829f1142b0e834979b3a73a3a1aa6a58347cacf4 switched to only detecting the type by file extension, we can reuse that part.

Change #1037502 merged by jenkins-bot:

[mediawiki/core@master] StreamFile: Support streaming webp from thumb_handler.php

https://gerrit.wikimedia.org/r/1037502

Func claimed this task.