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

nepada / file-upload-control / 9738314049

01 Jul 2024 04:43AM UTC coverage: 90.293%. Remained the same
9738314049

Pull #158

github

web-flow
Merge bbbdee319 into cb835c645
Pull Request #158: Bump the phpstan group with 2 updates

679 of 752 relevant lines covered (90.29%)

0.9 hits per line

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

80.87
/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\FailedUpload;
12
use Nepada\FileUploadControl\Storage\FileUploadChunk;
13
use Nepada\FileUploadControl\Storage\FileUploadId;
14
use Nepada\FileUploadControl\Storage\FileUploadItem;
15
use Nepada\FileUploadControl\Storage\FileUploadNotFoundException;
16
use Nepada\FileUploadControl\Storage\Storage;
17
use Nepada\FileUploadControl\Storage\StorageDoesNotExistException;
18
use Nepada\FileUploadControl\Storage\StorageManager;
19
use Nepada\FileUploadControl\Storage\UnableToSaveFileUploadException;
20
use Nepada\FileUploadControl\Storage\UploadNamespace;
21
use Nepada\FileUploadControl\Thumbnail\NullThumbnailProvider;
22
use Nepada\FileUploadControl\Thumbnail\ThumbnailProvider;
23
use Nepada\FileUploadControl\Validation\ClientSide;
24
use Nepada\FileUploadControl\Validation\FakeUploadControl;
25
use Nepada\FileUploadControl\Validation\UploadValidation;
26
use Nette;
27
use Nette\Bridges\ApplicationLatte\Template;
28
use Nette\Forms\Form;
29
use Nette\Http\FileUpload;
30
use Nette\Utils\Arrays;
31
use Nette\Utils\Html;
32
use Nette\Utils\Strings;
33
use Nextras\FormComponents\Fragments\UIControl\BaseControl;
34

35
class FileUploadControl extends BaseControl
36
{
37

38
    use UploadValidation {
39
        UploadValidation::addRule as private _addRule;
40
    }
41

42
    final public const TEMPLATE_FILE_BOOTSTRAP4 = __DIR__ . '/templates/bootstrap4.latte';
43
    final public const TEMPLATE_FILE_BOOTSTRAP5 = __DIR__ . '/templates/bootstrap5.latte';
44
    final public const DEFAULT_TEMPLATE_FILE = self::TEMPLATE_FILE_BOOTSTRAP4;
45

46
    private string $templateFile = self::DEFAULT_TEMPLATE_FILE;
47

48
    private StorageManager $storageManager;
49

50
    private ThumbnailProvider $thumbnailProvider;
51

52
    private bool $httpDataLoaded = false;
53

54
    public function __construct(StorageManager $storageManager, string|\Stringable|null $caption = null)
1✔
55
    {
56
        parent::__construct($caption);
1✔
57
        $this->storageManager = $storageManager;
1✔
58
        $this->thumbnailProvider = new NullThumbnailProvider();
1✔
59
        $this->control = Html::el();
1✔
60
        $this->setOption('type', 'file-upload');
1✔
61
        $this->addComponent(new Nette\Forms\Controls\UploadControl($caption, true), 'upload');
1✔
62
        $this->addComponent(new Nette\Forms\Controls\HiddenField(), 'namespace');
1✔
63
        $this->initializeValidation($this);
1✔
64
        $this->addCondition(fn () => $this->getPresenterIfExists()?->isSignalReceiver($this, 'upload') !== true) // disable during file upload via signal
1✔
65
            ->addRule($this->validateUploadSuccess(...), Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::VALID]);
1✔
66
        $this->addRule(ClientSide::NO_UPLOAD_IN_PROGRESS, 'File upload is still in progress - wait until it is finished, or abort it.');
1✔
67
    }
1✔
68

69
    private function validateUploadSuccess(FakeUploadControl $control): bool
1✔
70
    {
71
        return Arrays::every($control->getValue(), fn (FileUpload $upload): bool => $upload->isOk());
1✔
72
    }
73

74
    public function setThumbnailProvider(ThumbnailProvider $thumbnailProvider): void
1✔
75
    {
76
        $this->thumbnailProvider = $thumbnailProvider;
1✔
77
    }
1✔
78

79
    public function setTemplateFile(string $templateFile): void
1✔
80
    {
81
        $this->templateFile = $templateFile;
1✔
82
    }
1✔
83

84
    /**
85
     * @throws Nette\Application\BadRequestException
86
     */
