Skip to content

Commit 3f0a9c5

Browse files
committed
Refactoring
1 parent 21d7cde commit 3f0a9c5

File tree

4 files changed

+101
-120
lines changed

4 files changed

+101
-120
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php namespace Tsawler\Laravelfilemanager\controllers;
2+
3+
use App\Http\Controllers\Controller;
4+
use Illuminate\Support\Facades\Config;
5+
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Facades\Input;
7+
use Illuminate\Support\Facades\Session;
8+
9+
/**
10+
* Class CropController
11+
* @package Tsawler\Laravelfilemanager\controllers
12+
*/
13+
class DeleteController extends Controller {
14+
15+
/**
16+
* @var
17+
*/
18+
protected $file_location;
19+
20+
21+
/**
22+
* constructor
23+
*/
24+
function __construct()
25+
{
26+
if (Session::get('lfm_type') == "Images")
27+
$this->file_location = Config::get('lfm.images_dir');
28+
else
29+
$this->file_location = Config::get('lfm.files_dir');
30+
}
31+
32+
33+
/**
34+
* Delete image and associated thumbnail
35+
*
36+
* @return mixed
37+
*/
38+
public function getDelete()
39+
{
40+
$to_delete = Input::get('items');
41+
$base = Input::get("base");
42+
43+
if ($base != "/")
44+
{
45+
if (File::isDirectory(base_path() . "/" . $this->file_location . $to_delete))
46+
{
47+
File::delete(base_path() . "/" . $this->file_location . $base . "/" . $to_delete);
48+
49+
return "OK";
50+
} else
51+
{
52+
if (File::exists(base_path() . "/" . $this->file_location . $base . "/" . $to_delete))
53+
{
54+
File::delete(base_path() . "/" . $this->file_location . $base . "/" . $to_delete);
55+
56+
if (Session::get('lfm_type') == "Images'")
57+
File::delete(base_path() . "/" . $this->file_location . $base . "/" . "thumbs/" . $to_delete);
58+
59+
return "OK";
60+
} else {
61+
return base_path() . "/" . $this->file_location . $base . "/" . $to_delete
62+
. " not found!";
63+
}
64+
}
65+
} else
66+
{
67+
$file_name = base_path() . "/" . $this->file_location . $to_delete;
68+
if (File::isDirectory($file_name))
69+
{
70+
// make sure the directory is empty
71+
if (sizeof(File::files($file_name)) == 0)
72+
{
73+
File::deleteDirectory($file_name);
74+
75+
return "OK";
76+
} else
77+
{
78+
return "You cannot delete this folder because it is not empty!";
79+
}
80+
} else
81+
{
82+
if (File::exists($file_name))
83+
{
84+
File::delete($file_name);
85+
if (Session::get('lfm_type') == "Images'")
86+
File::delete(base_path() . "/" . $this->file_location . "thumbs/" . $to_delete);
87+
88+
return "OK";
89+
}
90+
}
91+
}
92+
}
93+
94+
}

src/controllers/LfmController.php

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -69,129 +69,13 @@ public function show()
6969
}
7070

7171

