|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\Concerns\Storage\Attachment; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Mostafaznv\Larupload\Larupload; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
use Illuminate\Http\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
10
|
|
|
|
|
11
|
|
|
trait RetrieveAttachment |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get metadata as an array or object |
|
15
|
|
|
* |
|
16
|
|
|
* @param string|null $key |
|
17
|
|
|
* @return object|string|integer|null |
|
18
|
|
|
*/ |
|
19
|
|
|
public function meta(string $key = null): object|int|string|null |
|
20
|
|
|
{ |
|
21
|
|
|
if ($key) { |
|
22
|
|
|
$meta = $this->output; |
|
23
|
|
|
|
|
24
|
|
|
if (array_key_exists($key, $meta)) { |
|
25
|
|
|
return $meta[$key]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return null; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $this->outputToObject(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get url for all styles (original, cover and ...) of current entity |
|
36
|
|
|
* |
|
37
|
|
|
* @return object |
|
38
|
|
|
*/ |
|
39
|
|
|
public function urls(): object |
|
40
|
|
|
{ |
|
41
|
|
|
$styles = new stdClass(); |
|
42
|
|
|
$staticStyles = [Larupload::ORIGINAL_FOLDER, Larupload::COVER_FOLDER, Larupload::STREAM_FOLDER]; |
|
43
|
|
|
$allStyles = array_merge( |
|
44
|
|
|
$staticStyles, |
|
45
|
|
|
array_keys($this->imageStyles), |
|
46
|
|
|
array_keys($this->videoStyles) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($allStyles as $style) { |
|
50
|
|
|
if ($style == Larupload::COVER_FOLDER and !$this->generateCover) { |
|
51
|
|
|
$styles->{$style} = null; |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
else if ($style == Larupload::STREAM_FOLDER and empty($this->streams)) { |
|
55
|
|
|
unset($styles->{$style}); |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$styles->{$this->nameStyle($style)} = $this->url($style); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($this->withMeta) { |
|
63
|
|
|
$styles->meta = $this->meta(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $styles; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Generate URL for attached file |
|
71
|
|
|
* Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $style |
|
74
|
|
|
* @return null|string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function url(string $style = Larupload::ORIGINAL_FOLDER): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
$path = $this->prepareStylePath($style); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
if ($path) { |
|
81
|
|
|
return $this->storageUrl($path); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Download attached file |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $style |
|
91
|
|
|
* @return RedirectResponse|StreamedResponse|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function download(string $style = Larupload::ORIGINAL_FOLDER): StreamedResponse|RedirectResponse|null |
|
94
|
|
|
{ |
|
95
|
|
|
$path = $this->prepareStylePath($style); |
|
96
|
|
|
|
|
97
|
|
|
if ($path) { |
|
98
|
|
|
return $this->storageDownload($path); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|