• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / file-upload-control / 3609782379

pending completion
3609782379

push

github

Petr Morávek
v1.4.0

637 of 706 relevant lines covered (90.23%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.37
/src/FileUploadControl/FileUploadControl.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\FileUploadControl;
5

6
use Nepada\FileUploadControl\Responses\ListResponse;
7
use Nepada\FileUploadControl\Responses\Response;
8
use Nepada\FileUploadControl\Responses\UploadErrorResponse;
9
use Nepada\FileUploadControl\Responses\UploadSuccessResponse;
10
use Nepada\FileUploadControl\Storage\ContentRange;
11
use Nepada\FileUploadControl\Storage\FileUploadChunk;
12
use Nepada\FileUploadControl\Storage\FileUploadId;
13
use Nepada\FileUploadControl\Storage\FileUploadItem;
14
use Nepada\FileUploadControl\Storage\FileUploadNotFoundException;
15
use Nepada\FileUploadControl\Storage\Storage;
16
use Nepada\FileUploadControl\Storage\StorageDoesNotExistException;
17
use Nepada\FileUploadControl\Storage\StorageManager;
18
use Nepada\FileUploadControl\Storage\UnableToSaveFileUploadException;
19
use Nepada\FileUploadControl\Storage\UploadNamespace;
20
use Nepada\FileUploadControl\Thumbnail\NullThumbnailProvider;
21
use Nepada\FileUploadControl\Thumbnail\ThumbnailProvider;
22
use Nepada\FileUploadControl\Validation\UploadValidation;
23
use Nette;
24
use Nette\Bridges\ApplicationLatte\Template;
25
use Nette\Forms\Form;
26
use Nette\Http\FileUpload;
27
use Nette\Utils\Html;
28
use Nette\Utils\Strings;
29
use Nextras\FormComponents\Fragments\UIControl\BaseControl;
30

31
class FileUploadControl extends BaseControl
32
{
33

34
    use UploadValidation {
35
        UploadValidation::addRule as private _addRule;
36
    }
37

38
    public const DEFAULT_TEMPLATE_FILE = __DIR__ . '/templates/bootstrap4.latte';
39

40
    private string $templateFile = self::DEFAULT_TEMPLATE_FILE;
41

42
    private StorageManager $storageManager;
43

44
    private ThumbnailProvider $thumbnailProvider;
45

46
    private bool $httpDataLoaded = false;
47

48
    /**
49
     * @param StorageManager $storageManager
50
     * @param string|Html|null $caption
51
     */
52
    public function __construct(StorageManager $storageManager, $caption = null)
1✔
53
    {
54
        parent::__construct($caption);
1✔
55
        $this->storageManager = $storageManager;
1✔
56
        $this->thumbnailProvider = new NullThumbnailProvider();
1✔
57
        $this->control = Html::el();
1✔
58
        $this->setOption('type', 'file-upload');
1✔
59
        $this->addComponent(new Nette\Forms\Controls\UploadControl($caption, true), 'upload');
1✔
60
        $this->addComponent(new Nette\Forms\Controls\HiddenField(), 'namespace');
1✔
61
        $this->initializeValidation($this);
1✔
62
    }
1✔
63

64
    public function setThumbnailProvider(ThumbnailProvider $thumbnailProvider): void
1✔
65
    {
66
        $this->thumbnailProvider = $thumbnailProvider;
1✔
67
    }
1✔
68

69
    public function setTemplateFile(string $templateFile): void
1✔
70
    {
71
        $this->templateFile = $templateFile;
1✔
72
    }
1✔
73

74
    /**
75
     * @throws Nette\Application\BadRequestException
76
     */
77
    public function loadHttpData(): void
78
    {
79
        $this->getNamespaceControl()->loadHttpData();
1✔
80
        try {
81
            $storage = $this->getStorage();
1✔
82
        } catch (StorageDoesNotExistException $exception) {
×
83
            // refresh namespace
84
            $this->setUploadNamespace($this->storageManager->createNewNamespace());
×
85
            try {
86
                $storage = $this->getStorage();
×
87
            } catch (StorageDoesNotExistException $exception) {
×
88
                throw new \LogicException($exception->getMessage(), 0, $exception);
×
89
            }
90
        }
91

92
        if ($this->httpDataLoaded || $this->isDisabled()) {
1✔
93
            return;
×
94
        }
95

96
        $this->httpDataLoaded = true;
1✔
97

98
        $fileUploadChunks = $this->getFileUploadChunks();
1✔
99
        if (count($fileUploadChunks) === 0) {
1✔
100
            return;
1✔
101
        }
102

103
        $uploadFailed = false;
1✔
104
        foreach ($fileUploadChunks as $fileUploadChunk) {
1✔
105
            try {
106
                $storage->save($fileUploadChunk);
1✔
107
            } catch (UnableToSaveFileUploadException $exception) {
×
108
                $uploadFailed = true;
×
109
            }
110
        }
111
        if ($uploadFailed) {
1✔
112
            $this->addError('Upload error');
×
113
        }
114
    }
1✔
115

116
    /**
117
     * @return FileUpload[]
118
     */
119
    public function getValue(): array
120
    {
121
        if ($this->isDisabled()) {
1✔
122
            return [];
1✔
123
        }
124

125
        try {
126
            return array_map(fn (FileUploadItem $fileUploadItem): FileUpload => $fileUploadItem->getFileUpload(), $this->getStorage()->list());
1✔
127
        } catch (StorageDoesNotExistException $exception) {
×
128
            throw new \LogicException($exception->getMessage(), 0, $exception);
×
129
        }
130
    }
131

132
    /**
133
     * @param mixed $value
134
     * @return static
135
     * @internal
136
     */
137
    public function setValue($value): self
138
    {
139
        return $this;
1✔
140
    }
141

142
    public function isDisabled(): bool
143
    {
144
        return $this->getUploadControl()->isDisabled();
1✔
145
    }
146

147
    /**
148
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
149
     * @param bool $value
150
     * @return static
151
     * @throws Nette\Application\BadRequestException
152
     */
153
    public function setDisabled($value = true): self
1✔
154
    {
155
        $this->getUploadControl()->setDisabled($value);
1✔
156
        if (! $value) {
1✔
157
            return $this;
×
158
        }
159

160
        $form = $this->getForm(false);
1✔
161
        if ($form !== null && $form->isAnchored() && (bool) $form->isSubmitted()) {
1✔
162
            $this->loadHttpData();
×
163
        }
164

165
        return $this;
1✔
166
    }
167

168
    public function getControlPart(?string $key = null): ?Html
1✔
169
    {
170
        if ($key === 'namespace') {
1✔
171
            return $this->getNamespaceControl()->getControl();
1✔
172
        }
173

174
        if ($key === 'upload') {
1✔
175
            $control = $this->getUploadControl()->getControl();
1✔
176
            assert($control instanceof Html);
1✔
177
            $control->{'data-nette-rules'} = Nette\Forms\Helpers::exportRules($this->getRules());
1✔
178
            return $control;
1✔
179
        }
180

181
        return $this->getControl();
×
182
    }
183

184
    public function getControl(): Html
185
    {
186
        $this->setOption('rendered', true);
1✔
187
        $control = clone $this->control;
1✔
188

189
        $template = $this->getTemplate();
1✔
190
        assert($template instanceof Template);
1✔
191

192
        try {
193
            $storage = $this->getStorage();
1✔
194
        } catch (StorageDoesNotExistException $exception) {
×
195
            throw new \LogicException($exception->getMessage(), 0, $exception);
×
196
        }
197
        $fileUploadItems = $storage->list();
1✔
198
        $uniqueFilenames = array_map(fn (FileUploadItem $fileUploadItem): string => $fileUploadItem->getFileUpload()->getUntrustedName(), $fileUploadItems);
1✔
199
        $completedFiles = array_map(
1✔
200
            fn (FileUploadItem $fileUploadItem): Response => $this->createUploadSuccessResponse($fileUploadItem),
1✔
201
            array_filter($fileUploadItems, fn (FileUploadItem $fileUploadItem): bool => $fileUploadItem->getFileUpload()->isOk()),
1✔
202
        );
203

204
        $template->uploadUrl = $this->link('upload!', ['namespace' => $this->getUploadNamespace()->toString()]);
1✔
205
        $template->uniqueFilenames = $uniqueFilenames;
1✔
206
        $template->completedFiles = $completedFiles;
1✔
207

208
        $controlHtml = Nette\Utils\Helpers::capture(function () use ($template): void {
1✔
209
            $template->render($this->templateFile);
1✔
210
        });
1✔
211
        return $control->addHtml($controlHtml);
1✔
212
    }
213

214
    public function getLabelPrototype(): Html
215
    {
216
        return $this->getUploadControl()->getLabelPrototype();
1✔
217
    }
218

219
    /**
220
     * @param string|Html|null $caption
221
     * @return Html
222
     */
223
    public function getLabel($caption = null): Html
1✔
224
    {
225
        $label = $this->getUploadControl()->getLabel($caption);
1✔
226
        assert($label instanceof Html);
1✔
227
        return $label;
1✔
228
    }
229

230
    /**
231
     * @param callable|string $validator
232
     * @param string|Html|null $errorMessage
233
     * @param mixed $arg
234
     * @return static
235
     */
236
    public function addRule($validator, $errorMessage = null, $arg = null): self
1✔
237
    {
238
        if ($validator === Form::IMAGE) {
1✔
239
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', FileUpload::IMAGE_MIME_TYPES));
1✔
240
        } elseif ($validator === Form::MIME_TYPE) {
1✔
241
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', (array) $arg));
1✔
242
        }
243

244
        return $this->_addRule($validator, $errorMessage, $arg);
1✔
245
    }
246

247
    /**
248
     * @param string $namespace
249
     * @throws Nette\Application\BadRequestException
250
     */
251
    public function handleUpload(string $namespace): void
1✔
252
    {
253
        $uploadNamespace = $this->parseUploadNamespace($namespace);
1✔
254
        $this->setUploadNamespace($uploadNamespace);
1✔
255

256
        $fileUploadChunks = $this->getFileUploadChunks();
1✔
257
        $responses = [];
1✔
258
        foreach ($fileUploadChunks as $fileUploadChunk) {
1✔
259
            if ($this->isDisabled()) {
1✔
260
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate('Upload disabled'));
1✔
261
                continue;
1✔
262
            }
263

264
            $fakeFileUpload = new FileUpload([
1✔
265
                'name' => $fileUploadChunk->getFileUpload()->getUntrustedName(),
1✔
266
                'size' => $fileUploadChunk->getContentRange()->getSize(),
1✔
267
                'tmp_name' => $fileUploadChunk->getFileUpload()->getTemporaryFile(),
1✔
268
                'error' => UPLOAD_ERR_OK,
1✔
269
            ]);
270
            $this->fakeUploadControl->setNewFileUpload($fakeFileUpload);
1✔
271
            $this->validate();
1✔
272
            $error = $this->getError();
1✔
273
            if ($error !== null) {
1✔
274
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $error);
1✔
275
                continue;
1✔
276
            }
277

278
            try {
279
                $fileUploadItem = $this->getStorage()->save($fileUploadChunk);
1✔
280
                $responses[] = $this->createUploadSuccessResponse($fileUploadItem);
1✔
281
            } catch (StorageDoesNotExistException | UnableToSaveFileUploadException $exception) {
×
282
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate('Upload error'));
×
283
            }
284
        }
