|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Uccello\Core\Http\Controllers\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Uccello\Core\Models\Domain; |
|
7
|
|
|
use Uccello\Core\Models\Module; |
|
8
|
|
|
use Storage; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class DownloadController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
protected $viewName = null; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Check user permissions |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function checkPermissions() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware('uccello.permissions:retrieve'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Process and display asked page |
|
25
|
|
|
* @param Domain|null $domain |
|
26
|
|
|
* @param Module $module |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Illuminate\Http\Response |
|
30
|
|
|
*/ |
|
31
|
|
|
public function process(?Domain $domain, Module $module, Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
// Pre-process |
|
34
|
|
|
$this->preProcess($domain, $module, $request); |
|
35
|
|
|
|
|
36
|
|
|
// Get file data from request |
|
37
|
|
|
$fileData = $this->getFileDataFromRequest(); |
|
38
|
|
|
$fileName = $fileData['name']; |
|
39
|
|
|
$filePath = $fileData['path']; |
|
40
|
|
|
|
|
41
|
|
|
// Check if file exists |
|
42
|
|
|
if (!Storage::exists($filePath)) { |
|
43
|
|
|
abort(404); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Download file |
|
47
|
|
|
return Storage::download($filePath, $fileName); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get file name and path from request data |
|
52
|
|
|
* |
|
53
|
|
|
* @return array|\Illuminate\Http\Response |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getFileDataFromRequest() |
|
56
|
|
|
{ |
|
57
|
|
|
$fileName = null; |
|
58
|
|
|
$filePath = null; |
|
59
|
|
|
|
|
60
|
|
|
$recordId = (int) request('id'); |
|
61
|
|
|
$fieldColumn = request('field'); |
|
62
|
|
|
|
|
63
|
|
|
// Get record |
|
64
|
|
|
$record = $this->getRecord($recordId); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
// Retrieve data from field column |
|
67
|
|
|
$fileData = $record->{$fieldColumn}; |
|
68
|
|
|
|
|
69
|
|
|
if ($fileData) { |
|
70
|
|
|
$fileParts = explode(';', $fileData); |
|
71
|
|
|
|
|
72
|
|
|
if (count($fileParts) === 2) { |
|
73
|
|
|
$fileName = $fileParts[0]; |
|
74
|
|
|
$filePath = $fileParts[1]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!$fileName || !$filePath) { |
|
79
|
|
|
abort(404); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return [ |
|
83
|
|
|
'name' => $fileName, |
|
84
|
|
|
'path' => $filePath |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get record by id |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $id |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getRecord(int $id) |
|
95
|
|
|
{ |
|
96
|
|
|
$record = null; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$modelClass = $this->module->model_class; |
|
|
|
|
|
|
99
|
|
|
$record = $modelClass::findOrFail($id); |
|
100
|
|
|
|
|
101
|
|
|
return $record; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.