Removed the ability to pass a key into stashFile(), which simplifies the stash row...
[lhc/web/wiklou.git] / includes / upload / UploadFromStash.php
index 1ee720a..a2f9be5 100644 (file)
@@ -38,7 +38,7 @@ class UploadFromStash extends UploadBase {
        
        public static function isValidKey( $key ) {
                // this is checked in more detail in UploadStash
-               return preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
+               return preg_match( UploadStash::KEY_FORMAT_REGEX, $key ) ? true : false;
        }
 
        /**
@@ -47,7 +47,10 @@ class UploadFromStash extends UploadBase {
         * @return Boolean
         */
        public static function isValidRequest( $request ) {
-               return self::isValidKey( $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' ) );
+               // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
+               // wpSessionKey has no default which guarantees failure if both are missing
+               // (though that should have been caught earlier)
+               return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
        }
 
        public function initialize( $key, $name = 'upload_file' ) {
@@ -74,12 +77,12 @@ class UploadFromStash extends UploadBase {
         * @param $request WebRequest
         */
        public function initializeFromRequest( &$request ) {
-               $fileKey = $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' );
+               // sends wpSessionKey as a default when wpFileKey is missing
+               $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
+
+               // chooses one of wpDestFile, wpUploadFile, filename in that order.
+               $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
 
-               $desiredDestName = $request->getText( 'wpDestFile' );
-               if( !$desiredDestName ) {
-                       $desiredDestName = $request->getText( 'wpUploadFile' ) || $request->getText( 'filename' );
-               }
                return $this->initialize( $fileKey, $desiredDestName );
        }
 
@@ -99,18 +102,18 @@ class UploadFromStash extends UploadBase {
        /**
         * There is no need to stash the image twice
         */
-       public function stashFile( $key = null ) {
-               if ( !empty( $this->mFileKey ) ) {
-                       return $this->mFileKey;
+       public function stashFile() {
+               if ( $this->mLocalFile ) {
+                       return $this->mLocalFile;
                }
-               return parent::stashFileGetKey();
+               return parent::stashFile();
        }
 
        /**
         * Alias for stashFile
         */
-       public function stashSession( $key = null ) {
-               return $this->stashFile( $key );
+       public function stashSession() {
+               return $this->stashFile();
        }
 
        /**
@@ -118,7 +121,48 @@ class UploadFromStash extends UploadBase {
         * @return success
         */
        public function unsaveUploadedFile() {
-               return $stash->removeFile( $this->mFileKey );
+               return $this->stash->removeFile( $this->mFileKey );
+       }
+
+       /**
+        * Perform the upload, then remove the database record afterward.
+        */
+       public function performUpload( $comment, $pageText, $watch, $user ) {
+               $rv = parent::performUpload( $comment, $pageText, $watch, $user );
+               $this->unsaveUploadedFile();
+               return $rv;
+       }
+
+       /**
+        * Append a chunk to the temporary file.
+        *
+        * @return void
+        */
+       public function appendChunk($chunk, $chunkSize, $offset) {
+               //to use $this->getFileSize() here, db needs to be updated
+               //in appendToUploadFile for that
+               $fileSize = $this->stash->getFile( $this->mFileKey )->getSize();
+               if ( $fileSize + $chunkSize > $this->getMaxUploadSize()) {
+                       $status = Status::newFatal( 'file-too-large' );
+               } else {
+                       //append chunk
+                       if ( $fileSize == $offset ) {
+                               $status = $this->appendToUploadFile( $chunk,
+                                                                                                        $this->mVirtualTempPath );
+                       } else {
+                               $status = Status::newFatal( 'invalid-chunk-offset' );
+                       }
+               }
+               return $status;
        }
 
-}
\ No newline at end of file
+       /**
+        * Append the final chunk and ready file for parent::performUpload()
+        * @return void
+        */
+       public function finalizeFile() {
+               $this->appendFinish ( $this->mVirtualTempPath );
+               $this->cleanupTempFile();
+               $this->mTempPath = $this->getRealPath( $this->mVirtualTempPath );
+       }
+}