Skip to content

Commit ccb5854

Browse files
committed
refactor path and url, make error message translatable
1 parent c4e2522 commit ccb5854

19 files changed

+269
-217
lines changed

src/controllers/CropController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class CropController extends LfmController {
1919
*/
2020
public function getCrop()
2121
{
22-
$dir = Input::get('dir');
22+
$working_dir = Input::get('working_dir');
2323
$image = Input::get('img');
2424

2525
return View::make('laravel-filemanager::crop')
26-
->with('img', Config::get('lfm.images_url') . $dir . "/" . $image)
27-
->with('dir', $dir)
26+
->with('img', parent::getUrl() . $image)
27+
->with('working_dir', $working_dir)
2828
->with('image', $image);
2929
}
3030

@@ -34,22 +34,21 @@ public function getCrop()
3434
*/
3535
public function getCropimage()
3636
{
37-
$dir = Input::get('dir');
38-
$img = Input::get('img');
37+
$image = Input::get('img');
3938
$dataX = Input::get('dataX');
4039
$dataY = Input::get('dataY');
4140
$dataHeight = Input::get('dataHeight');
4241
$dataWidth = Input::get('dataWidth');
4342

4443
// crop image
45-
$image = Image::make(public_path() . $img);
44+
$image = Image::make(public_path() . $image);
4645
$image->crop($dataWidth, $dataHeight, $dataX, $dataY)
47-
->save(public_path() . $img);
46+
->save(public_path() . $image);
4847

4948
// make new thumbnail
50-
$thumb_img = Image::make(public_path() . $img);
49+
$thumb_img = Image::make(public_path() . $image);
5150
$thumb_img->fit(200, 200)
52-
->save(base_path() . "/" . Config::get('lfm.images_dir') . $dir . "/thumbs/" . basename($img));
51+
->save(parent::getPath('thumb') . parent::getFileName($image));
5352
}
5453

5554
}

src/controllers/DeleteController.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
77
use Illuminate\Support\Facades\Session;
8+
use Lang;
89

910
/**
1011
* Class CropController
@@ -20,24 +21,19 @@ class DeleteController extends LfmController {
2021
public function getDelete()
2122
{
2223
$name_to_delete = Input::get('items');
23-
$base = Input::get('base');
2424

25-
$file_path = base_path() . '/' . $this->file_location;
26-
27-
if ($base !== '/') {
28-
$file_path = $file_path . $base . '/';
29-
}
25+
$file_path = parent::getPath();
3026

3127
$file_to_delete = $file_path . $name_to_delete;
32-
$thumb_to_delete = $file_path . 'thumbs/' . $name_to_delete;
28+
$thumb_to_delete = parent::getPath('thumb') . $name_to_delete;
3329

3430
if (!File::exists($file_to_delete)) {
3531
return $file_to_delete . ' not found!';
3632
}
3733

3834
if (File::isDirectory($file_to_delete)) {
3935
if (sizeof(File::files($file_to_delete)) != 0) {
40-
return 'You cannot delete this folder because it is not empty!';
36+
return Lang::get('laravel-filemanager::lfm.error-delete');
4137
}
4238

4339
File::deleteDirectory($file_to_delete);

src/controllers/DownloadController.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ class DownloadController extends LfmController {
1919
*/
2020
public function getDownload()
2121
{
22-
$file_to_download = Input::get('file');
23-
$dir = Input::get('dir');
24-
return Response::download(base_path()
25-
. "/"
26-
. $this->file_location
27-
. $dir
28-
. "/"
29-
. $file_to_download);
22+
return Response::download(parent::getPath() . Input::get('file'));
3023
}
3124

3225
}

src/controllers/FolderController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Session;
88
use Illuminate\Support\Facades\View;
99
use Illuminate\Support\Str;
10+
use Lang;
1011