87
    public function loadHttpData(): void
88
    {
89
        $this->getNamespaceControl()->loadHttpData();
1✔
90
        try {
91
            $storage = $this->getStorage();
1✔
92
        } catch (StorageDoesNotExistException $exception) {
×
93
            // refresh namespace
94
            $this->setUploadNamespace($this->storageManager->createNewNamespace());
×
95
            try {
96
                $storage = $this->getStorage();
×
97
            } catch (StorageDoesNotExistException $exception) {
×
98
                throw new \LogicException($exception->getMessage(), 0, $exception);
×
99
            }
100
        }
101

102
        if ($this->httpDataLoaded || $this->isDisabled()) {
1✔
103
            return;
×
104
        }
105

106
        $this->httpDataLoaded = true;
1✔
107

108
        $fileUploadChunks = $this->getFileUploadChunks();
1✔
109
        if (count($fileUploadChunks) === 0) {
1✔
110
            return;
1✔
111
        }
112

113
        $uploadFailed = false;
1✔
114
        foreach ($fileUploadChunks as $fileUploadChunk) {
1✔
115
            if ($fileUploadChunk instanceof FailedUpload) {
1✔
116
                $uploadFailed = true;
1✔
117
                continue;
1✔
118
            }
119
            try {
120
                $storage->save($fileUploadChunk);
1✔
121
            } catch (UnableToSaveFileUploadException $exception) {
×
122
                $uploadFailed = true;
×
123
            }
124
        }
125
        if ($uploadFailed) {
1✔
126
            $this->addError(Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::VALID]);
1✔
127
        }
128
    }
1✔
129

130
    /**
131
     * @return FileUpload[]
132
     */
133
    public function getValue(): array
134
    {
135
        if ($this->isDisabled()) {
1✔
136
            return [];
1✔
137
        }
138

139
        try {
140
            return array_map(fn (FileUploadItem $fileUploadItem): FileUpload => $fileUploadItem->fileUpload, $this->getStorage()->list());
1✔
141
        } catch (StorageDoesNotExistException $exception) {
×
142
            throw new \LogicException($exception->getMessage(), 0, $exception);
×
143
        }
144
    }
145

146
    /**
147
     * @return $this
148
     * @internal
149
     */
150
    public function setValue(mixed $value): static
1✔
151
    {
152
        return $this;
1✔
153
    }
154

155
    public function isDisabled(): bool
156
    {
157
        return $this->getUploadControl()->isDisabled();
1✔
158
    }
159

160
    /**
161
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
162
     * @param bool $value
163
     * @return $this
164
     * @throws Nette\Application\BadRequestException
165
     */
166
    public function setDisabled($value = true): static
1✔
167
    {
168
        $this->getUploadControl()->setDisabled($value);
1✔
169
        if (! $value) {
1✔
170
            return $this;
×
171
        }
172

173
        $form = $this->getForm(false);
1✔
174
        if ($form !== null && $form->isAnchored() && (bool) $form->isSubmitted()) {
1✔
175
            $this->loadHttpData();
×
176
        }
177

178
        return $this;
1✔
179
    }
180

181
    public function getControlPart(?string $key = null): ?Html
1✔
182
    {
183
        if ($key === 'namespace') {
1✔
184
            return $this->getNamespaceControl()->getControl();
1✔
185
        }
186

187
        if ($key === 'upload') {
1✔
188
            $control = $this->getUploadControl()->getControl();
1✔
189
            assert($control instanceof Html);
1✔
190
            $control->{'data-nette-rules'} = Nette\Forms\Helpers::exportRules($this->getRules());
1✔
191
            return $control;
1✔
192
        }
193

194
        return $this->getControl();
×
195
    }
196

197
    public function getControl(): Html
