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

nepada / file-upload-control / 11627792231

28 Oct 2024 11:42AM UTC coverage: 81.674%. Remained the same
11627792231

push

github

xificurk
FE: fix binding to latest nette-forms.js

722 of 884 relevant lines covered (81.67%)

0.82 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 Stringable;
33
use function implode;
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
    public function getLabel(string|Stringable|null $caption = null): Html
1✔
241
    {
242
        $label = $this->getUploadControl()->getLabel($caption);
1✔
243
        assert($label instanceof Html);
1✔
244
        return $label;
1✔
245
    }
246

247
    /**
248
     * @return $this
249
     */
250
    public function addRule(callable|string $validator, string|Stringable|null $errorMessage = null, mixed $arg = null): static
1✔
251
    {
252
        if ($validator === Form::Image) {
1✔
253
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', Nette\Forms\Helpers::getSupportedImages()));
1✔
254
        } elseif ($validator === Form::MimeType) {
1✔
255
            $mimeTypes = is_array($arg) ? $arg : ($arg === null ? [] : [$arg]);
1✔
256
            $this->getUploadControl()->setHtmlAttribute('accept', implode(',', $mimeTypes));
1✔
257
        }
258

259
        return $this->_addRule($validator, $errorMessage, $arg);
1✔
260
    }
261

262
    /**
263
     * @throws Nette\Application\BadRequestException
264
     */
265
    public function handleUpload(string $namespace): void
1✔
266
    {
267
        $uploadNamespace = $this->parseUploadNamespace($namespace);
1✔
268
        $this->setUploadNamespace($uploadNamespace);
1✔
269

270
        $fileUploadChunks = $this->getFileUploadChunks();
1✔
271
        $responses = [];
1✔
272
        foreach ($fileUploadChunks as $fileUploadChunk) {
1✔
273
            if ($this->isDisabled()) {
1✔
274
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate('Upload disabled'));
1✔
275
                continue;
1✔
276
            }
277

278
            if ($fileUploadChunk instanceof FailedUpload) {
1✔
279
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate(Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::Valid]));
1✔
280
                continue;
1✔
281
            }
282

283
            if ($fileUploadChunk->contentRange->containsFirstByte()) {
1✔
284
                $fakeFileUpload = new FileUpload([
1✔
285
                    'name' => $fileUploadChunk->fileUpload->getUntrustedName(),
1✔
286
                    'size' => $fileUploadChunk->contentRange->getSize(),
1✔
287
                    'tmp_name' => $fileUploadChunk->fileUpload->getTemporaryFile(),
1✔
288
                    'error' => UPLOAD_ERR_OK,
1✔
289
                ]);
290
                $this->fakeUploadControl->setNewFileUpload($fakeFileUpload);
1✔
291
                $this->validate();
1✔
292
                $error = $this->getError();
1✔
293
                if ($error !== null) {
1✔
294
                    $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $error);
1✔
295
                    continue;
1✔
296
                }
297
            }
298

299
            try {
300
                $fileUploadItem = $this->getStorage()->save($fileUploadChunk);
1✔
301
                $responses[] = $this->createUploadSuccessResponse($fileUploadItem);
1✔
302
            } catch (StorageDoesNotExistException | UnableToSaveFileUploadException $exception) {
×
303
                $responses[] = $this->createUploadErrorResponse($fileUploadChunk, $this->translate(Nette\Forms\Validator::$messages[Nette\Forms\Controls\UploadControl::Valid]));
×
304
            }
305
        }
306

307
        $this->sendJson(new ListResponse(...$responses));
1✔
308
    }
309

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

319
        try {
320
            $this->getStorage()->delete($fileUploadId);
×
321
        } catch (StorageDoesNotExistException $exception) {
×
322
            // noop
323
        }
324
        $this->sendJson('');
×
325
    }
326

327
    /**
328
     * @throws Nette\Application\BadRequestException
329
     */
330
    public function handleDownload(string $namespace, string $id): void
1✔
331
    {
332
        $fileUploadId = $this->parseFileUploadId($id);
1✔
333
        $uploadNamespace = $this->parseUploadNamespace($namespace);
1✔
334
        $this->setUploadNamespace($uploadNamespace);
1✔
335

336
        try {
337
            $fileUploadItem = $this->getStorage()->load($fileUploadId);
1✔
338
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
339
            throw new Nette\Application\BadRequestException('File upload not found.', 0, $exception);
×
340
        }
341

342
        $fileUpload = $fileUploadItem->fileUpload;
1✔
343
        $response = new Nette\Application\Responses\FileResponse(
1✔
344
            $fileUpload->getTemporaryFile(),
1✔
345
            $fileUpload->getUntrustedName(),
1✔
346
        );
347
        $this->getPresenter()->sendResponse($response);
1✔
348
    }
349

350
    /**
351
     * @throws Nette\Application\BadRequestException
352
     */
353
    public function handleThumbnail(string $namespace, string $id): void
354
    {
355
        $fileUploadId = $this->parseFileUploadId($id);
×
356
        $uploadNamespace = $this->parseUploadNamespace($namespace);
×
357
        $this->setUploadNamespace($uploadNamespace);
×
358

359
        try {
360
            $fileUpload = $this->getStorage()->load($fileUploadId)->fileUpload;
×
361
        } catch (StorageDoesNotExistException | FileUploadNotFoundException $exception) {
×
362
            throw new Nette\Application\BadRequestException('Source file for thumbnail not found.', 0, $exception);
×
363
        }
364

365
        if (! $this->thumbnailProvider->isSupported($fileUpload)) {
×
366
            throw new Nette\Application\BadRequestException('Thumbnail could not be generated');
×
367
        }
368

369
        $response = $this->thumbnailProvider->createThumbnail($fileUpload);
×
370
        $this->getPresenter()->sendResponse($response);
×
371
    }