285

286
        $this->sendJson(new ListResponse(...$responses));
1✔
287
    }
288

289
    /**
290
     * @param string $namespace
291
     * @param string $id
292
     * @throws Nette\Application\BadRequestException
293
     */
294
    public function handleDelete(string $namespace, string $id): void
295
    {
296
        $fileUploadId = $this->parseFileUploadId($id);
×
297
        $uploadNamespace = $this->parseUploadNamespace($namespace);
×
298
        $this->setUploadNamespace($uploadNamespace);
×
299

300
        try {
301
            $this->getStorage()->delete($fileUploadId);
×
302
        } catch (StorageDoesNotExistException $exception) {
×
303
            // noop
304
        }
305
        $this->sendJson('');
×
306
    }
307

308
    /**
309
     * @param string $namespace
310
     * @param string $id
311
     * @throws Nette\Application\BadRequestException
312
     */
313
    public function handleDownload(string $namespace, string $id): void
1✔
314
    {
315
        $fileUploadId = $this->parseFileUploadId($id);
1✔
316
        $uploadNamespace = $this->parseUploadNamespace($namespace);
1✔
317
        $this->setUploadNamespace($uploadNamespace);
1✔
318

319
        try {
320
            $fileUploadItem = $this->getStorage()->load($fileUploadId);
1✔
321
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
322
            throw new Nette\Application\BadRequestException('File upload not found.', 0, $exception);
×
323
        }
324

325
        $fileUpload = $fileUploadItem->getFileUpload();
1✔
326
        $response = new Nette\Application\Responses\FileResponse(
1✔
327
            $fileUpload->getTemporaryFile(),
1✔
328
            $fileUpload->getUntrustedName(),
1✔
329
        );
330
        $this->getPresenter()->sendResponse($response);
1✔
331
    }