198
    {
199
        $this->setOption('rendered', true);
1✔
200
        $control = clone $this->control;
1✔
201

202
        $template = $this->getTemplate();
1✔
203
        assert($template instanceof Template);
1✔
204

205
        try {
206
            $storage = $this->getStorage();
1✔
207
        } catch (StorageDoesNotExistException $exception) {
×
208
            throw new \LogicException($exception->getMessage(), 0, $exception);
×
209
        }
210
        $fileUploadItems = $storage->list();
1✔
211

212
        $uniqueFilenames = array_map(fn (FileUploadItem $fileUploadItem): string => $fileUploadItem->fileUpload->getUntrustedName(), $fileUploadItems);
1✔
213
        $completedFiles = [];
1✔
214
        $partiallyUploadedFiles = [];
1✔
215
        foreach ($fileUploadItems as $fileUploadItem) {
1✔
216
            if ($fileUploadItem->fileUpload->isOk()) {
1✔
217
                $completedFiles[] = $this->createUploadSuccessResponse($fileUploadItem);
1✔
218
            } else {
219
                $partiallyUploadedFiles[] = $this->createUploadSuccessResponse($fileUploadItem);
1✔
220
            }
221
        }
222

223
        $template->uniqueFilenames = $uniqueFilenames;
1✔
224
        $template->completedFiles = $completedFiles;
1✔
225
        $template->partiallyUploadedFiles = $partiallyUploadedFiles;
1✔
226
        $template->allFiles = array_merge($completedFiles, $partiallyUploadedFiles);
1✔
227
        $template->uploadUrl = $this->link('upload!', ['namespace' => $this->getUploadNamespace()->toString()]);
1✔
228

229
        $controlHtml = Nette\Utils\Helpers::capture(function () use ($template): void {
1✔
230
            $template->render($this->templateFile);
1✔
231
        });
1✔
232
        return $control->addHtml($controlHtml);
1✔
233
    }
234

235
    public function getLabelPrototype(): Html
236
    {
237
        return $this->getUploadControl()->getLabelPrototype();
1✔
238
    }
239

240
    /**
241
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
242
     * @param string|\Stringable|null $caption
243
     */
244
    public function getLabel($caption = null): Html
1✔
245
    {
246
        $label = $this->getUploadControl()->getLabel($caption);
1✔
247
        assert($label instanceof Html);
1✔
248
        return $label;
1✔
249
    }
250

251
    /**
252
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
253
     * @param callable|string $validator
254
     * @param string|\Stringable|null $errorMessage
255
     * @return $this
256
     */
257
    public function addRule($validator, $errorMessage = null, mixed $arg = null): static
1✔
258
    {
259
        if ($validator === Form::IMAGE) {
1✔
260
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', FileUpload::IMAGE_MIME_TYPES));
1✔
261
        } elseif ($validator === Form::MIME_TYPE) {
1✔
262
            $mimeTypes = is_array($arg) ? $arg : ($arg === null ? [] : [$arg]);
1✔
263
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', $mimeTypes));
1✔
264
        }
265

266
        return $this->_addRule($validator, $errorMessage, $arg);
1✔
267
    }
268

269
    /**
270
     * @throws Nette\Application\BadRequestException
271
     */
272
    public function handleUpload(string $namespace): void
1✔
273
    {
274
        $uploadNamespace = $this->parseUploadNamespace($namespace);
1✔
275
        $this->setUploadNamespace($uploadNamespace);
1✔
276

277
        $fileUploadChunks = $this->getFileUploadChunks();
1✔
278
        $responses = [];
1✔
279
        foreach ($fileUploadChunks as $fileUploadChunk) {
1✔
280
            if ($this->isDisabled()) {
1✔
281
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate('Upload disabled'));
1✔
282
                continue;
1✔
283
            }
284

285
            if ($fileUploadChunk instanceof FailedUpload) {
1✔
286
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate(Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::VALID]));
1✔
287
                continue;
1✔
288
            }
289

290
            if ($fileUploadChunk->contentRange->containsFirstByte()) {
1✔
291
                $fakeFileUpload = new FileUpload([
1✔
292
                    'name' => $fileUploadChunk->fileUpload->getUntrustedName(),
1✔
293
                    'size' => $fileUploadChunk->contentRange->getSize(),
1✔
294
                    'tmp_name' => $fileUploadChunk->fileUpload->getTemporaryFile(),
1✔
295
                    'error' => UPLOAD_ERR_OK,
1✔
296
                ]);
297
                $this->fakeUploadControl->setNewFileUpload($fakeFileUpload);
1✔
298
                $this->validate();
1✔
299
                $error = $this->getError();
1✔
300
                if ($error !== null) {
1✔
301
                    $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $error);
1✔
302
                    continue;
1✔
303
                }
304
            }
305

306
            try {
307
                $fileUploadItem = $this->getStorage()->save($fileUploadChunk);
1✔
308
                $responses[] = $this->createUploadSuccessResponse($fileUploadItem);
1✔
309
            } catch (StorageDoesNotExistException | UnableToSaveFileUploadException $exception) {
×
310
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate(Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::VALID]));
×
311
            }
312
        }
313

