1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaChunkedVideo; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use Laravel\Nova\Contracts\Deletable as DeletableContract; |
7
|
|
|
use Laravel\Nova\Contracts\Downloadable as DownloadableContract; |
8
|
|
|
use Laravel\Nova\Contracts\Storable as StorableContract; |
9
|
|
|
use Laravel\Nova\Fields\AcceptsTypes; |
10
|
|
|
use Laravel\Nova\Fields\Deletable; |
11
|
|
|
use Laravel\Nova\Fields\Field; |
12
|
|
|
use Laravel\Nova\Fields\HasDownload; |
13
|
|
|
use Laravel\Nova\Fields\HasPreview; |
14
|
|
|
use Laravel\Nova\Fields\HasThumbnail; |
15
|
|
|
use Laravel\Nova\Fields\Storable; |
16
|
|
|
use Laravel\Nova\Fields\SupportsDependentFields; |
17
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
18
|
|
|
|
19
|
|
|
class ChunkedVideo extends Field implements StorableContract, DeletableContract, DownloadableContract |
20
|
|
|
{ |
21
|
|
|
use Storable; |
22
|
|
|
use HasPreview; |
23
|
|
|
use HasThumbnail; |
24
|
|
|
use AcceptsTypes; |
25
|
|
|
use Deletable; |
26
|
|
|
use HasDownload; |
27
|
|
|
use SupportsDependentFields; |
28
|
|
|
use FileChunkSize; |
29
|
|
|
use HasChunkFolder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
|
|
public $component = 'chunked-video'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public $showOnCreation = false; |
40
|
|
|
|
41
|
13 |
|
public function __construct($name, $attribute = null, callable $resolveCallback = null) |
42
|
|
|
{ |
43
|
13 |
|
$this->chunksFolder = config('nova-chunked.tmp_chunks_folder'); |
44
|
|
|
|
45
|
13 |
|
parent::__construct($name, $attribute, $resolveCallback); |
46
|
|
|
|
47
|
13 |
|
$this |
48
|
13 |
|
->store(function ($filePath, $disk, $model, $attribute, $request) { |
|
|
|
|
49
|
1 |
|
$model->$attribute = $filePath; |
50
|
1 |
|
$model->save(); |
51
|
|
|
|
52
|
1 |
|
return Storage::disk($disk)->url($filePath); |
53
|
13 |
|
}) |
54
|
13 |
|
->preview(function ($value, ?string $disk, $model) { |
|
|
|
|
55
|
1 |
|
return $value ? Storage::disk($disk)->url($value) : null; |
56
|
13 |
|
}) |
57
|
13 |
|
->delete(function () { |
58
|
2 |
|
if ($this->value) { |
59
|
1 |
|
Storage::disk($this->getStorageDisk())->delete($this->value); |
60
|
|
|
|
61
|
1 |
|
return [$this->attribute => null]; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return null; |
65
|
13 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
1 |
|
protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute) |
72
|
|
|
{ |
73
|
|
|
// skip fill |
74
|
1 |
|
} |
75
|
|
|
|
76
|
7 |
|
public function getStorageDisk() |
77
|
|
|
{ |
78
|
7 |
|
return $this->disk ?: $this->getDefaultStorageDisk(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the full path that the field is stored at on disk. |
83
|
|
|
* |
84
|
|
|
* @return string|null |
85
|
|
|
*/ |
86
|
3 |
|
public function getStoragePath() |
87
|
|
|
{ |
88
|
3 |
|
return $this->value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Prepare the field for JSON serialization. |
93
|
|
|
* |
94
|
|
|
* @return array<string, mixed> |
95
|
|
|
*/ |
96
|
1 |
|
public function jsonSerialize(): array |
97
|
|
|
{ |
98
|
1 |
|
$previewUrl = $this->resolvePreviewUrl(); |
99
|
|
|
|
100
|
1 |
|
return array_merge(parent::jsonSerialize(), [ |
101
|
1 |
|
'thumbnailUrl' => $this->resolveThumbnailUrl(), |
102
|
1 |
|
'previewUrl' => $previewUrl, |
103
|
1 |
|
'downloadable' => $this->downloadsAreEnabled && isset($this->downloadResponseCallback) && !empty($previewUrl), |
104
|
1 |
|
'deletable' => isset($this->deleteCallback) && $this->deletable, |
105
|
1 |
|
'acceptedTypes' => $this->acceptedTypes, |
106
|
1 |
|
'maxSize' => $this->getMaxSize(), |
107
|
1 |
|
'chunkSize' => $this->getChunkSize(), |
108
|
1 |
|
]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.