332

333
    /**
334
     * @param string $namespace
335
     * @param string $id
336
     * @throws Nette\Application\BadRequestException
337
     */
338
    public function handleThumbnail(string $namespace, string $id): void
339
    {
340
        $fileUploadId = $this->parseFileUploadId($id);
×
341
        $uploadNamespace = $this->parseUploadNamespace($namespace);
×
342
        $this->setUploadNamespace($uploadNamespace);
×
343

344
        try {
345
            $fileUpload = $this->getStorage()->load($fileUploadId)->getFileUpload();
×
346
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
347
            throw new Nette\Application\BadRequestException('Source file for thumbnail not found.', 0, $exception);
×
348
        }
349

350
        if (! $this->thumbnailProvider->isSupported($fileUpload)) {
×
351
            throw new Nette\Application\BadRequestException('Thumbnail could not be generated');
×
352
        }
353

354
        $response = $this->thumbnailProvider->createThumbnail($fileUpload);
×
355
        $this->getPresenter()->sendResponse($response);
×
356
    }
357

358
    protected function createTemplate(): Template
359
    {
360
        $template = parent::createTemplate();
1✔
361
        assert($template instanceof Template);
1✔
362

363
        $translator = $this->getTranslator();
1✔
364
        if ($translator !== null) {
1✔
365
            $template->setTranslator($translator);
1✔
366
        }
367

368
        $template->getLatte()->addFilter('json', fn ($data): string => Nette\Utils\Json::encode($data));
1✔
369

370
        return $template;
1✔
371
    }