72-
/**
73-
* Upload an image and create thumbnail
74-
*
75-
* @param UploadRequest $request
76-
* @return string
77-
*/
78-
public function upload()
79-
{
80-
if (Session::get('type') == "Image")
81-
{
82-
$file = Input::file('file_to_upload');
83-
$working_dir = Input::get('working_dir');
84-
$destinationPath = base_path() . "/" . $this->file_location;
85-
86-
if (strlen($working_dir) > 1)
87-
{
88-
$destinationPath .= $working_dir . "/";
89-
}
90-
91-
$filename = $file->getClientOriginalName();
92-
$extension = $file->getClientOriginalExtension();
93-
94-
$new_filename = Str::slug(str_replace($extension, '', $filename)) . "." . $extension;
95-
96-
Input::file('file_to_upload')->move($destinationPath, $new_filename);
97-
98-
if (!File::exists($destinationPath . "thumbs"))
99-
{
100-
File::makeDirectory($destinationPath . "thumbs");
101-
}
102-
103-
$thumb_img = Image::make($destinationPath . $new_filename);
104-
$thumb_img->fit(200, 200)
105-
->save($destinationPath . "thumbs/" . $new_filename);
106-
unset($thumb_img);
107-
108-
return "OK";
109-
} else
110-
{
111-
$file = Input::file('file_to_upload');
112-
$working_dir = Input::get('working_dir');
113-
$destinationPath = base_path() . "/" . $this->file_location;
114-
115-
if (strlen($working_dir) > 1)
116-
{
117-
$destinationPath .= $working_dir . "/";
118-
}
119-
120-
$filename = $file->getClientOriginalName();
121-
$extension = $file->getClientOriginalExtension();
122-
123-
$new_filename = Str::slug(str_replace($extension, '', $filename)) . "." . $extension;
124-
125-
Input::file('file_to_upload')->move($destinationPath, $new_filename);
126-
127-
return "OK";
128-
}
129-
130-
}
131-
132-
133-
/**
134-
* Delete image and associated thumbnail
135-
*
136-
* @return mixed
137-
*/
138-
public function getDelete()
139-
{
140-
$to_delete = Input::get('items');
141-
$base = Input::get("base");
142-
143-
if ($base != "/")
144-
{
145-
if (File::isDirectory(base_path() . "/" . Config::get('lfm.images_dir') . $to_delete))
146-
{
147-
File::delete(base_path() . "/" . Config::get('lfm.images_dir') . $base . "/" . $to_delete);
148-
149-
return "OK";
150-
} else
151-
{
152-
if (File::exists(base_path() . "/" . Config::get('lfm.images_dir') . $base . "/" . $to_delete))
153-
{
154-
File::delete(base_path() . "/" . Config::get('lfm.images_dir') . $base . "/" . $to_delete);
155-
File::delete(base_path() . "/" . Config::get('lfm.images_dir') . $base . "/" . "thumbs/" . $to_delete);
156-
157-
return "OK";
158-
}
159-
}
160-
} else
161-
{
162-
$file_name = base_path() . "/" . Config::get('lfm.images_dir') . $to_delete;
163-
if (File::isDirectory($file_name))
164-
{
165-
// make sure the directory is empty
166-
if (sizeof(File::files($file_name)) == 0)
167-
{
168-
File::deleteDirectory($file_name);
169-
170-
return "OK";
171-
} else
172-
{
173-
return "You cannot delete this folder because it is not empty!";
174-
}
175-
} else
176-
{
177-
if (File::exists($file_name))
178-
{
179-
File::delete($file_name);
180-
File::delete(base_path() . "/" . Config::get('lfm.images_dir') . "thumbs/" . $to_delete);
181-
182-
return "OK";
183-
}
184-
}
185-
}
186-
}
187-
188-
18972

19073
public function getFiles()
19174
{
19275
return "List of files";
19376
}
19477

78+
19579
/**
19680
* Get the images to load for a selected folder
19781
*

src/controllers/UploadController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function __construct()
3333

3434

3535
/**
36-
* Upload an image and create thumbnail
36+
* Upload an image/file and (for images) create thumbnail
3737
*
3838
* @param UploadRequest $request
3939
* @return string

src/routes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
return \Illuminate\Support\Facades\View::make('editor');
55
});
66

7-
// general routes
7+
// Show LFM
88
Route::get('/laravel-filemanager', 'Tsawler\Laravelfilemanager\controllers\LfmController@show');
9-
Route::get('/laravel-filemanager/delete', 'Tsawler\Laravelfilemanager\controllers\LfmController@getDelete');
9+
1010

1111
// upload
1212
Route::any('/laravel-filemanager/upload', 'Tsawler\Laravelfilemanager\controllers\UploadController@upload');
@@ -32,3 +32,6 @@
3232

3333
// download
3434
Route::get('/laravel-filemanager/download', 'Tsawler\Laravelfilemanager\controllers\DownloadController@getDownload');
35+
36+
// delete
37+
Route::get('/laravel-filemanager/delete', 'Tsawler\Laravelfilemanager\controllers\DeleteController@getDelete');

0 commit comments

Comments
 (0)