1112
/**
1213
* Class FolderController
@@ -27,7 +28,7 @@ public function getFolders()
2728
$share_path = $this->file_location . Config::get('lfm.shared_folder_name');
2829
$shared_folders = parent::getDirectories($share_path);
2930

30-
return View::make("laravel-filemanager::tree")
31+
return View::make('laravel-filemanager::tree')
3132
->with('dirs', $directories)
3233
->with('shares', $shared_folders);
3334
}
@@ -42,15 +43,15 @@ public function getAddfolder()
4243
{
4344
$folder_name = Input::get('name');
4445

45-
$path = base_path($this->file_location . Input::get('base')) . "/" . $folder_name;
46+
$path = parent::getPath() . $folder_name;
4647

4748
if (!File::exists($path)) {
4849
File::makeDirectory($path, $mode = 0777, true, true);
49-
return "OK";
50+
return 'OK';
5051
} else if (empty($folder_name)) {
51-
return 'Folder name cannot be empty!';
52+
return Lang::get('laravel-filemanager::lfm.error-folder-name');
5253
} else {
53-
return "A folder with this name already exists!";
54+
return Lang::get('laravel-filemanager::lfm.error-folder-exist');
5455
}
5556
}
5657

src/controllers/ItemsController.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,17 @@ class ItemsController extends LfmController {
2424
*/
2525
public function getItems()
2626
{
27-
$path = $this->file_location;
28-
29-
$base = Input::get('base');
30-
31-
$path .= Input::get('base');
32-
3327
$type = Input::get('type');
28+
$view = $this->getView($type);
29+
$path = $this->file_location . Input::get('working_dir');
3430

35-
$files = File::files(base_path($path));
36-
$file_info = $this->getFileInfos($files, $type);
31+
$files = File::files(base_path($path));
32+
$file_info = $this->getFileInfos($files, $type);
3733
$directories = parent::getDirectories($path);
38-
39-
$dir_location = $this->dir_location;
40-
$view = 'laravel-filemanager::images';
41-
42-
if ($type !== 'Images') {
43-
$dir_location = $this->file_location;
44-
$view = 'laravel-filemanager::files';
45-
}
46-
47-
if (Input::get('show_list') == 1) {
48-
$view .= '-list';
49-
}
34+
$thumb_url = parent::getUrl('thumb');
5035

5136
return View::make($view)
52-
->with(compact('files', 'file_info', 'directories', 'base', 'dir_location'));
37+
->with(compact('files', 'file_info', 'directories', 'thumb_url'));
5338
}
5439

5540

@@ -58,8 +43,7 @@ private function getFileInfos($files, $type = 'Images')
5843
$file_info = [];
5944

6045
foreach ($files as $key => $file) {
61-
$path_parts = explode('/', $file);
62-
$file_name = end($path_parts);
46+
$file_name = parent::getFileName($file);
6347
$file_created = filemtime($file);
6448

6549
$file_size = number_format((File::size($file) / 1024), 2, ".", "");
@@ -99,4 +83,19 @@ private function getFileInfos($files, $type = 'Images')
9983
return $file_info;
10084
}
10185

86+
87+
private function getView($type = 'Images')
88+
{
89+
$view = 'laravel-filemanager::images';
90+
91+
if ($type !== 'Images') {
92+
$view = 'laravel-filemanager::files';
93+
}
94+
95+
if (Input::get('show_list') == 1) {
96+
$view .= '-list';
97+
}
98+
99+
return $view;
100+
}
102101
}

