Skip to content

Commit c9f8c62

Browse files
committed
Implement valid_image_mimetypes and valid_file_mimetypes #32 / Support pdf/docs upload #29
Also remove Session, I don't see why filemanager need session.
1 parent f7ae36c commit c9f8c62

10 files changed

+131
-34
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ In `config/lfm.php` :
165165
'files_dir' => 'public/files/',
166166
'files_url' => '/files/',
167167
// path and url of files
168+
169+
170+
// valid image mimetypes
171+
'valid_image_mimetypes' => [
172+
'image/jpeg',
173+
'image/pjpeg',
174+
'image/png',
175+
'image/gif'
176+
],
177+
178+
179+
// valid file mimetypes (only when '/laravel-filemanager?type=Files')
180+
'valid_file_mimetypes' => [
181+
'image/jpeg',
182+
'image/pjpeg',
183+
'image/png',
184+
'image/gif',
185+
'application/pdf',
186+
'text/plain'
187+
],
168188
```
169189

170190
## Customization

src/config/lfm.php

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020
'files_dir' => 'public/files/',
2121
'files_url' => '/files/',
2222

23+
// available since v1.3.0
24+
'valid_image_mimetypes' => [
25+
'image/jpeg',
26+
'image/pjpeg',
27+
'image/png',
28+
'image/gif'
29+
],
30+
31+
// available since v1.3.0
32+
// only when '/laravel-filemanager?type=Files'
33+
'valid_file_mimetypes' => [
34+
'image/jpeg',
35+
'image/pjpeg',
36+
'image/png',
37+
'image/gif',
38+
'application/pdf',
39+
'text/plain',
40+
],
41+
2342
'file_type_array' => [
2443
'pdf' => 'Adobe Acrobat',
2544
'docx' => 'Microsoft Word',

src/controllers/DeleteController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
7-
use Illuminate\Support\Facades\Session;
87
use Lang;
98

109
/**
@@ -43,7 +42,7 @@ public function getDelete()
4342

4443
File::delete($file_to_delete);
4544

46-
if (Session::get('lfm_type') == 'Images') {
45+
if ('Images' === $this->file_type) {
4746
File::delete($thumb_to_delete);
4847
}
4948

src/controllers/DownloadController.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\Input;
66
use Illuminate\Support\Facades\Response;
7-
use Illuminate\Support\Facades\Session;
87

98
/**
109
* Class DownloadController

src/controllers/FolderController.php

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
7-
use Illuminate\Support\Facades\Session;
87
use Illuminate\Support\Facades\View;
98
use Illuminate\Support\Str;
109
use Lang;

src/controllers/ItemsController.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Support\Facades\Input;
77
use Illuminate\Support\Facades\Redirect;
88
use Illuminate\Support\Str;
9-
use Illuminate\Support\Facades\Session;
109
use Illuminate\Support\Facades\View;
1110
use Intervention\Image\Facades\Image;
1211

src/controllers/LfmController.php

+19-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
7-
use Illuminate\Support\Facades\Session;
7+
use Illuminate\Support\Facades\Request;
88
use Illuminate\Support\Facades\View;
99
use Intervention\Image\Facades\Image;
1010

@@ -17,16 +17,27 @@ class LfmController extends Controller {
1717
/**
1818
* @var
1919
*/
20-
public $file_location;
21-
public $dir_location;
20+
public $file_location = null;
21+
public $dir_location = null;
22+
public $file_type = null;
2223

2324

2425
/**
2526
* Constructor
2627
*/
2728
public function __construct()
2829
{
29-
$this->setPathAndType();
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+
}
3041

3142
$this->checkMyFolderExists();
3243

@@ -48,7 +59,8 @@ public function show()
4859
}
4960

5061
return View::make('laravel-filemanager::index')
51-
->with('working_dir', $working_dir);
62+
->with('working_dir', $working_dir)
63+
->with('file_type', $this->file_type);
5264
}
5365

5466

@@ -57,22 +69,6 @@ public function show()
5769
*****************************/
5870

5971

60-
private function setPathAndType()
61-
{
62-
// dd('type:'.Input::get('type'));
63-
64-
if (Input::has('type') && Input::get('type') === 'Files') {
65-
Session::put('lfm_type', 'Files');
66-
Session::put('lfm.file_location', Config::get('lfm.files_dir'));
67-
Session::put('lfm.dir_location', Config::get('lfm.files_url'));
68-
} else if (Input::has('type') && Input::get('type') === 'Images') {
69-
Session::put('lfm_type', 'Images');
70-
Session::put('lfm.file_location', Config::get('lfm.images_dir'));
71-
Session::put('lfm.dir_location', Config::get('lfm.images_url'));
72-
}
73-
}
74-
75-
7672
private function checkMyFolderExists()
7773
{
7874
if (\Config::get('lfm.allow_multi_user') === true) {
@@ -122,7 +118,7 @@ private function formatLocation($location, $type = null)
122118

123119
public function getPath($type = null)
124120
{
125-
$path = base_path() . '/' . Session::get('lfm.file_location');
121+
$path = base_path() . '/' . $this->file_location;
126122

127123
$path = $this->formatLocation($path, $type);
128124

@@ -132,7 +128,7 @@ public function getPath($type = null)
132128

133129
public function getUrl($type = null)
134130
{
135-
$url = Session::get('lfm.dir_location');
131+
$url = $this->dir_location;
136132

137133
$url = $this->formatLocation($url, $type);
138134

src/controllers/RenameController.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
7-
use Illuminate\Support\Facades\Session;
87
use Illuminate\Support\Str;
98
use Lang;
109

@@ -45,7 +44,7 @@ public function getRename()
4544

4645
File::move($old_file, $new_file);
4746

48-
if (Session::get('lfm_type') == 'Images') {
47+
if ('Images' === $this->file_type) {
4948
File::move($thumb_path . $old_name, $thumb_path . $new_name);
5049
}
5150

src/controllers/UploadController.php

+68-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use Illuminate\Support\Facades\Config;
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Input;
7-
use Illuminate\Support\Facades\Session;
87
use Illuminate\Support\Str;
98
use Lang;
109
use Intervention\Image\Facades\Image;
10+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1111

1212
/**
1313
* Class UploadController
@@ -23,6 +23,15 @@ class UploadController extends LfmController {
2323
*/
2424
public function upload()
2525
{
26+
try {
27+
$res = $this->uploadValidator();
28+
if (true !== $res) {
29+
return "Invalid upload request";
30+
}
31+
} catch (\Exception $e) {
32+
return $e->getMessage();
33+
}
34+
2635
if (!Input::hasFile('upload')) {
2736
return Lang::get('laravel-filemanager::lfm.error-file-empty');
2837
}
@@ -39,7 +48,7 @@ public function upload()
3948

4049
$file->move($dest_path, $new_filename);
4150

42-
if (Session::get('lfm_type') == 'Images') {
51+
if ('Images' === $this->file_type) {
4352
$this->makeThumb($dest_path, $new_filename);
4453
}
4554

@@ -51,6 +60,63 @@ public function upload()
5160
return 'OK';
5261
}
5362

63+
private function uploadValidator()
64+
{
65+
// when uploading a file with the POST named "upload"
66+
67+
$file_array = Input::file();
68+
$expected_file_type = $this->file_type;
69+
$is_valid = false;
70+
71+
if (!is_array($file_array)) {
72+
throw new \Exception('Incorrect file_array');
73+
}
74+
75+
if (!array_key_exists('upload', $file_array)) {
76+
throw new \Exception('name: "upload" not exists');
77+
}
78+
79+
$file = $file_array['upload'];
80+
if (!$file) {
81+
throw new \Exception('Unexpected, nothing in "upload"');
82+
}
83+
if (!$file instanceof UploadedFile) {
84+
throw new \Exception('The uploaded file should be an instance of UploadedFile');
85+
}
86+
87+
$mimetype = $file->getMimeType();
88+
89+
// File MimeTypes Check
90+
$valid_file_mimetypes = Config::get(
91+
'lfm.valid_file_mimetypes',
92+
['application/pdf']
93+
);
94+
if (!is_array($valid_file_mimetypes)) {
95+
throw new \Exception('valid_file_mimetypes is not set correctly');
96+
}
97+
98+
if (in_array($mimetype, $valid_file_mimetypes) && $expected_file_type === 'Files') {
99+
$is_valid = true;
100+
}
101+
102+
// Image MimeTypes Check
103+
$valid_image_mimetypes = Config::get(
104+
'lfm.valid_image_mimetypes',
105+
['image/jpeg', 'image/png', 'image/gif']
106+
);
107+
if (!is_array($valid_image_mimetypes)) {
108+
throw new \Exception('valid_image_mimetypes is not set correctly');
109+
}
110+
if (in_array($mimetype, $valid_image_mimetypes)) {
111+
$is_valid = true;
112+
}
113+
114+
if (false === $is_valid) {
115+
throw new \Exception('Unexpected MimeType: ' . $mimetype);
116+
}
117+
return $is_valid;
118+
}
119+
54120
private function getNewName($file)
55121
{
56122
$new_filename = $file->getClientOriginalName();

src/views/index.blade.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
</div>
9696
<input type='hidden' name='working_dir' id='working_dir' value='{{$working_dir}}'>
9797
<input type='hidden' name='show_list' id='show_list' value='0'>
98+
<input type='hidden' name='type' value='{{$file_type}}'>
9899
<input type='hidden' name='_token' value='{{csrf_token()}}'>
99100
</form>
100101
</div>
@@ -239,7 +240,7 @@ function download(x) {
239240
function loadItems() {
240241
var type = 'Images';
241242
242-
@if ((Session::has('lfm_type')) && (Session::get('lfm_type') == 'Files'))
243+
@if ('Files' === $file_type)
243244
type = 'Files';
244245
@endif
245246
@@ -363,7 +364,7 @@ function useFile(file) {
363364
364365
var item_url = image_url;
365366
366-
@if ((Session::has('lfm_type')) && (Session::get('lfm_type') != "Images"))
367+
@if ("Images" !== $file_type)
367368
item_url = file_url;
368369
@endif
369370

0 commit comments

Comments
 (0)