3 * Backend for uploading files from previously stored file.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Implements uploading from previously stored file.
28 * @author Bryan Tong Minh
30 class UploadFromStash
extends UploadBase
{
32 protected $mVirtualTempPath;
33 protected $mFileProps;
34 protected $mSourceType;
36 // an instance of UploadStash
43 * @param User|bool $user Default: false Sometimes this won't exist, as when running from cron.
44 * @param UploadStash|bool $stash Default: false
45 * @param FileRepo|bool $repo Default: false
47 public function __construct( $user = false, $stash = false, $repo = false ) {
51 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
55 $this->stash
= $stash;
58 wfDebug( __METHOD__
. " creating new UploadStash instance for " . $user->getId() . "\n" );
60 wfDebug( __METHOD__
. " creating new UploadStash instance with no user\n" );
63 $this->stash
= new UploadStash( $this->repo
, $user );
71 public static function isValidKey( $key ) {
72 // this is checked in more detail in UploadStash
73 return (bool)preg_match( UploadStash
::KEY_FORMAT_REGEX
, $key );
77 * @param WebRequest $request
80 public static function isValidRequest( $request ) {
81 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
82 // wpSessionKey has no default which guarantees failure if both are missing
83 // (though that should have been caught earlier)
84 return self
::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
90 * @param bool $initTempFile
92 public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
94 * Confirming a temporarily stashed upload.
95 * We don't want path names to be forged, so we keep
96 * them in the session on the server and just give
97 * an opaque key to the user agent.
99 $metadata = $this->stash
->getMetadata( $key );
100 $this->initializePathInfo( $name,
101 $initTempFile ?
$this->getRealPath( $metadata['us_path'] ) : false,
102 $metadata['us_size'],
106 $this->mFileKey
= $key;
107 $this->mVirtualTempPath
= $metadata['us_path'];
108 $this->mFileProps
= $this->stash
->getFileProps( $key );
109 $this->mSourceType
= $metadata['us_source_type'];
113 * @param WebRequest &$request
115 public function initializeFromRequest( &$request ) {
116 // sends wpSessionKey as a default when wpFileKey is missing
117 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
119 // chooses one of wpDestFile, wpUploadFile, filename in that order.
120 $desiredDestName = $request->getText(
122 $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
125 $this->initialize( $fileKey, $desiredDestName );
131 public function getSourceType() {
132 return $this->mSourceType
;
136 * Get the base 36 SHA1 of the file
139 public function getTempFileSha1Base36() {
140 return $this->mFileProps
['sha1'];
144 * Remove a temporarily kept file stashed by saveTempUploadedFile().
145 * @return bool Success
147 public function unsaveUploadedFile() {
148 return $this->stash
->removeFile( $this->mFileKey
);
152 * Remove the database record after a successful upload.
154 public function postProcessUpload() {
155 parent
::postProcessUpload();
156 $this->unsaveUploadedFile();