6 * First, destination checks are made, and, if ignorewarnings is not
7 * checked, errors / warning is returned.
9 * 1. We return the uploadUrl.
10 * 2. We then accept chunk uploads from the client.
11 * 3. Return chunk id on each POSTED chunk.
12 * 4. Once the client posts "done=1", the files are concatenated together.
14 * (More info at: http://firefogg.org/dev/chunk_post.html)
16 class UploadFromChunks
extends UploadBase
{
22 protected $chunkMode; // INIT, CHUNK, DONE
23 protected $sessionKey;
25 protected $fileSize = 0;
32 // Parent class requires this function even though it is only
33 // used from SpecialUpload.php and we don't do chunked uploading
34 // from SpecialUpload -- best to raise an exception for
36 public function initializeFromRequest( &$request ) {
37 throw new MWException( 'not implemented' );
40 public function initialize( $done, $filename, $sessionKey, $path,
41 $fileSize, $sessionData )
43 $this->initFromSessionKey( $sessionKey, $sessionData );
45 if ( !$this->sessionKey
&& !$done ) {
46 // session key not set, init the chunk upload system:
47 $this->chunkMode
= self
::INIT
;
48 $this->mDesiredDestName
= $filename;
49 } else if ( $this->sessionKey
&& !$done ) {
50 $this->chunkMode
= self
::CHUNK
;
51 } else if ( $this->sessionKey
&& $done ) {
52 $this->chunkMode
= self
::DONE
;
54 if ( $this->chunkMode
== self
::CHUNK ||
$this->chunkMode
== self
::DONE
) {
55 $this->mTempPath
= $path;
56 $this->fileSize +
= $fileSize;
61 * Set session information for chunked uploads and allocate a unique key.
62 * @param $comment string
63 * @param $pageText string
64 * @param $watch boolean
66 * @returns string the session key for this chunked upload
68 protected function setupChunkSession( $comment, $pageText, $watch ) {
69 $this->sessionKey
= $this->getSessionKey();
70 $_SESSION['wsUploadData'][$this->sessionKey
] = array(
71 'comment' => $comment,
72 'pageText' => $pageText,
74 'mFilteredName' => $this->mFilteredName
,
76 'mDesiredDestName' => $this->mDesiredDestName
,
77 'version' => self
::SESSION_VERSION
,
79 return $this->sessionKey
;
83 * Initialize a continuation of a chunked upload from a session key
84 * @param $sessionKey string
85 * @param $request WebRequest
89 protected function initFromSessionKey( $sessionKey, $sessionData ) {
90 // testing against null because we don't want to cause obscure
91 // bugs when $sessionKey is full of "0"
92 if ( $sessionKey === null ) {
95 $this->sessionKey
= $sessionKey;
97 if ( isset( $sessionData[$this->sessionKey
]['version'] )
98 && $sessionData[$this->sessionKey
]['version'] == self
::SESSION_VERSION
)
100 $this->comment
= $sessionData[$this->sessionKey
]['comment'];
101 $this->pageText
= $sessionData[$this->sessionKey
]['pageText'];
102 $this->watch
= $sessionData[$this->sessionKey
]['watch'];
103 $this->mFilteredName
= $sessionData[$this->sessionKey
]['mFilteredName'];
104 $this->repoPath
= $sessionData[$this->sessionKey
]['repoPath'];
105 $this->mDesiredDestName
= $sessionData[$this->sessionKey
]['mDesiredDestName'];
107 $this->status
= Status
::newFatal( 'invalid-session-key' );
112 * Handle a chunk of the upload. Overrides the parent method
113 * because Chunked Uploading clients (i.e. Firefogg) require
114 * specific API responses.
115 * @see UploadBase::performUpload
117 public function performUpload( $comment, $pageText, $watch, $user ) {
118 wfDebug( "\n\n\performUpload(chunked): comment:" . $comment . ' pageText: ' . $pageText . ' watch:' . $watch );
119 global $wgUser, $wgOut;
121 if ( $this->chunkMode
== self
::INIT
) {
122 // firefogg expects a specific result per:
123 // http://www.firefogg.org/dev/chunk_post.html
125 // it's okay to return the token here because
126 // a) the user must have requested the token to get here and
127 // b) should only happen over POST
128 // c) we need the token to validate chunks are coming from a non-xss request
129 $token = urlencode( $wgUser->editToken() );
131 echo FormatJson
::encode( array(
132 'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&" .
133 "token={$token}&format=json&enablechunks=true&chunksessionkey=" .
134 $this->setupChunkSession( $comment, $pageText, $watch ) ) );
136 } else if ( $this->chunkMode
== self
::CHUNK
) {
137 $status = $this->appendChunk();
138 if ( !$status->isOK() ) {
142 // firefogg expects a specific result
143 // http://www.firefogg.org/dev/chunk_post.html
145 echo FormatJson
::encode(
146 array( 'result' => 1, 'filesize' => $this->fileSize
)
149 } else if ( $this->chunkMode
== self
::DONE
) {
151 $comment = $this->comment
;
154 $pageText = $this->pageText
;
157 $watch = $this->watch
;
159 $status = parent
::performUpload( $comment, $pageText, $watch, $user );
160 if ( !$status->isGood() ) {
163 $file = $this->getLocalFile();
165 // firefogg expects a specific result
166 // http://www.firefogg.org/dev/chunk_post.html
168 echo FormatJson
::encode( array(
171 'resultUrl' => $file->getDescriptionUrl() )
178 * Append a chunk to the Repo file
180 * @param string $srcPath Path to file to append from
181 * @param string $toAppendPath Path to file to append to
182 * @return Status Status
184 protected function appendToUploadFile( $srcPath, $toAppendPath ) {
185 $repo = RepoGroup
::singleton()->getLocalRepo();
186 $status = $repo->append( $srcPath, $toAppendPath );
191 * Append a chunk to the temporary file.
195 protected function appendChunk() {
196 global $wgMaxUploadSize;
198 if ( !$this->repoPath
) {
199 $this->status
= $this->saveTempUploadedFile( $this->mDesiredDestName
, $this->mTempPath
);
201 if ( $status->isOK() ) {
202 $this->repoPath
= $status->value
;
203 $_SESSION['wsUploadData'][$this->sessionKey
]['repoPath'] = $this->repoPath
;
207 if ( $this->getRealPath( $this->repoPath
) ) {
208 $this->status
= $this->appendToUploadFile( $this->repoPath
, $this->mTempPath
);
210 $this->status
= Status
::newFatal( 'filenotfound', $this->repoPath
);
212 if ( $this->fileSize
> $wgMaxUploadSize )
213 $this->status
= Status
::newFatal( 'largefileserver' );
216 public function verifyUpload() {
217 if ( $this->chunkMode
!= self
::DONE
) {
218 return Status
::newGood();
220 return parent
::verifyUpload();
223 public function checkWarnings() {
224 if ( $this->chunkMode
!= self
::DONE
) {
227 return parent
::checkWarnings();
230 public function getImageInfo( $result ) {
231 if ( $this->chunkMode
!= self
::DONE
) {
234 return parent
::getImageInfo( $result );