|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SET\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Encryption\DecryptException; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Http\Response; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use Krucas\Notification\Facades\Notification; |
|
10
|
|
|
use SET\Attachment; |
|
11
|
|
|
use SET\Training; |
|
12
|
|
|
use SET\User; |
|
13
|
|
|
|
|
14
|
|
|
class AttachmentController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Currently only called from the Training page via the sidebar upload. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $request |
|
20
|
|
|
* |
|
21
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
22
|
|
|
*/ |
|
23
|
|
|
public function store(Request $request) |
|
24
|
|
|
{ |
|
25
|
|
|
$data = $request->all(); |
|
26
|
|
|
|
|
27
|
|
|
$encrypt = false; |
|
28
|
|
|
|
|
29
|
|
|
if (array_key_exists('encrypt', $data)) { |
|
30
|
|
|
$encrypt = $data['encrypt']; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($data['type'] == 'training') { |
|
34
|
|
|
$model = Training::findOrFail($data['id']); |
|
35
|
|
|
} elseif ($data['type'] == 'user') { |
|
36
|
|
|
$model = User::findOrFail($data['id']); |
|
37
|
|
|
$encrypt = true; |
|
38
|
|
|
} else { |
|
39
|
|
|
Notification::container()->success('Upload Failed'); |
|
40
|
|
|
|
|
41
|
|
|
return back(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
Attachment::upload($model, $request->file('files'), $encrypt); |
|
45
|
|
|
Notification::container()->success('Upload Complete'); |
|
46
|
|
|
|
|
47
|
|
|
return back(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Let the browser download the file. |
|
52
|
|
|
* |
|
53
|
|
|
* @param $fileId |
|
54
|
|
|
* |
|
55
|
|
|
* @return Response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function show($fileId) |
|
58
|
|
|
{ |
|
59
|
|
|
$entry = Attachment::findOrFail($fileId); |
|
60
|
|
|
$type = explode('\\', $entry->imageable_type); |
|
61
|
|
|
$modelName = strtolower($type[1]); |
|
62
|
|
|
|
|
63
|
|
|
$file = $modelName.'_'.$entry->imageable_id.'/'.$entry->filename; |
|
64
|
|
|
|
|
65
|
|
|
if ($entry->encrypted) { |
|
66
|
|
|
try { |
|
67
|
|
|
$fileContent = decrypt(Storage::get($file)); |
|
68
|
|
|
} catch (DecryptException $e) { |
|
69
|
|
|
dd($e); |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
$fileContent = Storage::get($file); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return response()->make($fileContent, 200, [ |
|
76
|
|
|
'Content-Type' => $entry->mime, |
|
77
|
|
|
'Content-Disposition' => 'attachment; filename="'.$entry->filename.'"', |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Remove the specified resource from storage. |
|
83
|
|
|
* |
|
84
|
|
|
* @param $fileId |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
87
|
|
|
*/ |
|
88
|
|
|
public function destroy($fileId) |
|
89
|
|
|
{ |
|
90
|
|
|
$file = Attachment::findOrFail($fileId); |
|
91
|
|
|
$file->delete(); |
|
92
|
|
|
|
|
93
|
|
|
$type = explode('\\', $file->imageable_type); |
|
94
|
|
|
$modelName = strtolower($type[1]); |
|
95
|
|
|
|
|
96
|
|
|
$fileLocation = 'app/'.$modelName.'_'.$file->imageable_id.'/'.$file->filename; |
|
97
|
|
|
Storage::delete($fileLocation); |
|
98
|
|
|
|
|
99
|
|
|
return back(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|