Making WordPress.org


Ignore:
Timestamp:
03/13/2017 05:56:27 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: ZIPs: Do not build zips on demand, instead store them within a SVN repository.

See #1578

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/zip/class-serve.php

    r4728 r5147  
    1313class Serve {
    1414
    15         const ZIP_DIR = '/tmp/plugin-zipfiles';
    16 
    1715        public function __construct() {
    1816                try {
    1917                        $request = $this->determine_request();
    2018
    21                         // Serve & perhaps build if need be
    22                         $files = $this->get_files( $request );
    23                         if ( ! file_exists( $files['zip'] ) ) {
    24                                 $builder = new Builder( $request['slug'], $request['version'] );
    25                                 $builder->build();
    26                                 clearstatcache();
    27                         }
    28 
    29                         $this->serve_zip( $files, $request );
     19                        $this->serve_zip( $request );
    3020
    3121                        if ( $request['args']['stats'] ) {
    … …  
    3424
    3525                } catch ( Exception $e )  {
    36                         $this->error( $e->getCode() );
     26                        $this->error();
    3727                }
    3828
    … …  
    5141
    5242                if ( ! preg_match( "!^(?P<slug>[a-z0-9-]+)(.(?P<version>.+))?.zip$!i", $zip, $m ) ) {
    53                         throw new Exception( __METHOD__ . ": Invalid URL" );
     43                        throw new Exception( __METHOD__ . ": Invalid URL." );
    5444                }
    5545
    … …  
    9686                }
    9787                if ( ! $version ) {
    98                         throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined.", 404 );
     88                        throw new Exception( __METHOD__ . ": A version for $plugin_slug cannot be determined." );
    9989                }
    10090
    … …  
    121111
    122112                if ( ! $post_id ) {
    123                         throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined.", 404 );
     113                        throw new Exception( __METHOD__ . ": A post_id for $plugin_slug cannot be determined." );
    124114                }
    125115
    … …  
    133123         * @return array An array containing the files to use for the request, 'zip' and 'md5'.
    134124         */
    135         protected function get_files( $request ) {
     125        protected function get_file( $request ) {
    136126                if ( empty( $request['version'] ) || 'trunk' == $request['version'] ) {
    137                         $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.zip";
     127                        return "{$request['slug']}/{$request['slug']}.zip";
    138128                } else {
    139                         $zip = self::ZIP_DIR . "/{$request['slug']}/{$request['slug']}.{$request['version']}.zip";
    140                 }
    141                 $md5 = $zip . '.md5';
    142 
    143                 return compact( 'zip', 'md5' );
     129                        return "{$request['slug']}/{$request['slug']}.{$request['version']}.zip";
     130                }
    144131        }
    145132
    … …  
    147134         * Output a ZIP file with all headers.
    148135         *
    149          * @param array $files {
    150          *   Array of files for the request.
    151          *
    152          *   @type string $zip The Zip file to serve.
    153          *   @type string $md5 The MD5 file to use for the Content-MD5 header. Optional.
    154          * }
    155136         * @param array $request The request array for the request.
    156137         */
    157         protected function serve_zip( $files, $request ) {
    158                 header( 'Content-Type: application/zip' );
    159                 header( 'Content-Disposition: attachment; filename=' . basename( $files['zip'] ) );
    160                 if ( !empty( $files['md5'] ) && ( $md5 = file_get_contents( $files['md5'] ) )  ) {
    161                         header( 'Content-MD5: ' . $md5 );
    162                 }
    163 
    164                 // TODO: Accel Redirect allows for ZIP files to be cached on the LB's
    165                 // header('X-Accel-Redirect: ' . $accel_redirect );
    166 
    167                 header( 'Content-Length: ' . filesize( $files['zip'] ) );
    168                 readfile( $files['zip'] );
     138        protected function serve_zip( $request ) {
     139                $zip = $this->get_file( $request );
     140
     141                if ( defined( 'PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION' ) ) {
     142                        $zip_url = PLUGIN_ZIP_X_ACCEL_REDIRECT_LOCATION . $zip;
     143
     144                        header( 'Content-Type: application/zip' );
     145                        header( 'Content-Disposition: attachment; filename=' . basename( $zip ) );
     146                        header( "X-Accel-Redirect: $zip_url" );
     147                } else {
     148                        header( 'Content-Type: text/plain' );
     149                        echo "This is a request for $zip, this server isn't currently configured to serve zip files.\n";
     150                }
     151
     152                if ( function_exists( 'fastcgi_finish_request' ) ) {
     153                        fastcgi_finish_request();
     154                }
     155
    169156        }
    170157
    … …  
    225212
    226213        /**
    227          * Quit with an Error code.
    228          *
    229          * @param int $code The HTTP Error code, 404 or 503.
    230          */
    231         protected function error( $code = 404 ) {
     214         * Bail with a 404.
     215         */
     216        protected function error() {
    232217                $protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
    233218                $protocol .= ' ';
    234                 switch ( $code ) {
    235                         case 503:
    236                                 header( $protocol . '503 Service Unavailable' );
    237                                 die( '503 Service Unavailable' );
    238 
    239                         default:
    240                         case 404:
    241                                 header( $protocol . '404 File not found' );
    242                                 die( '404 File not found' );
    243                 }
     219
     220                header( $protocol . '404 File not found' );
     221                die( '404 file not found' );
    244222        }
    245223
Note: See TracChangeset for help on using the changeset viewer.