Path: blob/master/src/applications/files/conduit/FileUploadChunkConduitAPIMethod.php
12241 views
<?php12final class FileUploadChunkConduitAPIMethod3extends FileConduitAPIMethod {45public function getAPIMethodName() {6return 'file.uploadchunk';7}89public function getMethodDescription() {10return pht('Upload a chunk of file data to the server.');11}1213protected function defineParamTypes() {14return array(15'filePHID' => 'phid',16'byteStart' => 'int',17'data' => 'string',18'dataEncoding' => 'string',19);20}2122protected function defineReturnType() {23return 'void';24}2526protected function execute(ConduitAPIRequest $request) {27$viewer = $request->getUser();2829$file_phid = $request->getValue('filePHID');30$file = $this->loadFileByPHID($viewer, $file_phid);3132$start = $request->getValue('byteStart');3334$data = $request->getValue('data');35$encoding = $request->getValue('dataEncoding');36switch ($encoding) {37case 'base64':38$data = $this->decodeBase64($data);39break;40case null:41break;42default:43throw new Exception(pht('Unsupported data encoding.'));44}45$length = strlen($data);4647$chunk = $this->loadFileChunkForUpload(48$viewer,49$file,50$start,51$start + $length);5253// If this is the initial chunk, leave the MIME type unset so we detect54// it and can update the parent file. If this is any other chunk, it has55// no meaningful MIME type. Provide a default type so we can avoid writing56// it to disk to perform MIME type detection.57if (!$start) {58$mime_type = null;59} else {60$mime_type = 'application/octet-stream';61}6263$params = array(64'name' => $file->getMonogram().'.chunk-'.$chunk->getID(),65'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,66'chunk' => true,67);6869if ($mime_type !== null) {70$params['mime-type'] = 'application/octet-stream';71}7273// NOTE: These files have a view policy which prevents normal access. They74// are only accessed through the storage engine.75$chunk_data = PhabricatorFile::newFromFileData(76$data,77$params);7879$chunk->setDataFilePHID($chunk_data->getPHID())->save();8081$needs_update = false;8283$missing = $this->loadAnyMissingChunk($viewer, $file);84if (!$missing) {85$file->setIsPartial(0);86$needs_update = true;87}8889if (!$start) {90$file->setMimeType($chunk_data->getMimeType());91$needs_update = true;92}9394if ($needs_update) {95$file->save();96}9798return null;99}100101}102103104