Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
fileuploader.php
1<?php
2
4
18
20{
21 public function configureActions()
22 {
23 $configureActions = [];
24
25 $configureActions['preview'] = [
26 '-prefilters' => [
27 ActionFilter\Csrf::class,
28 ActionFilter\HttpMethod::class,
29 ],
30 '+prefilters' => [
31 new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_GET]),
32 // new ActionFilter\CheckImageSignature(),
33 ]
34 ];
35
36 $configureActions['download'] = [
37 '-prefilters' => [
38 ActionFilter\Csrf::class,
39 ActionFilter\HttpMethod::class,
40 ],
41 '+prefilters' => [
42 new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_GET]),
43 ]
44 ];
45
46 return $configureActions;
47 }
48
49 protected function getDefaultPreFilters(): array
50 {
51 return [
52 new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_POST]),
53 new ActionFilter\Csrf(),
54 new ActionFilter\CloseSession(),
55 ];
56 }
57
63 protected function getAvailableControllers(): ?array
64 {
65 return null;
66 }
67
68 public function getAutoWiredParameters(): array
69 {
70 $request = $this->getRequest();
71
72 return [
74 UploaderController::class,
75 'controller',
76 function ($className, string $controller, string $controllerOptions = null) {
77 try
78 {
79 $options = empty($controllerOptions) ? [] : Json::decode($controllerOptions);
80 $options = is_array($options) ? $options : [];
81 }
82 catch (\Exception $e)
83 {
84 $options = [];
85 }
86
87 if (is_subclass_of($this, FileUploader::class))
88 {
89 $availableControllers = $this->getAvailableControllers();
90 if ($availableControllers === null)
91 {
92 $this->addError(new Error(
93 'You have to override "getAvailableControllers" method for your custom ajax controller.'
94 ));
95
96 return null;
97 }
98
99 [$moduleId, $className] = ControllerResolver::resolveName($controller);
100 $className = strtolower(ltrim($className, '\\'));
101
102 $availableNames = array_map(function($name) {
103 return strtolower(ltrim($name, '\\'));
104 }, $availableControllers);
105
106 if (
107 !in_array(strtolower($controller), $availableNames)
108 && !in_array($className, $availableNames)
109 )
110 {
111 $this->addError(new Error('Invalid Controller Name.'));
112
113 return null;
114 }
115 }
116
117 return ControllerResolver::createController($controller, $options);
118 }
119 ),
120 new Parameter(
121 Chunk::class,
122 function ($className) use ($request) {
124 if ($result->isSuccess())
125 {
126 return $result->getData()['chunk'];
127 }
128 else
129 {
130 $this->addErrors($result->getErrors());
131
132 return null;
133 }
134 }
135 ),
136 ];
137 }
138
139 public function processBeforeAction(Action $action): bool
140 {
141 $contentLength = (int)$this->getRequest()->getServer()->get('CONTENT_LENGTH');
142 $maxFileSize = min(
143 \CUtil::unformat(ini_get('upload_max_filesize')),
144 \CUtil::unformat(ini_get('post_max_size'))
145 );
146
147 if ($contentLength > $maxFileSize)
148 {
150
151 return false;
152 }
153
154 return parent::processBeforeAction($action);
155 }
156
157 public function uploadAction(UploaderController $controller, Chunk $chunk, string $token = null): array
158 {
159 $uploader = new Uploader($controller);
160 $uploadResult = $uploader->upload($chunk, $token);
161 if ($uploadResult->isSuccess())
162 {
163 return $uploadResult->jsonSerialize();
164 }
165 else
166 {
167 $this->addErrors($uploadResult->getErrors());
168 }
169
170 return [];
171 }
172
173 public function loadAction(UploaderController $controller, array $fileIds): array
174 {
175 $uploader = new Uploader($controller);
176 $loadResults = $uploader->load($fileIds);
177
178 return [
179 'files' => $loadResults,
180 ];
181 }
182
183 public function downloadAction(UploaderController $controller, string $fileId)
184 {
185 $uploader = new Uploader($controller);
186 $loadResults = $uploader->load([$fileId]);
187 $loadResult = $loadResults->getAll()[0] ?? null;
188
189 if ($loadResult === null)
190 {
192 }
193 else if ($loadResult->isSuccess())
194 {
195 $fileId = $loadResult->getFile() ? $loadResult->getFile()->getFileId() : 0;
196 if ($fileId > 0)
197 {
198 return Response\BFile::createByFileId($fileId);
199 }
200 else
201 {
203 }
204 }
205 else
206 {
207 $this->addErrors($loadResult->getErrors());
208 }
209
210 return [];
211 }
212
213 public function previewAction(UploaderController $controller, string $fileId)
214 {
215 $uploader = new Uploader($controller);
216 $loadResults = $uploader->load([$fileId]);
217 $loadResult = $loadResults->getAll()[0] ?? null;
218
219 if ($loadResult === null)
220 {
222 }
223 else if ($loadResult->isSuccess())
224 {
225 $imageId = $loadResult->getFile() ? $loadResult->getFile()->getFileId() : 0;
226 $imageData = \CFile::getFileArray($imageId);
227
228 if (is_array($imageData))
229 {
230 // Sync with \Bitrix\UI\FileUploader\Uploader::getFileInfo
231 $response = new Response\ResizedImage($imageData, 300, 300);
232 $response->setResizeType(BX_RESIZE_IMAGE_PROPORTIONAL);
233 $response->setCacheTime(86400);
234
235 return $response;
236 }
237 else
238 {
240 }
241 }
242 else
243 {
244 $this->addErrors($loadResult->getErrors());
245 }
246
247 return [];
248 }
249
250 public function removeAction(UploaderController $controller, array $fileIds): array
251 {
252 $uploader = new Uploader($controller);
253 $removeResult = $uploader->remove($fileIds);
254
255 return [
256 'files' => $removeResult,
257 ];
258 }
259}
uploadAction(UploaderController $controller, Chunk $chunk, string $token=null)
loadAction(UploaderController $controller, array $fileIds)
previewAction(UploaderController $controller, string $fileId)
removeAction(UploaderController $controller, array $fileIds)
downloadAction(UploaderController $controller, string $fileId)
static createFromRequest(HttpRequest $request)
Definition Chunk.php:30
static resolveName(string $controllerName)