Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / files / conduit / FileUploadChunkConduitAPIMethod.php
blobeab46c4992b9d9d1dad96c72ba7305d4a3d16a45
1 <?php
3 final class FileUploadChunkConduitAPIMethod
4 extends FileConduitAPIMethod {
6 public function getAPIMethodName() {
7 return 'file.uploadchunk';
10 public function getMethodDescription() {
11 return pht('Upload a chunk of file data to the server.');
14 protected function defineParamTypes() {
15 return array(
16 'filePHID' => 'phid',
17 'byteStart' => 'int',
18 'data' => 'string',
19 'dataEncoding' => 'string',
23 protected function defineReturnType() {
24 return 'void';
27 protected function execute(ConduitAPIRequest $request) {
28 $viewer = $request->getUser();
30 $file_phid = $request->getValue('filePHID');
31 $file = $this->loadFileByPHID($viewer, $file_phid);
33 $start = $request->getValue('byteStart');
35 $data = $request->getValue('data');
36 $encoding = $request->getValue('dataEncoding');
37 switch ($encoding) {
38 case 'base64':
39 $data = $this->decodeBase64($data);
40 break;
41 case null:
42 break;
43 default:
44 throw new Exception(pht('Unsupported data encoding.'));
46 $length = strlen($data);
48 $chunk = $this->loadFileChunkForUpload(
49 $viewer,
50 $file,
51 $start,
52 $start + $length);
54 // If this is the initial chunk, leave the MIME type unset so we detect
55 // it and can update the parent file. If this is any other chunk, it has
56 // no meaningful MIME type. Provide a default type so we can avoid writing
57 // it to disk to perform MIME type detection.
58 if (!$start) {
59 $mime_type = null;
60 } else {
61 $mime_type = 'application/octet-stream';
64 $params = array(
65 'name' => $file->getMonogram().'.chunk-'.$chunk->getID(),
66 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
67 'chunk' => true,
70 if ($mime_type !== null) {
71 $params['mime-type'] = 'application/octet-stream';
74 // NOTE: These files have a view policy which prevents normal access. They
75 // are only accessed through the storage engine.
76 $chunk_data = PhabricatorFile::newFromFileData(
77 $data,
78 $params);
80 $chunk->setDataFilePHID($chunk_data->getPHID())->save();
82 $needs_update = false;
84 $missing = $this->loadAnyMissingChunk($viewer, $file);
85 if (!$missing) {
86 $file->setIsPartial(0);
87 $needs_update = true;
90 if (!$start) {
91 $file->setMimeType($chunk_data->getMimeType());
92 $needs_update = true;
95 if ($needs_update) {
96 $file->save();
99 return null;