372

373
    protected function getHttpRequest(): Nette\Http\IRequest
374
    {
375
        return $this->getPresenter()->getHttpRequest();
1✔
376
    }
377

378
    /**
379
     * Sends back JSON response.
380
     * Sets the right content type based on the support on the other end.
381
     * https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#wiki-content-type-negotiation
382
     *
383
     * @param mixed $data
384
     */
385
    protected function sendJson($data): void
386
    {
387
        $contentType = Strings::contains((string) $this->getHttpRequest()->getHeader('accept'), 'application/json') ? 'application/json' : 'text/plain';
1✔
388
        $response = new Nette\Application\Responses\JsonResponse($data, $contentType);
1✔
389
        $this->getPresenter()->sendResponse($response);
1✔
390
    }
391

392
    protected function getUploadControl(): Nette\Forms\Controls\UploadControl
393
    {
394
        return $this->getComponent('upload');
1✔
395
    }
396

397
    protected function getNamespaceControl(): Nette\Forms\Controls\HiddenField
398
    {
399
        return $this->getComponent('namespace');
1✔
400
    }
401

402
    protected function getThumbnailProvider(): ThumbnailProvider
403
    {
404
        return $this->thumbnailProvider;
×
405
    }
406

407
    protected function getUploadNamespace(): UploadNamespace
408
    {
409
        $nameSpaceValue = (string) $this->getNamespaceControl()->getValue();
1✔
410
        if (UploadNamespace::isValid($nameSpaceValue)) {
1✔
411
            return UploadNamespace::fromString($nameSpaceValue);
1✔
412
        }
413

414
        $namespace = $this->storageManager->createNewNamespace();
×
415
        $this->setUploadNamespace($namespace);
×
416
        return $namespace;
×
417
    }
418

419
    protected function setUploadNamespace(UploadNamespace $namespace): void
1✔
420
    {
421
        $this->getNamespaceControl()->setValue($namespace->toString());
1✔
422
    }
