1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Unisharp\Laravelfilemanager\controllers; |
4
|
|
|
|
5
|
|
|
use Unisharp\Laravelfilemanager\controllers\Controller; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Illuminate\Support\Facades\File; |
8
|
|
|
use Illuminate\Support\Facades\Input; |
9
|
|
|
use CRUDBooster; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class LfmController |
13
|
|
|
* @package Unisharp\Laravelfilemanager\controllers |
14
|
|
|
*/ |
15
|
|
|
class LfmController extends Controller { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
public $file_location = null; |
21
|
|
|
public $dir_location = null; |
22
|
|
|
public $file_type = null; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->file_type = Input::get('type', 'Images'); // default set to Images. |
31
|
|
|
|
32
|
|
|
if ('Images' === $this->file_type) { |
33
|
|
|
$this->dir_location = Config::get('lfm.images_url'); |
34
|
|
|
$this->file_location = Config::get('lfm.images_dir'); |
35
|
|
|
} elseif ('Files' === $this->file_type) { |
36
|
|
|
$this->dir_location = Config::get('lfm.files_url'); |
37
|
|
|
$this->file_location = Config::get('lfm.files_dir'); |
38
|
|
|
} else { |
39
|
|
|
throw new \Exception('unexpected type parameter'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->checkDefaultFolderExists('user'); |
43
|
|
|
$this->checkDefaultFolderExists('share'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Show the filemanager |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function show() |
53
|
|
|
{ |
54
|
|
|
$working_dir = '/'; |
55
|
|
|
$working_dir .= (Config::get('lfm.allow_multi_user')) ? $this->getUserSlug() : Config::get('lfm.shared_folder_name'); |
56
|
|
|
|
57
|
|
|
$extension_not_found = ! extension_loaded('gd') && ! extension_loaded('imagick'); |
58
|
|
|
|
59
|
|
|
return view('laravel-filemanager::index') |
60
|
|
|
->with('working_dir', $working_dir) |
61
|
|
|
->with('file_type', $this->file_type) |
62
|
|
|
->with('extension_not_found', $extension_not_found); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/***************************** |
67
|
|
|
*** Private Functions *** |
68
|
|
|
*****************************/ |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
private function checkDefaultFolderExists($type = 'share') |
72
|
|
|
{ |
73
|
|
|
if ($type === 'user' && \Config::get('lfm.allow_multi_user') !== true) { |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$path = $this->getPath($type); |
78
|
|
|
|
79
|
|
|
if (!File::exists($path)) { |
80
|
|
|
File::makeDirectory($path, $mode = 0777, true, true); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
private function formatLocation($location, $type = null, $get_thumb = false) |
86
|
|
|
{ |
87
|
|
|
if ($type === 'share') { |
88
|
|
|
return $location . Config::get('lfm.shared_folder_name'); |
89
|
|
|
} elseif ($type === 'user') { |
90
|
|
|
return $location . $this->getUserSlug(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$working_dir = Input::get('working_dir'); |
94
|
|
|
|
95
|
|
|
// remove first slash |
96
|
|
|
if (substr($working_dir, 0, 1) === '/') { |
97
|
|
|
$working_dir = substr($working_dir, 1); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$location .= $working_dir; |
102
|
|
|
|
103
|
|
|
if ($type === 'directory' || $type === 'thumb') { |
104
|
|
|
$location .= '/'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//if user is inside thumbs folder there is no need |
108
|
|
|
// to add thumbs substring to the end of $location |
109
|
|
|
$in_thumb_folder = preg_match('/'.Config::get('lfm.thumb_folder_name').'$/i',$working_dir); |
110
|
|
|
|
111
|
|
|
if ($type === 'thumb' && !$in_thumb_folder) { |
112
|
|
|
$location .= Config::get('lfm.thumb_folder_name') . '/'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $location; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/**************************** |
120
|
|
|
*** Shared Functions *** |
121
|
|
|
****************************/ |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function getUserSlug() |
125
|
|
|
{ |
126
|
|
|
return empty(CRUDBooster::myId()) ? '' : CRUDBooster::myId(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
public function getPath($type = null, $get_thumb = false) |
131
|
|
|
{ |
132
|
|
|
$path = base_path() . '/' . $this->file_location; |
133
|
|
|
|
134
|
|
|
$path = $this->formatLocation($path, $type); |
135
|
|
|
|
136
|
|
|
return $path; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
public function getUrl($type = null) |
141
|
|
|
{ |
142
|
|
|
$url = $this->dir_location; |
143
|
|
|
|
144
|
|
|
$url = $this->formatLocation($url, $type); |
145
|
|
|
|
146
|
|
|
$url = str_replace('\\','/',$url); |
147
|
|
|
|
148
|
|
|
return $url; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
public function getDirectories($path) |
153
|
|
|
{ |
154
|
|
|
$thumb_folder_name = Config::get('lfm.thumb_folder_name'); |
155
|
|
|
$all_directories = File::directories($path); |
156
|
|
|
|
157
|
|
|
$arr_dir = []; |
158
|
|
|
|
159
|
|
|
foreach ($all_directories as $directory) { |
160
|
|
|
$dir_name = $this->getFileName($directory); |
161
|
|
|
|
162
|
|
|
if ($dir_name['short'] !== $thumb_folder_name) { |
163
|
|
|
$arr_dir[] = $dir_name; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $arr_dir; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
public function getFileName($file) |
172
|
|
|
{ |
173
|
|
|
$lfm_dir_start = strpos($file, $this->file_location); |
174
|
|
|
$working_dir_start = $lfm_dir_start + strlen($this->file_location); |
175
|
|
|
$lfm_file_path = substr($file, $working_dir_start); |
176
|
|
|
|
177
|
|
|
$arr_dir = explode('/', $lfm_file_path); |
178
|
|
|
$arr_filename['short'] = end($arr_dir); |
179
|
|
|
$arr_filename['long'] = '/' . $lfm_file_path; |
180
|
|
|
|
181
|
|
|
return $arr_filename; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|