Exemple #1
0
 /**
  * Stash the file and add the file key, or error information if it fails, to the data.
  *
  * @param string $failureMode What to do on failure to stash:
  *   - When 'critical', use dieStatus() to produce an error response and throw an exception.
  *     Use this when stashing the file was the primary purpose of the API request.
  *   - When 'optional', only add a 'stashfailed' key to the data and return null.
  *     Use this when some error happened for a non-stash upload and we're stashing the file
  *     only to save the client the trouble of re-uploading it.
  * @param array &$data API result to which to add the information
  * @return string|null File key
  */
 private function performStash($failureMode, &$data = null)
 {
     $isPartial = (bool) $this->mParams['chunk'];
     try {
         $status = $this->mUpload->tryStashFile($this->getUser(), $isPartial);
         if ($status->isGood() && !$status->getValue()) {
             // Not actually a 'good' status...
             $status->fatal(new ApiRawMessage('Invalid stashed file', 'stashfailed'));
         }
     } catch (Exception $e) {
         $debugMessage = 'Stashing temporary file failed: ' . get_class($e) . ' ' . $e->getMessage();
         wfDebug(__METHOD__ . ' ' . $debugMessage . "\n");
         $status = Status::newFatal(new ApiRawMessage($e->getMessage(), 'stashfailed'));
     }
     if ($status->isGood()) {
         $stashFile = $status->getValue();
         $data['filekey'] = $stashFile->getFileKey();
         // Backwards compatibility
         $data['sessionkey'] = $data['filekey'];
         return $data['filekey'];
     }
     if ($status->getMessage()->getKey() === 'uploadstash-exception') {
         // The exceptions thrown by upload stash code and pretty silly and UploadBase returns poor
         // Statuses for it. Just extract the exception details and parse them ourselves.
         list($exceptionType, $message) = $status->getMessage()->getParams();
         $debugMessage = 'Stashing temporary file failed: ' . $exceptionType . ' ' . $message;
         wfDebug(__METHOD__ . ' ' . $debugMessage . "\n");
         list($msg, $code) = $this->handleStashException($exceptionType, $message);
         $status = Status::newFatal(new ApiRawMessage($msg, $code));
     }
     // Bad status
     if ($failureMode !== 'optional') {
         $this->dieStatus($status);
     } else {
         list($code, $msg) = $this->getErrorFromStatus($status);
         $data['stashfailed'] = $msg;
         return null;
     }
 }