314
        $this->sendJson(new ListResponse(...$responses));
1✔
315
    }
316

317
    /**
318
     * @throws Nette\Application\BadRequestException
319
     */
320
    public function handleDelete(string $namespace, string $id): void
321
    {
322
        $fileUploadId = $this->parseFileUploadId($id);
×
323
        $uploadNamespace = $this->parseUploadNamespace($namespace);
×
324
        $this->setUploadNamespace($uploadNamespace);
×
325

326
        try {
327
            $this->getStorage()->delete($fileUploadId);
×
328
        } catch (StorageDoesNotExistException $exception) {
×
329
            // noop
330
        }
331
        $this->sendJson('');
×
332
    }
333

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

343
        try {
344
            $fileUploadItem = $this->getStorage()->load($fileUploadId);
1✔
345
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
346
            throw new Nette\Application\BadRequestException('File upload not found.', 0, $exception);
×
347
        }
348

349
        $fileUpload = $fileUploadItem->fileUpload;
1✔
350
        $response = new Nette\Application\Responses\FileResponse(
1✔
351
            $fileUpload->getTemporaryFile(),
1✔
352
            $fileUpload->getUntrustedName(),
1✔
353
        );
354
        $this->getPresenter()->sendResponse($response);
1✔
355
    }
356

357
    /**
358
     * @throws Nette\Application\BadRequestException
359
     */
360
    public function handleThumbnail(string $namespace, string $id): void
361
    {
362
        $fileUploadId = $this->parseFileUploadId($id);
×
363
        $uploadNamespace = $this->parseUploadNamespace($namespace);
×
364
        $this->setUploadNamespace($uploadNamespace);
×
365

366
        try {
367
            $fileUpload = $this->getStorage()->load($fileUploadId)->fileUpload;
×
368
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
369
            throw new Nette\Application\BadRequestException('Source file for thumbnail not found.', 0, $exception);
×
370
        }
371

372
        if (! $this->thumbnailProvider->isSupported($fileUpload)) {
×
373
            throw new Nette\Application\BadRequestException('Thumbnail could not be generated');
×
374
        }
375

376
        $response = $this->thumbnailProvider->createThumbnail($fileUpload);
×
377
        $this->getPresenter()->sendResponse($response);
×
378
    }
379

380
    protected function createTemplate(?string $class = null): Template
1✔
381
    {
382
        $template = parent::createTemplate($class);
1✔
383
        assert($template instanceof Template);
1✔
384

385
        $translator = $this->getTranslator();
1✔
386
        if ($translator !== null) {
1✔
387
            $template->setTranslator($translator);
1✔
388
        }
389

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

392
        return $template;
1✔
393
    }
394

395
    protected function getHttpRequest(): Nette\Http\IRequest
396
    {
397
        return $this->getPresenter()->getHttpRequest();
1✔
398
    }
399

400
    /**
401
     * Sends back JSON response.
402
     * Sets the right content type based on the support on the other end.
403
     * https://github.com/blueimp/jQuery-File-Upload/wiki/Setup#wiki-content-type-negotiation
404
     */
405
    protected function sendJson(mixed $data): void
1✔
406
    {
407
        $contentType = Strings::contains((string) $this->getHttpRequest()->getHeader('accept'), 'application/json') ? 'application/json' : 'text/plain';
1✔
408
        $response = new Nette\Application\Responses\JsonResponse($data, $contentType);
1✔
409
        $this->getPresenter()->sendResponse($response);
1✔
410
    }
411

412
    protected function getUploadControl(): Nette\Forms\Controls\UploadControl
413
    {
414
        return $this->getComponent('upload');
1✔
415
    }
416

417
    protected function getNamespaceControl(): Nette\Forms\Controls\HiddenField
418
    {
419
        return $this->getComponent('namespace');
1✔
420
    }
421

422
    protected function getThumbnailProvider(): ThumbnailProvider
423
    {
424
        return $this->thumbnailProvider;
×
425
    }
426

427
    protected function getUploadNamespace(): UploadNamespace
428
    {
429
        /** @var string|null $nameSpaceValue */
430
        $nameSpaceValue = $this->getNamespaceControl()->getValue();
1✔
431
        if ($nameSpaceValue !== null && UploadNamespace::isValid($nameSpaceValue)) {
1✔
432
            return UploadNamespace::fromString($nameSpaceValue);
1✔
433
        }
434

435
        $namespace = $this->storageManager->createNewNamespace();
×
436
        $this->setUploadNamespace($namespace);
×
437
        return $namespace;
×
438
    }
