-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelFileManager.php
More file actions
165 lines (153 loc) · 4.23 KB
/
Copy pathModelFileManager.php
File metadata and controls
165 lines (153 loc) · 4.23 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
trait ModelFileManager
{
/**
* Main function used to delete a file.
*
* @param string $field
* @return void
*/
public function deleteTheFile(string $field)
{
$this->deleteFile($field);
$this->save();
return;
}
/**
* Main function used to store a new file.
*
* @param string $field
* @param [type] $file
* @return void
*/
public function storeTheFile(string $field = 'image', $file = '')
{
$this->deleteFile($field);
$this->storeFile($field, $file);
$this->save();
return;
}
/**
* If existe Return the file content.
*
* @param string $field
* @return
*/
public function getFile(string $field = 'image')
{
if (Storage::disk($this->disk)->has($this->{$field})) {
return Storage::disk($this->disk)->get($this->{$field});
}
$this->logError($field, 'Retrieve the contents(get)', 'getFile');
}
/**
* If exist return a download response.
*
* @param string $field
* @return void
*/
public function downloadFile(string $field = 'image')
{
if (Storage::disk($this->disk)->has($this->{$field})) {
return Storage::disk($this->disk)->download($this->{$field});
}
$this->logError($field, 'Download', 'downloadFile');
}
/**
* If exist, delete the file.
*
* @param string $field
* @return void
*/
protected function deleteFile(string $field = 'image')
{
//First upload there is no need to delete
if (!$this->{$field}) {
return;
}
if (Storage::disk($this->disk)->has($this->{$field})) {
Storage::disk($this->disk)->delete($this->{$field});
$this->{$field} = null;
return;
}
//If there is not file, just put it in null and return error.
$this->{$field} = null;
$this->save();
$this->logError($field, 'Delete', 'deleteFile');
}
/**
* Store the content of the file in the disk.
*
* @param string $field
* @return void
*/
protected function storeFile(string $field, $file)
{
if ($file) {
$this->contentFile($file, $field);
} else {
$this->requestFile($field);
}
}
/**
* Store the file that came from get or file_get_contents.
*
* @param [type] $file
* @param string $field
* @return void
*/
protected function contentFile($file, string $field)
{
$this->{$field} = Storage::disk($this->disk)->put('', $file);
}
/**
* Store the file that came from the request.
*
* @param string $field
* @return void
*/
protected function requestFile(string $field)
{
$request = request();
if ($this->fileIsSafe($field, $request)) {
$this->{$field} = $request->{$field}->store('', $this->disk);
return;
}
return;
}
/**
* Check if the file is in the request and came alright.
*
* @param string $field
* @param Request $request
* @return void
*/
protected function fileIsSafe(string $field, \Illuminate\Http\Request $request)
{
if ($request->hasFile($field) && $request->file($field)->isValid()) {
return true;
}
return false;
}
/**
* Local function used to store errors from the trait.
*
* @param string $field
* @param string $action
* @param string $funcion
* @return void
*/
protected function logError(string $field, string $action = 'descargar', string $funcion = ''): void
{
$modelo = class_basename($this);
if (\Auth::check()) {
Log::error("Error en $funcion, Usuario id = " . \Auth::user()->id . " - Intento $action un archivo del campo $field del modelo $modelo con id = $this->id .");
} else {
Log::error("Error en $funcion, Usuario visitante - Intento $action un archivo del campo $field del modelo $modelo con id = $this->id .");
}
abort(404);
}
}