-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCloudinaryAudio.php
More file actions
58 lines (47 loc) · 1.94 KB
/
CloudinaryAudio.php
File metadata and controls
58 lines (47 loc) · 1.94 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
49
50
51
52
53
54
55
56
57
58
<?php
namespace Silvanite\NovaFieldCloudinary\Fields;
use Illuminate\Http\Request;
use Davidpiesse\Audio\Audio;
class CloudinaryAudio extends Audio
{
/**
* 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();
});
}
public function preview(callable $previewUrlCallback)
{
$this->previewUrlCallback = function () {
return $this->value
? cloudinary_audio($this->value, [], $this->disk)
: null;
};
return $this;
}
}