3 * Implements uploading from previously stored file.
7 * @author Bryan Tong Minh
10 class UploadFromStash
extends UploadBase
{
11 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
13 // an instance of UploadStash
19 public function __construct( $user = false, $stash = false, $repo = false ) {
20 // user object. sometimes this won't exist, as when running from cron.
26 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
30 $this->stash
= $stash;
32 wfDebug( __METHOD__
. " creating new UploadStash instance for " . $user->getId() . "\n" );
33 $this->stash
= new UploadStash( $this->repo
, $this->user
);
39 public static function isValidKey( $key ) {
40 // this is checked in more detail in UploadStash
41 return preg_match( UploadStash
::KEY_FORMAT_REGEX
, $key );
45 * @param $request WebRequest
49 public static function isValidRequest( $request ) {
50 return self
::isValidKey( $request->getText( 'wpFileKey' ) ||
$request->getText( 'wpSessionKey' ) );
53 public function initialize( $key, $name = 'upload_file' ) {
55 * Confirming a temporarily stashed upload.
56 * We don't want path names to be forged, so we keep
57 * them in the session on the server and just give
58 * an opaque key to the user agent.
60 $metadata = $this->stash
->getMetadata( $key );
61 $this->initializePathInfo( $name,
62 $this->getRealPath ( $metadata['us_path'] ),
67 $this->mFileKey
= $key;
68 $this->mVirtualTempPath
= $metadata['us_path'];
69 $this->mFileProps
= $this->stash
->getFileProps( $key );
70 $this->mSourceType
= $metadata['us_source_type'];
74 * @param $request WebRequest
76 public function initializeFromRequest( &$request ) {
77 $fileKey = $request->getText( 'wpFileKey' ) ||
$request->getText( 'wpSessionKey' );
79 $desiredDestName = $request->getText( 'wpDestFile' );
80 if( !$desiredDestName ) {
81 $desiredDestName = $request->getText( 'wpUploadFile' ) ||
$request->getText( 'filename' );
83 return $this->initialize( $fileKey, $desiredDestName );
86 public function getSourceType() {
87 return $this->mSourceType
;
91 * File has been previously verified so no need to do so again.
95 protected function verifyFile() {
100 * There is no need to stash the image twice
102 public function stashFile( $key = null ) {
103 if ( !empty( $this->mFileKey
) ) {
104 return $this->mFileKey
;
106 return parent
::stashFileGetKey();
110 * Alias for stashFile
112 public function stashSession( $key = null ) {
113 return $this->stashFile( $key );
117 * Remove a temporarily kept file stashed by saveTempUploadedFile().
120 public function unsaveUploadedFile() {
121 return $stash->removeFile( $this->mFileKey
);