|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Standalone; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Http\UploadedFile; |
|
7
|
|
|
use Illuminate\Support\Facades\Storage; |
|
8
|
|
|
use Mostafaznv\Larupload\Actions\GuessLaruploadFileTypeAction; |
|
9
|
|
|
use Mostafaznv\Larupload\Actions\OptimizeImageAction; |
|
10
|
|
|
|
|
11
|
|
|
trait BaseStandaloneLarupload |
|
12
|
|
|
{ |
|
13
|
|
|
public function upload(UploadedFile $file, ?UploadedFile $cover = null): object |
|
14
|
|
|
{ |
|
15
|
|
|
$this->internalFunctionIsCallable = true; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
file_is_valid($file, $this->name, 'file'); |
|
18
|
|
|
file_is_valid($cover, $this->name, 'cover'); |
|
19
|
|
|
|
|
20
|
|
|
$this->file = $this->optimizeImage ? OptimizeImageAction::make($file)->process() : $file; |
|
|
|
|
|
|
21
|
|
|
$this->type = GuessLaruploadFileTypeAction::make($file)->calc(); |
|
|
|
|
|
|
22
|
|
|
$this->cover = $cover; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
$this->clean($this->id); |
|
|
|
|
|
|
25
|
|
|
$this->setBasicDetails(); |
|
|
|
|
|
|
26
|
|
|
$this->setMediaDetails(); |
|
|
|
|
|
|
27
|
|
|
$this->uploadOriginalFile($this->id); |
|
|
|
|
|
|
28
|
|
|
$this->setCover($this->id); |
|
|
|
|
|
|
29
|
|
|
$this->handleStyles($this->id, self::class, true); |
|
|
|
|
|
|
30
|
|
|
$urls = $this->urls(); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$this->updateMeta($urls); |
|
33
|
|
|
|
|
34
|
|
|
return $urls; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function delete(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
$basePath = $this->getBasePath($this->id); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (Storage::disk($this->disk)->exists($basePath)) { |
|
42
|
|
|
$this->clean($this->id); |
|
43
|
|
|
|
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function updateMeta(?object $urls = null): void |
|
51
|
|
|
{ |
|
52
|
|
|
if (is_null($urls)) { |
|
53
|
|
|
$urls = $this->urls(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$metaPath = $this->getBasePath($this->id) . '/.meta'; |
|
57
|
|
|
Storage::disk($this->disk)->put($metaPath, json_encode($urls), 'private'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Check if .meta file exists |
|
62
|
|
|
* |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
private function metaExists(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
$metaPath = $this->getBasePath($this->id) . '/.meta'; |
|
68
|
|
|
|
|
69
|
|
|
if (Storage::disk($this->disk)->exists($metaPath)) { |
|
70
|
|
|
$meta = Storage::disk($this->disk)->get($metaPath); |
|
71
|
|
|
$meta = json_decode($meta); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
foreach ($meta->meta as $key => $value) { |
|
74
|
|
|
$this->output[$key] = $value; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|