1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Clamavfileupload\Support; |
4
|
|
|
|
5
|
|
|
use Ikechukwukalu\Clamavfileupload\Foundation\FileUpload; |
6
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
|
9
|
|
|
class TemporaryFileUpload extends FileUpload |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Run files scan and upload. |
13
|
|
|
* |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
|
|
public function fileUpload(): bool|array |
17
|
|
|
{ |
18
|
|
|
if($this->request->file()) { |
19
|
|
|
return $this->storeFiles(); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$this->failedUpload(trans('clamavfileupload::clamav.empty_file_input')); |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Remove single or multiple files. |
28
|
|
|
* |
29
|
|
|
* @param array $files |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function removeFiles(array $files = [], null|string $disk = null): bool |
33
|
|
|
{ |
34
|
|
|
foreach ($files as $file) { |
35
|
|
|
$file = str_replace($this->storageDisk()->path('tmp'), '', $file); |
36
|
|
|
$this->storageDisk()->delete('tmp' . $file); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Provide \Illuminate\Support\Facades\Storage::build. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Contracts\Filesystem\Filesystem |
46
|
|
|
*/ |
47
|
|
|
protected function provideDisk(): Filesystem |
48
|
|
|
{ |
49
|
|
|
return Storage::build([ |
50
|
|
|
'driver' => 'local', |
51
|
|
|
'root' => $this->storageDisk()->path('tmp') |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get \Illuminate\Support\Facades\Storage::disk. |
57
|
|
|
* |
58
|
|
|
* @return \Illuminate\Contracts\Filesystem\Filesystem |
59
|
|
|
*/ |
60
|
|
|
protected function storageDisk(): Filesystem |
61
|
|
|
{ |
62
|
|
|
return Storage::disk('local'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Save single or multiple files. |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function storeFiles(): bool|array |
72
|
|
|
{ |
73
|
|
|
$this->fileName = $this->setFileName(); |
74
|
|
|
|
75
|
|
|
if (is_array($this->request->file($this->input))) { |
76
|
|
|
return $this->saveMultipleFiles($this->fileName); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->saveSingleFile($this->fileName); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Save multiple files. |
84
|
|
|
* |
85
|
|
|
* @param null|string $fileName |
86
|
|
|
* @return bool |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
protected function saveMultipleFiles(null|string $fileName = null): bool|array |
90
|
|
|
{ |
91
|
|
|
$disk = $this->provideDisk(); |
92
|
|
|
$tmpFiles = []; |
93
|
|
|
$i = 1; |
94
|
|
|
|
95
|
|
|
foreach ($this->request->file($this->input) as $file) { |
96
|
|
|
$tmp = $fileName . "_{$i}" . $this->getExtension($file); |
97
|
|
|
$disk->putFileAs("", $file, $tmp); |
98
|
|
|
$tmpFiles[] = $this->storageDisk()->path("tmp/{$tmp}"); |
99
|
|
|
|
100
|
|
|
$i ++; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $tmpFiles; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Save single file. |
108
|
|
|
* |
109
|
|
|
* @param null|string $fileName |
110
|
|
|
* @return bool |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
protected function saveSingleFile(null|string $fileName = null): bool|array |
114
|
|
|
{ |
115
|
|
|
$tmp = $fileName . $this->getExtension(); |
116
|
|
|
|
117
|
|
|
$this->provideDisk()->putFileAs("", |
118
|
|
|
$this->request->file($this->input), |
|
|
|
|
119
|
|
|
$fileName . $this->getExtension()); |
120
|
|
|
|
121
|
|
|
return [$this->storageDisk()->path("tmp/{$tmp}")]; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|