Skip to content

Commit 24cf788

Browse files
committed
add back button, fix directory separator, refactor working_dir usage
1 parent f70c542 commit 24cf788

File tree

7 files changed

+209
-212
lines changed

7 files changed

+209
-212
lines changed

src/controllers/FolderController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ class FolderController extends LfmController {
2121
*/
2222
public function getFolders()
2323
{
24-
$dir_path = parent::getPath();
25-
$directories = parent::getDirectories($dir_path);
24+
$user_path = parent::getPath('user');
25+
$lfm_user_path = parent::getFileName($user_path);
26+
$user_folders = parent::getDirectories($user_path);
2627

27-
$share_path = parent::getPath('share');
28+
$share_path = parent::getPath('share');
29+
$lfm_share_path = parent::getFileName($share_path);
2830
$shared_folders = parent::getDirectories($share_path);
2931

3032
return View::make('laravel-filemanager::tree')
31-
->with('dirs', $directories)
33+
->with('user_dir', $lfm_user_path['long'])
34+
->with('dirs', $user_folders)
35+
->with('share_dir', $lfm_share_path['long'])
3236
->with('shares', $shared_folders);
3337
}
3438

@@ -42,7 +46,7 @@ public function getAddfolder()
4246
{
4347
$folder_name = Input::get('name');
4448

45-
$path = parent::getPath() . $folder_name;
49+
$path = parent::getPath() . DIRECTORY_SEPARATOR . $folder_name;
4650

4751
if (!File::exists($path)) {
4852
File::makeDirectory($path, $mode = 0777, true, true);

src/controllers/ItemsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private function getFileInfos($files, $type = 'Images')
4242
$file_info = [];
4343

4444
foreach ($files as $key => $file) {
45-
$file_name = parent::getFileName($file);
45+
$file_name = parent::getFileName($file)['short'];
4646
$file_created = filemtime($file);
4747
$file_size = number_format((File::size($file) / 1024), 2, ".", "");
4848

src/controllers/LfmController.php

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function __construct()
3939
throw new \Exception('unexpected type parameter');
4040
}
4141

42-
$this->checkMyFolderExists();
43-
44-
$this->checkSharedFolderExists();
42+
$this->checkDefaultFolderExists('user');
43+
$this->checkDefaultFolderExists('share');
4544
}
4645

4746

@@ -52,11 +51,8 @@ public function __construct()
5251
*/
5352
public function show()
5453
{
55-
if (Input::has('working_dir')) {
56-
$working_dir = Input::get('working_dir');
57-
} else {
58-
$working_dir = '/';
59-
}
54+
$working_dir = DIRECTORY_SEPARATOR;
55+
$working_dir .= (Config::get('lfm.allow_multi_user')) ? \Auth::user()->user_field : Config::get('lfm.shared_folder_name');
6056

6157
return View::make('laravel-filemanager::index')
6258
->with('working_dir', $working_dir)
@@ -69,42 +65,38 @@ public function show()
6965
*****************************/
7066

7167

72-
private function checkMyFolderExists()
68+
private function checkDefaultFolderExists($type = 'share')
7369
{
74-
if (\Config::get('lfm.allow_multi_user') === true) {
75-
$path = $this->getPath();
76-
77-
if (!File::exists($path)) {
78-
File::makeDirectory($path, $mode = 0777, true, true);
79-
}
70+
if ($type === 'user' && \Config::get('lfm.allow_multi_user') !== true) {
71+
return;
8072
}
81-
}
82-
8373

84-
private function checkSharedFolderExists()
85-
{
86-
$path = $this->getPath('share');
74+
$path = $this->getPath($type);
8775

8876
if (!File::exists($path)) {
8977
File::makeDirectory($path, $mode = 0777, true, true);
9078
}
9179
}
9280

9381

94-
private function formatLocation($location, $type = null)
82+
private function formatLocation($location, $type = null, $get_thumb = false)
9583
{
9684
if ($type === 'share') {
97-
return $location . Config::get('lfm.shared_folder_name') . '/';
85+
return $location . Config::get('lfm.shared_folder_name');
86+
} elseif ($type === 'user') {
87+
return $location . \Auth::user()->user_field;
9888
}
9989

10090
$working_dir = Input::get('working_dir');
10191

102-
if ($working_dir !== '/') {
103-
$location .= $working_dir . '/';
92+
if (substr($working_dir, 0, 1) === DIRECTORY_SEPARATOR) {
93+
$working_dir = substr($working_dir, 1);
10494
}
10595

96+
$location .= $working_dir;
97+
10698
if ($type === 'thumb') {
107-
$location = $location . Config::get('lfm.thumb_folder_name') . '/';
99+
$location .= Config::get('lfm.thumb_folder_name') . DIRECTORY_SEPARATOR;
108100
}
109101

110102
return $location;
@@ -116,9 +108,9 @@ private function formatLocation($location, $type = null)
116108
****************************/
117109

118110

119-
public function getPath($type = null)
111+
public function getPath($type = null, $get_thumb = false)
120112
{
121-
$path = base_path() . '/' . $this->file_location;
113+
$path = base_path() . DIRECTORY_SEPARATOR . $this->file_location;
122114

123115
$path = $this->formatLocation($path, $type);
124116

@@ -157,10 +149,14 @@ public function getDirectories($path)
157149

158150
public function getFileName($file)
159151
{
160-
$path_parts = explode('/', $file);
152+
$lfm_dir_start = strpos($file, $this->file_location);
153+
$working_dir_start = $lfm_dir_start + strlen($this->file_location);
154+
$lfm_file_path = substr($file, $working_dir_start);
161155

162-
$filename = end($path_parts);
156+
$arr_dir = explode(DIRECTORY_SEPARATOR, $lfm_file_path);
157+
$arr_filename['short'] = end($arr_dir);
158+
$arr_filename['long'] = DIRECTORY_SEPARATOR . $lfm_file_path;
163159

164-
return $filename;
160+
return $arr_filename;
165161
}
166162
}

src/controllers/UploadController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private function makeThumb($dest_path, $new_filename)
138138

139139
$thumb_img = Image::make($dest_path . $new_filename);
140140
$thumb_img->fit(200, 200)
141-
->save($dest_path . $thumb_folder_name . '/' . $new_filename);
141+
->save($dest_path . $thumb_folder_name . DIRECTORY_SEPARATOR . $new_filename);
142142
unset($thumb_img);
143143
}
144144

src/middleware/MultiUser.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,33 @@ public function handle($request, Closure $next)
1212
$slug = \Config::get('lfm.user_field');
1313

1414
\Auth::user()->user_field = \Auth::user()->$slug;
15+
$new_working_dir = DIRECTORY_SEPARATOR . \Auth::user()->user_field;
1516

16-
$base = $request->input('working_dir');
17+
$previous_dir = $request->input('working_dir');
1718

18-
if ($base == null) {
19-
$request->merge(['working_dir' => \Auth::user()->user_field]);
20-
} elseif ($this->wrongDir($base)) {
21-
$request->replace(['working_dir' => \Auth::user()->user_field]);
19+
if ($previous_dir == null) {
20+
$request->merge(['working_dir' => $new_working_dir]);
21+
} elseif (! $this->validDir($previous_dir)) {
22+
$request->replace(['working_dir' => $new_working_dir]);
2223
}
2324
}
2425

2526
return $next($request);
2627
}
2728

28-
private function wrongDir($base)
29+
private function validDir($previous_dir)
2930
{
30-
if (strpos($base, \Config::get('lfm.shared_folder_name')) !== false) {
31-
return false;
31+
if (starts_with($previous_dir, DIRECTORY_SEPARATOR . \Config::get('lfm.shared_folder_name'))) {
32+
return true;
3233
}
3334

34-
if (strpos($base, (string)\Auth::user()->user_field) !== false) {
35-
return false;
35+
if (starts_with($previous_dir, DIRECTORY_SEPARATOR . (string)\Auth::user()->user_field)) {
36+
return true;
3637
}
3738

38-
if (strpos($base, (string)\Auth::user()->user_field) === false) {
39-
return true;
40-
}
39+
// if (strpos($previous_dir, (string)\Auth::user()->user_field) === false) {
40+
// return true;
41+
// }
4142

4243
return false;
4344
}

0 commit comments

Comments
 (0)