https://www.mediawiki.org/wiki/Special:Code/MediaWiki/104645

Revision: 104645
Author:   aaron
Date:     2011-11-30 01:53:43 +0000 (Wed, 30 Nov 2011)
Log Message:
-----------
* doTranform() error handling cleanups
* Created TempFSFile::factory() function

Modified Paths:
--------------
    branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php
    branches/FileBackend/phase3/includes/media/Generic.php
    branches/FileBackend/phase3/languages/messages/MessagesEn.php

Property Changed:
----------------
    branches/FileBackend/phase3/


Property changes on: branches/FileBackend/phase3
___________________________________________________________________
Modified: svn:ignore
   - *~
.classpath
.idea
.metadata*
.project
.settings
AdminSettings.php
LocalSettings.php
StartProfiler.php
cscope.files
cscope.out
favicon.ico
nbproject*
project.index
static*
tags

   + *~
.classpath
.idea
.metadata*
.project
.settings
AdminSettings.php
LocalSettings.php
StartProfiler.php
cscope.files
cscope.out
favicon.ico
nbproject*
project.index
static*
tags
.jpg
.png


Modified: branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php   
2011-11-30 01:42:16 UTC (rev 104644)
+++ branches/FileBackend/phase3/includes/filerepo/file/TempFSFile.php   
2011-11-30 01:53:43 UTC (rev 104645)
@@ -16,6 +16,29 @@
        protected $canDelete = true; // garbage collect the temp file
 
        /**
+        * Make a new temporary file on the file system
+        * 
+        * @param $prefix string
+        * @param $extension string
+        * @return TempFSFile|null 
+        */
+       public static function factory( $prefix, $extension = '' ) {
+               $tmpDest = tempnam( wfTempDir(), $prefix );
+               if ( $tmpDest === false ) {
+                       return null;
+               }
+               if ( $extension != '' ) {
+                       $path = "{$tmpDest}.{$extension}";
+                       if ( !rename( $tmpDest, $path ) ) {
+                               return null;
+                       }
+               } else {
+                       $path = $tmpDest;
+               }
+               return new self( $path );
+       }
+
+       /**
         * Flag to not clean up after the temporary file
         *
         * @return void

Modified: branches/FileBackend/phase3/includes/media/Generic.php
===================================================================
--- branches/FileBackend/phase3/includes/media/Generic.php      2011-11-30 
01:42:16 UTC (rev 104644)
+++ branches/FileBackend/phase3/includes/media/Generic.php      2011-11-30 
01:53:43 UTC (rev 104645)
@@ -205,24 +205,25 @@
         */
        final function doTransform( $image, $dstPath, $dstUrl, $params, $flags 
= 0 ) {
                if ( FileBackend::isStoragePath( $dstPath ) ) {
-                       // Create output on FS
-                       $tmpDest = tempnam( wfTempDir(), 'transform' ) . '.' . 
$image->getExtension();
-                       if ( $tmpDest === false ) {
-                               throw new MWException( "Could not create 
temporary thumbnail file." );
+                       // Create a temp FS file with the same extension
+                       $tmpFile = TempFSFile::factory( 'transform', 
$image->getExtension() );
+                       if ( !$tmpFile ) {
+                               return new MediaTransformError( 
'thumbnail_error',
+                                       $params['width'], 0, wfMsg( 
'thumbnail-temp-create' ) );
                        }
+                       $tmpDest = $tmpFile->getPath();
+                       // Create the output thumbnail on the FS
                        $out = $this->doFSTransform( $image, $tmpDest, $dstUrl, 
$params, $flags );
-                       // Copy any thumbnail from FS $tmpDest to $dstpath.
+                       // Copy any thumbnail from FS into storage at $dstpath
                        // Note: no file is created if it's to be rendered 
client-side.
                        if ( !$out->isError() && is_file( $tmpDest ) ) {
                                $op = array( 'op' => 'store',
                                        'source' => $tmpDest, 'dest' => 
$dstPath, 'overwriteDest' => true );
                                if ( 
!$image->getRepo()->getBackend()->doOperation( $op )->isOK() ) {
-                                       throw new MWException( "Could not copy 
thumbnail to $dstPath." );
+                                       return new MediaTransformError( 
'thumbnail_error',
+                                               $params['width'], 0, wfMsg( 
'thumbnail-dest-create' ) );
                                }
                        }
-                       wfSuppressWarnings();
-                       unlink( $tmpDest );
-                       wfRestoreWarnings();
                } else {
                        $out = $this->doFSTransform( $image, $dstPath, $dstUrl, 
$params, $flags );
                }

Modified: branches/FileBackend/phase3/languages/messages/MessagesEn.php
===================================================================
--- branches/FileBackend/phase3/languages/messages/MessagesEn.php       
2011-11-30 01:42:16 UTC (rev 104644)
+++ branches/FileBackend/phase3/languages/messages/MessagesEn.php       
2011-11-30 01:53:43 UTC (rev 104645)
@@ -3311,6 +3311,8 @@
 'thumbnail_error'          => 'Error creating thumbnail: $1',
 'djvu_page_error'          => 'DjVu page out of range',
 'djvu_no_xml'              => 'Unable to fetch XML for DjVu file',
+'thumbnail-temp-create'    => 'Unable to create temporary thumbnail file',
+'thumbnail-dest-create'    => 'Unable to save thumbnail to destination',
 'thumbnail_invalid_params' => 'Invalid thumbnail parameters',
 'thumbnail_dest_directory' => 'Unable to create destination directory',
 'thumbnail_image-type'     => 'Image type not supported',


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to