src/controllers/LfmController.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function setDirPath()
6464
private function checkMyFolderExists()
6565
{
6666
if (\Config::get('lfm.allow_multi_user') === true) {
67-
$path = base_path($this->file_location . Input::get('base'));
67+
$path = base_path($this->file_location . Input::get('working_dir'));
6868

6969
if (!File::exists($path)) {
7070
File::makeDirectory($path, $mode = 0777, true, true);
@@ -83,24 +83,47 @@ private function checkSharedFolderExists()
8383
}
8484

8585

86+
private function formatLocation($location, $type = null)
87+
{
88+
$working_dir = Input::get('working_dir');
89+
90+
if ($working_dir !== '/') {
91+
$location .= $working_dir . '/';
92+
}
93+
94+
if ($type === 'thumb') {
95+
$location = $location . Config::get('lfm.thumb_folder_name') . '/';
96+
}
97+
98+
return $location;
99+
}
100+
101+
86102
/****************************
87103
*** Shared Functions ***
88104
****************************/
89105

90106

91-
public function getPath()
107+
public function getPath($type = null)
92108
{
93-
$working_dir = Input::get('working_dir');
94109
$path = base_path() . '/' . $this->file_location;
95110

96-
if (strlen($working_dir) !== '/') {
97-
$path .= $working_dir . '/';
98-
}
111+
$path = $this->formatLocation($path, $type);
99112

100113
return $path;
101114
}
102115

103116

117+
public function getUrl($type = null)
118+
{
119+
$url = $this->dir_location;
120+
121+
$url = $this->formatLocation($url, $type);
122+
123+
return $url;
124+
}
125+
126+
104127
public function getDirectories($path)
105128
{
106129
$thumb_folder_name = Config::get('lfm.thumb_folder_name');
@@ -121,6 +144,16 @@ public function getDirectories($path)
121144
}
122145

123146

147+
public function getFileName($file)
148+
{
149+
$path_parts = explode('/', $file);
150+
151+
$filename = end($path_parts);
152+
153+
return $filename;
154+
}
155+
156+
124157
/**
125158
* Show the filemanager
126159
*
@@ -134,16 +167,13 @@ public function show()
134167
Session::put('lfm_type', 'Images');
135168
}
136169

137-
if (Input::has('base')) {
138-
$working_dir = Input::get('base');
139-
$base = $this->file_location . Input::get('base') . "/";
170+
if (Input::has('working_dir')) {
171+
$working_dir = Input::get('working_dir');
140172
} else {
141-
$working_dir = "/";
142-
$base = $this->file_location;
173+
$working_dir = '/';
143174
}
144175

145176
return View::make('laravel-filemanager::index')
146-
->with('base', $base)
147177
->with('working_dir', $working_dir);
148178
}
149179

src/controllers/RenameController.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Input;
77
use Illuminate\Support\Facades\Session;
88
use Illuminate\Support\Str;
9+
use Lang;
910

1011
/**
1112
* Class RenameController
@@ -18,32 +19,23 @@ class RenameController extends LfmController {
1819
*/
1920
public function getRename()
2021
{
21-
$file_to_rename = Input::get('file');
22-
$dir = Input::get('dir');
22+
$old_name = Input::get('file');
2323
$new_name = Input::get('new_name');
2424

25-
$file_path = base_path() . '/' . $this->file_location;
26-
$user_path = $file_path . '/';
25+
$file_path = parent::getPath();
26+
$thumb_path = parent::getPath('thumb');
2727

28-
if ($dir !== '/') {
29-
$user_path = $user_path . $dir . '/';
30-
}
31-
32-
$old_file = $user_path . $file_to_rename;
28+
$old_file = $file_path . $old_name;
3329

3430
if (!File::isDirectory($old_file)) {
3531
$extension = File::extension($old_file);
36-
$new_name = str_replace($extension, '', $new_name) . '.' . $extension;
32+
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
3733
}
3834

39-
$thumb_path = $user_path . Config::get('lfm.thumb_folder_name') . '/';
40-
41-
$new_file = $user_path . $new_name;
42-
$new_thumb = $thumb_path . $new_name;
43-
$old_thumb = $thumb_path . $file_to_rename;
35+
$new_file = $file_path . $new_name;
4436

4537
if (File::exists($new_file)) {
46-
return 'File name already in use!';
38+
return Lang::get('laravel-filemanager::lfm.error-rename');
4739
}
4840

4941
if (File::isDirectory($old_file)) {
@@ -54,7 +46,7 @@ public function getRename()
5446
File::move($old_file, $new_file);
5547

5648
if (Session::get('lfm_type') == 'Images') {
57-
File::move($old_thumb, $new_thumb);
49+
File::move($thumb_path . $old_name, $thumb_path . $new_name);
5850
}
5951

6052
return 'OK';

0 commit comments

Comments
 (0)