1✔
423

424
    /**
425
     * @return Storage
426
     * @throws StorageDoesNotExistException
427
     */
428
    protected function getStorage(): Storage
429
    {
430
        return $this->storageManager->getStorage($this->getUploadNamespace());
1✔
431
    }
432

433
    protected function createUploadSuccessResponse(FileUploadItem $fileUploadItem): Response
1✔
434
    {
435
        $fileUpload = $fileUploadItem->getFileUpload();
1✔
436
        $idValue = $fileUploadItem->getId()->toString();
1✔
437
        $namespaceValue = $this->getUploadNamespace()->toString();
1✔
438
        return new UploadSuccessResponse(
1✔
439
            $fileUpload->getUntrustedName(),
1✔
440
            $fileUpload->getSize(),
1✔
441
            $fileUpload->getContentType(),
1✔
442
            $this->link('download!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
443
            $this->link('delete!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
444
            $this->thumbnailProvider->isSupported($fileUpload) ? $this->link('thumbnail!', ['namespace' => $namespaceValue, 'id' => $idValue]) : null,
1✔
445
        );
446
    }
447

448
    protected function createUploadErrorResponse(FileUploadChunk $fileUploadChunk, string $error): Response
1✔
449
    {
450
        return new UploadErrorResponse(
1✔
451
            $fileUploadChunk->getFileUpload()->getUntrustedName(),
1✔
452
            $fileUploadChunk->getContentRange()->getSize(),
1✔
453
            $error,
454
        );
455
    }
456

457
    /**
458
     * @param string $value
459
     * @return UploadNamespace
460
     * @throws Nette\Application\BadRequestException
461
     */
462
    protected function parseUploadNamespace(string $value): UploadNamespace
1✔
463
    {
464
        if (! UploadNamespace::isValid($value)) {
1✔
465
            throw new Nette\Application\BadRequestException('Invalid namespace value', 400);
×
466
        }
467
        return UploadNamespace::fromString($value);
1✔
468
    }
469

470
    /**
471
     * @param string $value
472
     * @return FileUploadId
473
     * @throws Nette\Application\BadRequestException
474
     */
475
    protected function parseFileUploadId(string $value): FileUploadId
1✔
476
    {
477
        if (! FileUploadId::isValid($value)) {
1✔
478
            throw new Nette\Application\BadRequestException('Invalid file upload id value', 400);
×
479
        }
480
        return FileUploadId::fromString($value);
1✔
481
    }
482

483
    /**
484
     * @return FileUploadChunk[]
485
     * @throws Nette\Application\BadRequestException
486
     */
487
    protected function getFileUploadChunks(): array
488
    {
489
        $httpRequest = $this->getHttpRequest();
1✔
490
        /** @var array<int, FileUpload> $files */
491
        $files = Nette\Forms\Helpers::extractHttpData($httpRequest->getFiles(), $this->getUploadControl()->getHtmlName() . '[]', Form::DATA_FILE);
1✔
492
        if (count($files) === 0) {
1✔
493
            return [];
1✔
494
        }
495

496
        $contentRangeHeaderValue = $httpRequest->getHeader('content-range');
1✔
497
        if ($contentRangeHeaderValue !== null) {
1✔
498
            if (count($files) > 1) {
1✔
499
                throw new Nette\Application\BadRequestException('Chunk upload does not support multi-file upload', 400);
×
500
            }
501
            try {
502
                $contentRange = ContentRange::fromHttpHeaderValue($contentRangeHeaderValue);
1✔
503
                $fileUploadChunk = FileUploadChunk::partialUpload(reset($files), $contentRange);
1✔
504
                return [$fileUploadChunk];
1✔
505
            } catch (\Throwable $exception) {
×
506
                throw new Nette\Application\BadRequestException('Invalid content-range header value', 400, $exception);
×
507
            }
508
        }
509

510
        /** @var FileUploadChunk[] $fileUploadChunks */
511
        $fileUploadChunks = [];
1✔
512
        foreach ($files as $file) {
1✔
513
            $fileUploadChunks[] = FileUploadChunk::completeUpload($file);
1✔
514
        }
515

516
        return $fileUploadChunks;
1✔
517
    }
518

519
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc