8 * first destination checks are made (if ignorewarnings is not checked) errors / warning is returned.
10 * we return the uploadUrl
11 * we then accept chunk uploads from the client.
12 * return chunk id on each POSTED chunk
13 * once the client posts done=1 concatenated the files together.
14 * more info at: http://firefogg.org/dev/chunk_post.html
16 class UploadFromChunks
extends UploadBase
{
18 var $chunk_mode; // init, chunk, done
19 var $mSessionKey = false;
20 var $status = array();
25 public function initializeFromRequest( &$request ){
26 //should merge initializeFromParams (but just needs to be working atm)
28 public function initializeFromParams( $param, &$request ) {
29 $this->initFromSessionKey( $param['chunksessionkey'], $request );
30 // set the chunk mode:
31 if( !$this->mSessionKey
&& !$param['done'] ){
32 // session key not set init the chunk upload system:
33 $this->chunk_mode
= UploadFromChunks
::INIT
;
34 $this->mDesiredDestName
= $param['filename'];
35 } else if( $this->mSessionKey
&& !$param['done'] ){
36 // this is a chunk piece
37 $this->chunk_mode
= UploadFromChunks
::CHUNK
;
38 } else if( $this->mSessionKey
&& $param['done'] ){
39 // this is the last chunk
40 $this->chunk_mode
= UploadFromChunks
::DONE
;
42 if( $this->chunk_mode
== UploadFromChunks
::CHUNK ||
43 $this->chunk_mode
== UploadFromChunks
::DONE
){
44 // set chunk related vars:
45 $this->mTempPath
= $request->getFileTempName( 'chunk' );
46 $this->mFileSize
= $request->getFileSize( 'chunk' );
51 static function isValidRequest( $request ) {
52 $sessionData = $request->getSessionData( 'wsUploadData' );
53 if( !self
::isValidSessionKey(
54 $request->getInt( 'wpSessionKey' ),
57 // check for the file:
58 return (bool)$request->getFileTempName( 'file' );
61 /* check warnings depending on chunk_mode */
62 function checkWarnings(){
67 function isEmptyFile(){
68 // does not apply to chunk init
69 if( $this->chunk_mode
== UploadFromChunks
::INIT
){
72 return parent
::isEmptyFile();
77 * Verify whether the upload is sane.
78 * Returns self::OK or else an array with error information
80 function verifyUpload() {
81 // no checks on chunk upload mode:
82 if( $this->chunk_mode
== UploadFromChunks
::INIT
)
83 return array( 'status' => self
::OK
);
85 // verify on init and last chunk request
86 if( $this->chunk_mode
== UploadFromChunks
::CHUNK ||
87 $this->chunk_mode
== UploadFromChunks
::DONE
)
88 return parent
::verifyUpload();
91 // only run verifyFile on completed uploaded chunks
92 function verifyFile(){
93 if( $this->chunk_mode
== UploadFromChunks
::DONE
){
94 // first append last chunk (so we can do a real verifyFile check... (check file type etc)
95 $status = $this->doChunkAppend();
96 if( $status->isOK() ){
97 $this->mTempPath
= $this->getRealPath( $this->mTempAppendPath
);
98 // verify the completed merged chunks as if it was the file that got uploaded:
99 return parent
::verifyFile( $this->mTempPath
);
101 // conflict of status returns (have to return the error ary) ... why we don't consistantly use a status object is beyond me..
102 return $status->getErrorsArray();
109 // pretty ugly inter-mixing of mParam and local vars
110 function setupChunkSession( $summary, $comment, $watch ) {
111 $this->mSessionKey
= $this->getSessionKey();
112 $_SESSION['wsUploadData'][$this->mSessionKey
] = array(
113 'mComment' => $comment,
114 'mSummary' => $summary,
116 'mIgnorewarnings' => true, //ignore warning on chunk uploads (for now)
117 'mFilteredName' => $this->mFilteredName
,
118 'mTempAppendPath' => null, // the repo append path (not temporary local node mTempPath)
119 'mDesiredDestName' => $this->mDesiredDestName
,
120 'version' => self
::SESSION_VERSION
,
122 return $this->mSessionKey
;
125 function initFromSessionKey( $sessionKey, $request ){
126 if( !$sessionKey ||
empty( $sessionKey ) ){
129 $this->mSessionKey
= $sessionKey;
130 // load the sessionData array:
131 $sessionData = $request->getSessionData( 'wsUploadData' );
133 if( isset( $sessionData[$this->mSessionKey
]['version'] ) &&
134 $sessionData[$this->mSessionKey
]['version'] == self
::SESSION_VERSION
) {
135 // update the local object from the session
136 $this->mComment
= $sessionData[$this->mSessionKey
]['mComment'];
137 $this->mSummary
= $sessionData[$this->mSessionKey
]['mSummary'];
138 $this->mWatch
= $sessionData[$this->mSessionKey
]['mWatch'];
139 $this->mIgnorewarnings
= $sessionData[$this->mSessionKey
]['mIgnorewarnings'];
140 $this->mFilteredName
= $sessionData[$this->mSessionKey
]['mFilteredName'];
141 $this->mTempAppendPath
= $sessionData[$this->mSessionKey
]['mTempAppendPath'];
142 $this->mDesiredDestName
= $sessionData[$this->mSessionKey
]['mDesiredDestName'];
144 $this->status
= array( 'error' => 'missing session data' );
149 // Lets us return an api result (as flow for chunk uploads is kind of different than others.
150 function performUpload( $summary = '', $comment = '', $watch = '', $user ){
153 if( $this->chunk_mode
== UploadFromChunks
::INIT
){
154 // firefogg expects a specific result per:
155 // http://www.firefogg.org/dev/chunk_post.html
157 // it's okay to return the token here because
158 // a) the user must have requested the token to get here and
159 // b) should only happen over POST
160 // c) (we need the token to validate chunks are coming from a non-xss request)
161 $token = urlencode( $wgUser->editToken() );
163 echo FormatJson
::encode( array(
164 'uploadUrl' => wfExpandUrl( wfScript( 'api' ) ) . "?action=upload&".
165 "token={$token}&format=json&enablechunks=true&chunksessionkey=".
166 $this->setupChunkSession( $summary, $comment, $watch ) ) );
168 } else if( $this->chunk_mode
== UploadFromChunks
::CHUNK
){
169 $status = $this->doChunkAppend();
170 if( $status->isOK() ){
172 // firefogg expects a specific result per:
173 // http://www.firefogg.org/dev/chunk_post.html
175 echo FormatJson
::encode( array(
177 'filesize' => filesize( $this->getRealPath( $this->mTempAppendPath
) )
187 } else if( $this->chunk_mode
== UploadFromChunks
::DONE
){
188 // update the values from the local (session init) if not paseed again)
190 $summary = $this->mSummary
;
193 $comment = $this->mComment
;
196 $watch = $this->mWatch
;
197 $status = parent
::performUpload( $summary, $comment, $watch, $user );
198 if( !$status->isGood() ) {
201 $file = $this->getLocalFile();
202 // firefogg expects a specific result per:
203 // http://www.firefogg.org/dev/chunk_post.html
205 echo FormatJson
::encode( array(
208 'resultUrl' => $file->getDescriptionUrl()
216 // append the given chunk to the temporary uploaded file. (if no temporary uploaded file exists created it.
217 function doChunkAppend(){
218 global $wgMaxUploadSize;
219 // if we don't have a mTempAppendPath to generate a file from the chunk packaged var:
220 if( !$this->mTempAppendPath
){
222 // make a chunk store path. (append tmp file to chunk)
223 $status = $this->saveTempUploadedFile( $this->mDestName
, $this->mTempPath
);
225 if( $status->isOK() ) {
226 $this->mTempAppendPath
= $status->value
;
227 $_SESSION['wsUploadData'][$this->mSessionKey
]['mTempAppendPath'] = $this->mTempAppendPath
;
231 if( is_file( $this->getRealPath( $this->mTempAppendPath
) ) ){
232 $status = $this->appendToUploadFile( $this->mTempAppendPath
, $this->mTempPath
);
234 $status = Status
::newFatal( 'filenotfound', $this->mTempAppendPath
);
236 //check to make sure we have not expanded beyond $wgMaxUploadSize
237 if( filesize( $this->getRealPath( $this->mTempAppendPath
) ) > $wgMaxUploadSize )
238 $status = Status
::newFatal( 'largefileserver' );