-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCloudinaryVideo.php
More file actions
48 lines (39 loc) · 1.71 KB
/
CloudinaryVideo.php
File metadata and controls
48 lines (39 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Silvanite\NovaFieldCloudinary\Fields;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\File;
use Illuminate\Support\Facades\Storage;
class CloudinaryVideo extends File
{
/**
* Create a new field.
*
* @param string $name
* @param string|null $attribute
* @param string|null $disk
* @param callable|null $storageCallback
* @return void
*/
public function __construct($name, $attribute = null, $disk = 'cloudinary', $storageCallback = null)
{
parent::__construct($name, $attribute, $disk, $storageCallback);
$this->store(function(Request $request, $model, $attribute, $requestAttribute){
$filename = $request->file($requestAttribute)->store($this->getStorageDir(), [
'disk' => $this->getStorageDisk(),
'cloudinary' => $this->cloudinaryOptions
]);
// If a folder is specified we ensure a trailing slash
// If no folder is specified we ensure no beginning slash
$path = array_key_exists('folder',$this->cloudinaryOptions) ? rtrim($this->cloudinaryOptions['folder'], '/') . '/' : '';
return $path . $filename;
})->storeAs(function (Request $request) {
$name = $request->{$this->attribute}->getClientOriginalName();
$ext = '.' . $request->{$this->attribute}->getClientOriginalExtension();
return sha1($name . time()) . $ext;
})->delete(function (Request $request, $model) {
$path = pathinfo($model->{$this->attribute});
Storage::disk($this->disk)->delete($path['dirname'] .'/'. $path['filename']);
return $this->columnsThatShouldBeDeleted();
});
}
}