From 9b50e7ade7d2befdcd933f1005f95c82469332b0 Mon Sep 17 00:00:00 2001 From: Ian Baker Date: Thu, 14 Jul 2011 23:01:00 +0000 Subject: [PATCH] properly handle the case where a file disappears during the uploadwizard process remove database records for files that move out of the stash --- includes/api/ApiUpload.php | 1 + includes/upload/UploadBase.php | 2 +- includes/upload/UploadFromStash.php | 17 +++++++++++++---- includes/upload/UploadStash.php | 9 +++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/includes/api/ApiUpload.php b/includes/api/ApiUpload.php index 3a8326f428..b101ecc672 100644 --- a/includes/api/ApiUpload.php +++ b/includes/api/ApiUpload.php @@ -147,6 +147,7 @@ class ApiUpload extends ApiBase { */ function performStash() { try { + xdebug_break(); $fileKey = $this->mUpload->stashFile()->getFileKey(); } catch ( MWException $e ) { $message = 'Stashing temporary file failed: ' . get_class( $e ) . ' ' . $e->getMessage(); diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index 67d275ff79..fc64818fe5 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -589,7 +589,7 @@ abstract class UploadBase { if ( $watch ) { $user->addWatch( $this->getLocalFile()->getTitle() ); } - + wfRunHooks( 'UploadComplete', array( &$this ) ); } diff --git a/includes/upload/UploadFromStash.php b/includes/upload/UploadFromStash.php index 1ee720a158..feb14a87f4 100644 --- a/includes/upload/UploadFromStash.php +++ b/includes/upload/UploadFromStash.php @@ -100,10 +100,10 @@ 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; + if ( !empty( $this->mLocalFile ) ) { + return $this->mLocalFile; } - return parent::stashFileGetKey(); + return parent::stashFile( $key ); } /** @@ -118,7 +118,16 @@ 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; } } \ No newline at end of file diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php index 66bf33f105..8ebacbbf76 100644 --- a/includes/upload/UploadStash.php +++ b/includes/upload/UploadStash.php @@ -131,6 +131,11 @@ class UploadStash { $this->fileProps[$key] = File::getPropsFromPath( $path ); } + if ( ! $this->files[$key]->exists() ) { + wfDebug( __METHOD__ . " tried to get file at $key, but it doesn't exist\n" ); + throw new UploadStashBadPathException( "path doesn't exist" ); + } + if( !$noAuth ) { if( $this->fileMetadata[$key]['us_user'] != $this->userId ) { throw new UploadStashWrongOwnerException( "This file ($key) doesn't belong to the current user." ); @@ -671,6 +676,10 @@ class UploadStashFile extends UnregisteredLocalFile { return $this->repo->freeTemp( $this->path ); } + public function exists() { + return $this->repo->fileExists( $this->path, FileRepo::FILES_ONLY ); + } + } class UploadStashNotAvailableException extends MWException {}; -- 2.20.1