439

440
    protected function setUploadNamespace(UploadNamespace $namespace): void
1✔
441
    {
442
        $this->getNamespaceControl()->setValue($namespace->toString());
1✔
443
    }
1✔
444

445
    /**
446
     * @throws StorageDoesNotExistException
447
     */
448
    protected function getStorage(): Storage
449
    {
450
        return $this->storageManager->getStorage($this->getUploadNamespace());
1✔
451
    }
452

453
    protected function createUploadSuccessResponse(FileUploadItem $fileUploadItem): Response
1✔
454
    {
455
        $fileUpload = $fileUploadItem->fileUpload;
1✔
456
        $idValue = $fileUploadItem->id->toString();
1✔
457
        $namespaceValue = $this->getUploadNamespace()->toString();
1✔
458
        return new UploadSuccessResponse(
1✔
459
            $fileUpload->getUntrustedName(),
1✔
460
            $fileUpload->getSize(),
1✔
461
            $fileUpload->getContentType(),
1✔
462
            $this->link('download!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
463
            $this->link('delete!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
464
            $this->thumbnailProvider->isSupported($fileUpload) ? $this->link('thumbnail!', ['namespace' => $namespaceValue, 'id' => $idValue]) : null,
1✔
465
        );
466
    }
467

468
    protected function createUploadErrorResponse(FileUploadChunk|FailedUpload $upload, string $error): Response
1✔
469
    {
470
        return new UploadErrorResponse(
1✔
471
            $upload->fileUpload->getUntrustedName(),
1✔
472
            $upload->contentRange?->getSize() ?? 0,
1✔
473
            $error,
474
        );
475
    }
476

477
    /**
478
     * @throws Nette\Application\BadRequestException
479
     */
480
    protected function parseUploadNamespace(string $value): UploadNamespace
1✔
481
    {
482
        if (! UploadNamespace::isValid($value)) {
1✔
483
            throw new Nette\Application\BadRequestException('Invalid namespace value', 400);
×
484
        }
485
        return UploadNamespace::fromString($value);
1✔
486
    }
487

488
    /**
489
     * @throws Nette\Application\BadRequestException
490
     */
491
    protected function parseFileUploadId(string $value): FileUploadId
1✔
492
    {
493
        if (! FileUploadId::isValid($value)) {
1✔
494
            throw new Nette\Application\BadRequestException('Invalid file upload id value', 400);
×
495
        }
496
        return FileUploadId::fromString($value);
1✔
497
    }
498

499
    /**
500
     * @return list<FileUploadChunk|FailedUpload>
501
     * @throws Nette\Application\BadRequestException
502
     */
503
    protected function getFileUploadChunks(): array
504
    {
505
        $httpRequest = $this->getHttpRequest();
1✔
506
        /** @var array<int, FileUpload> $files */
507
        $files = Nette\Forms\Helpers::extractHttpData($httpRequest->getFiles(), $this->getUploadControl()->getHtmlName() . '[]', Form::DATA_FILE);
1✔
508
        if (count($files) === 0) {
1✔
509
            return [];
1✔
510
        }
511

512
        $contentRangeHeaderValue = $httpRequest->getHeader('content-range');
1✔
513
        if ($contentRangeHeaderValue !== null) {
1✔
514
            if (count($files) > 1) {
1✔
515
                throw new Nette\Application\BadRequestException('Chunk upload does not support multi-file upload', 400);
×
516
            }
517
            $file = reset($files);
1✔
518

519
            try {
520
                $contentRange = ContentRange::fromHttpHeaderValue($contentRangeHeaderValue);
1✔
521
                $fileUpload = $file->isOk() ? FileUploadChunk::partialUpload($file, $contentRange) : FailedUpload::of($file, $contentRange);
1✔
522
                return [$fileUpload];
1✔
523
            } catch (\Throwable $exception) {
×
524
                throw new Nette\Application\BadRequestException('Invalid content-range header value', 400, $exception);
×
525
            }
526
        }
527

528
        /** @var list<FileUploadChunk|FailedUpload> $fileUploads */
529
        $fileUploads = [];
1✔
530
        foreach ($files as $file) {
1✔
531
            $fileUploads[] = $file->isOk() ? FileUploadChunk::completeUpload($file) : FailedUpload::of($file);
1✔
532
        }
533

534
        return $fileUploads;
1✔
535
    }
536

537
}
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