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
|
|
|
array_keys($this->audioStyles) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
foreach ($allStyles as $style) { |
51
|
|
|
if ($style == Larupload::COVER_FOLDER and !$this->generateCover) { |
52
|
|
|
$styles->{$style} = null; |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
else if ($style == Larupload::STREAM_FOLDER and empty($this->streams)) { |
56
|
|
|
unset($styles->{$style}); |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$styles->{$this->nameStyle($style)} = $this->url($style); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->withMeta) { |
64
|
|
|
$styles->meta = $this->meta(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $styles; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate URL for attached file |
72
|
|
|
* 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 |
73
|
|
|
* |
74
|
|
|
* @param string $style |
75
|
|
|
* @return null|string |
76
|
|
|
*/ |
77
|
|
|
public function url(string $style = Larupload::ORIGINAL_FOLDER): ?string |
78
|
|
|
{ |
79
|
|
|
$path = $this->prepareStylePath($style); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($path) { |
82
|
|
|
return $this->storageUrl($path); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Download attached file |
90
|
|
|
* |
91
|
|
|
* @param string $style |
92
|
|
|
* @return RedirectResponse|StreamedResponse|null |
93
|
|
|
*/ |
94
|
|
|
public function download(string $style = Larupload::ORIGINAL_FOLDER): StreamedResponse|RedirectResponse|null |
95
|
|
|
{ |
96
|
|
|
$path = $this->prepareStylePath($style); |
97
|
|
|
|
98
|
|
|
if ($path) { |
99
|
|
|
return $this->storageDownload($path); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|