Add ability to proxy thumbnail requests to a service
authorGilles Dubuc <gilles@wikimedia.org>
Mon, 20 Nov 2017 06:57:29 +0000 (07:57 +0100)
committerGilles Dubuc <gilles@wikimedia.org>
Thu, 1 Feb 2018 09:07:32 +0000 (10:07 +0100)
Bug: T169144
Change-Id: I4af09a8b75e7158d6ff15f97e8f067b66ac33d5c

includes/filerepo/FileRepo.php
thumb.php

index b4df68a..6b32953 100644 (file)
@@ -132,6 +132,13 @@ class FileRepo {
        /** @var array callable|bool Override these in the base class */
        protected $oldFileFactoryKey = false;
 
+       /** @var string URL of where to proxy thumb.php requests to.
+        *    Example: http://127.0.0.1:8888/wiki/dev/thumb/
+        */
+       protected $thumbProxyUrl;
+       /** @var string Secret key to pass as an X-Swift-Secret header to the proxied thumb service */
+       protected $thumbProxySecret;
+
        /**
         * @param array|null $info
         * @throws MWException
@@ -159,7 +166,7 @@ class FileRepo {
                $optionalSettings = [
                        'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
                        'thumbScriptUrl', 'pathDisclosureProtection', 'descriptionCacheExpiry',
-                       'scriptExtension', 'favicon'
+                       'scriptExtension', 'favicon', 'thumbProxyUrl', 'thumbProxySecret'
                ];
                foreach ( $optionalSettings as $var ) {
                        if ( isset( $info[$var] ) ) {
@@ -611,6 +618,24 @@ class FileRepo {
                return $this->thumbScriptUrl;
        }
 
+       /**
+        * Get the URL thumb.php requests are being proxied to
+        *
+        * @return string
+        */
+       public function getThumbProxyUrl() {
+               return $this->thumbProxyUrl;
+       }
+
+       /**
+        * Get the secret key for the proxied thumb service
+        *
+        * @return string
+        */
+       public function getThumbProxySecret() {
+               return $this->thumbProxySecret;
+       }
+
        /**
         * Returns true if the repository can transform files via a 404 handler
         *
index 02ac0b0..c4b40dc 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -337,7 +337,16 @@ function wfStreamThumb( array $params ) {
                return;
        }
 
-       list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
+       $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
+
+       if ( strlen( $thumbProxyUrl ) ) {
+               wfProxyThumbnailRequest( $img, $thumbName );
+               // No local fallback when in proxy mode
+               return;
+       } else {
+               // Generate the thumbnail locally
+               list( $thumb, $errorMsg ) = wfGenerateThumbnail( $img, $params, $thumbName, $thumbPath );
+       }
 
        /** @var MediaTransformOutput|MediaTransformError|bool $thumb */
 
@@ -377,6 +386,43 @@ function wfStreamThumb( array $params ) {
        }
 }
 
+/**
+ * Proxies thumbnail request to a service that handles thumbnailing
+ *
+ * @param File $img
+ * @param string $thumbName
+ */
+function wfProxyThumbnailRequest( $img, $thumbName ) {
+       $thumbProxyUrl = $img->getRepo()->getThumbProxyUrl();
+
+       // Instead of generating the thumbnail ourselves, we proxy the request to another service
+       $thumbProxiedUrl = $thumbProxyUrl . $img->getThumbRel( $thumbName );
+
+       $req = MWHttpRequest::factory( $thumbProxiedUrl );
+       $secret = $img->getRepo()->getThumbProxySecret();
+
+       // Pass a secret key shared with the proxied service if any
+       if ( strlen( $secret ) ) {
+               $req->setHeader( 'X-Swift-Secret', $secret );
+       }
+
+       // Send request to proxied service
+       $status = $req->execute();
+
+       // Simply serve the response from the proxied service as-is
+       header( 'HTTP/1.1 ' . $req->getStatus() );
+
+       $headers = $req->getResponseHeaders();
+
+       foreach ( $headers as $key => $values ) {
+               foreach ( $values as $value ) {
+                       header( $key . ': ' . $value, false );
+               }
+       }
+
+       echo $req->getContent();
+}
+
 /**
  * Actually try to generate a new thumbnail
  *