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( $stash = false, $repo = false ) {
21 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
25 $this->stash
= new UploadStash( $this->repo
);
31 public static function isValidKey( $key ) {
32 // this is checked in more detail in UploadStash
33 return preg_match( UploadStash
::KEY_FORMAT_REGEX
, $key );
37 * @param $request WebRequest
41 public static function isValidRequest( $request ) {
42 return self
::isValidKey( $request->getText( 'wpFileKey' ) ||
$request->getText( 'wpSessionKey' ) );
45 public function initialize( $key, $name = 'upload_file' ) {
47 * Confirming a temporarily stashed upload.
48 * We don't want path names to be forged, so we keep
49 * them in the session on the server and just give
50 * an opaque key to the user agent.
52 $metadata = $this->stash
->getMetadata( $key );
53 $this->initializePathInfo( $name,
54 $this->getRealPath ( $metadata['us_path'] ),
59 $this->mFileKey
= $key;
60 $this->mVirtualTempPath
= $metadata['us_path'];
61 $this->mFileProps
= $this->stash
->getFileProps( $key );
62 $this->mSourceType
= $metadata['us_source_type'];
66 * @param $request WebRequest
68 public function initializeFromRequest( &$request ) {
69 $fileKey = $request->getText( 'wpFileKey' ) ||
$request->getText( 'wpSessionKey' );
71 $desiredDestName = $request->getText( 'wpDestFile' );
72 if( !$desiredDestName ) {
73 $desiredDestName = $request->getText( 'wpUploadFile' ) ||
$request->getText( 'filename' );
75 return $this->initialize( $fileKey, $desiredDestName );
78 public function getSourceType() {
79 return $this->mSourceType
;
83 * File has been previously verified so no need to do so again.
87 protected function verifyFile() {
92 * There is no need to stash the image twice
94 public function stashFile( $key = null ) {
95 if ( !empty( $this->mFileKey
) ) {
96 return $this->mFileKey
;
98 return parent
::stashFileGetKey();
102 * Alias for stashFile
104 public function stashSession( $key = null ) {
105 return $this->stashFile( $key );
109 * Remove a temporarily kept file stashed by saveTempUploadedFile().
112 public function unsaveUploadedFile() {
113 return $stash->removeFile( $this->mFileKey
);