372

373
    protected function createTemplate(?string $class = null): Template
1✔
374
    {
375
        $template = parent::createTemplate($class);
1✔
376
        assert($template instanceof Template);
1✔
377

378
        $translator = $this->getTranslator();
1✔
379
        if ($translator !== null) {
1✔
380
            $template->setTranslator($translator);
1✔
381
        }
382

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

385
        return $template;
1✔
386
    }
387

388
    protected function getHttpRequest(): Nette\Http\IRequest
389
    {
390
        return $this->getPresenter()->getHttpRequest();
1✔
391
    }
392

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

405
    protected function getUploadControl(): Nette\Forms\Controls\UploadControl
406
    {
407
        return $this->getComponent('upload');
1✔
408
    }
409

410
    protected function getNamespaceControl(): Nette\Forms\Controls\HiddenField
411
    {
412
        return $this->getComponent('namespace');
1✔
413
    }
414

415
    protected function getThumbnailProvider(): ThumbnailProvider
416
    {
417
        return $this->thumbnailProvider;
×
418
    }
419

420
    protected function getUploadNamespace(): UploadNamespace
421
    {
422
        /** @var string|null $nameSpaceValue */
423
        $nameSpaceValue = $this->getNamespaceControl()->getValue();
1✔
424
        if ($nameSpaceValue !== null && UploadNamespace::isValid($nameSpaceValue)) {
1✔
425
            return UploadNamespace::fromString($nameSpaceValue);
1✔
426
        }
427

428
        $namespace = $this->storageManager->createNewNamespace();
×
429
        $this->setUploadNamespace($namespace);
×
430
        return $namespace;
×
431
    }
432

433
    protected function setUploadNamespace(UploadNamespace $namespace): void
1✔
434
    {
435
        $this->getNamespaceControl()->setValue($namespace->toString());
1✔
436
    }
1✔
437

438
    /**
439
     * @throws StorageDoesNotExistException
440
     */
441
    protected function getStorage(): Storage
442
    {
443
        return $this->storageManager->getStorage($this->getUploadNamespace());
1✔
444
    }
445

446
    protected function createUploadSuccessResponse(FileUploadItem $fileUploadItem): Response
1✔
447
    {
448
        $fileUpload = $fileUploadItem->fileUpload;
1✔
449
        $idValue = $fileUploadItem->id->toString();
1✔
450
        $namespaceValue = $this->getUploadNamespace()->toString();
1✔
451
        return new UploadSuccessResponse(
1✔
452
            $fileUpload->getUntrustedName(),
1✔
453
            $fileUpload->getSize(),
1✔
454
            $fileUpload->getContentType(),
1✔
455
            $this->link('download!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
456
            $this->link('delete!', ['namespace' => $namespaceValue, 'id' => $idValue]),
1✔
457
            $this->thumbnailProvider->isSupported($fileUpload) ? $this->link('thumbnail!', ['namespace' => $namespaceValue, 'id' => $idValue]) : null,
1✔
458
        );
459
    }
460

461
    protected function createUploadErrorResponse(FileUploadChunk|FailedUpload $upload, string $error): Response
1✔
462
    {
463
        return new UploadErrorResponse(
1✔
464
            $upload->fileUpload->getUntrustedName(),
1✔
465
            $upload->contentRange?->getSize() ?? 0,
1✔
466
            $error,
467
        );
468
    }
469

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

481
    /**
482
     * @throws Nette\Application\BadRequestException
483
     */
484
    protected function parseFileUploadId(string $value): FileUploadId
1✔
485
    {
486
        if (! FileUploadId::isValid($value)) {
1✔
487
            throw new Nette\Application\BadRequestException('Invalid file upload id value', 400);
×
488
        }
489
        return FileUploadId::fromString($value);
1✔
490
    }
491

492
    /**
493
     * @return list<FileUploadChunk|FailedUpload>
494
     * @throws Nette\Application\BadRequestException
495
     */
496
    protected function getFileUploadChunks(): array
497
    {
498
        $httpRequest = $this->getHttpRequest();
1✔
499
        /** @var array<int, FileUpload> $files */
500
        $files = Nette\Forms\Helpers::extractHttpData($httpRequest->getFiles(), $this->getUploadControl()->getHtmlName() . '[]', Form::DataFile);
1✔
501
        if (count($files) === 0) {
1✔
502
            return [];
1✔
503
        }
504

505
        $contentRangeHeaderValue = $httpRequest->getHeader('content-range');
1✔
506
        if ($contentRangeHeaderValue !== null) {
1✔
507
            if (count($files) > 1) {
1✔
508
                throw new Nette\Application\BadRequestException('Chunk upload does not support multi-file upload', 400);
×
509
            }
510
            $file = reset($files);
1✔
511

512
            try {
513
                $contentRange = ContentRange::fromHttpHeaderValue($contentRangeHeaderValue);
1✔
514
                $fileUpload = $file->isOk() ? FileUploadChunk::partialUpload($file, $contentRange) : FailedUpload::of($file, $contentRange);
1✔
515
                return [$fileUpload];
1✔
516
            } catch (\Throwable $exception) {
×
517
                throw new Nette\Application\BadRequestException('Invalid content-range header value', 400, $exception);
×
518
            }
519
        }
520

521
        /** @var list<FileUploadChunk|FailedUpload> $fileUploads */
522
        $fileUploads = [];
1✔
523
        foreach ($files as $file) {
1✔
524
            $fileUploads[] = $file->isOk() ? FileUploadChunk::completeUpload($file) : FailedUpload::of($file);
1✔
525
        }
526

527
        return $fileUploads;
1✔
528
    }
529

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