Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/conduit/FileUploadChunkConduitAPIMethod.php
12241 views
1
<?php
2
3
final class FileUploadChunkConduitAPIMethod
4
extends FileConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'file.uploadchunk';
8
}
9
10
public function getMethodDescription() {
11
return pht('Upload a chunk of file data to the server.');
12
}
13
14
protected function defineParamTypes() {
15
return array(
16
'filePHID' => 'phid',
17
'byteStart' => 'int',
18
'data' => 'string',
19
'dataEncoding' => 'string',
20
);
21
}
22
23
protected function defineReturnType() {
24
return 'void';
25
}
26
27
protected function execute(ConduitAPIRequest $request) {
28
$viewer = $request->getUser();
29
30
$file_phid = $request->getValue('filePHID');
31
$file = $this->loadFileByPHID($viewer, $file_phid);
32
33
$start = $request->getValue('byteStart');
34
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.'));
45
}
46
$length = strlen($data);
47
48
$chunk = $this->loadFileChunkForUpload(
49
$viewer,
50
$file,
51
$start,
52
$start + $length);
53
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';
62
}
63
64
$params = array(
65
'name' => $file->getMonogram().'.chunk-'.$chunk->getID(),
66
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
67
'chunk' => true,
68
);
69
70
if ($mime_type !== null) {
71
$params['mime-type'] = 'application/octet-stream';
72
}
73
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);
79
80
$chunk->setDataFilePHID($chunk_data->getPHID())->save();
81
82
$needs_update = false;
83
84
$missing = $this->loadAnyMissingChunk($viewer, $file);
85
if (!$missing) {
86
$file->setIsPartial(0);
87
$needs_update = true;
88
}
89
90
if (!$start) {
91
$file->setMimeType($chunk_data->getMimeType());
92
$needs_update = true;
93
}
94
95
if ($needs_update) {
96
$file->save();
97
}
98
99
return null;
100
}
101
102
}
103
104