Uploads.php
2.36 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
<?php
namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Common\Media;
use File;
use Storage;
class Uploads extends Controller
{
/**
* Get the specified resource.
*
* @param $id
* @return mixed
*/
public function get($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($media)) {
return false;
}
return response()->file($path);
}
/**
* Download the specified resource.
*
* @param $id
* @return mixed
*/
public function download($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($media)) {
return false;
}
return response()->download($path);
}
/**
* Destroy the specified resource.
*
* @param $id
* @return callable
*/
public function destroy($id, Request $request)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($media)) {
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
flash($message)->warning();
return back();
}
$media->delete(); //will not delete files
File::delete($path);
if (!empty($request->input('page'))) {
switch ($request->input('page')) {
case 'setting':
setting()->set($request->input('key'), '');
setting()->save();
break;
default;
}
}
return back();
}
/**
* Get the full path of resource.
*
* @param $media
* @return boolean|string
*/
protected function getPath($media)
{
$path = $media->basename;
if (!empty($media->directory)) {
$folders = explode('/', $media->directory);
// Check if company can access media
if ($folders[0] != session('company_id')) {
return false;
}
$path = $media->directory . '/' . $media->basename;
}
if (!Storage::exists($path)) {
return false;
}
$full_path = Storage::path($path);
return $full_path;
}
}