Skip to content

Commit 1fe0ba1

Browse files
committed
Merge pull request #70 from amin101/add_filesize
Add filesize to upload validation
2 parents 936d8be + d321a82 commit 1fe0ba1

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

src/config/lfm.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
return [
44
// If true, the uploaded file will be renamed to uniqid() + file extension.
5-
'rename_file' => true,
5+
'rename_file' => false,
66

77
// If rename_file set to false and this set to true, then non-alphanumeric characters in filename will be replaced.
88
'alphanumeric_filename' => true,
@@ -12,7 +12,7 @@
1212
'use_package_routes' => true,
1313

1414
// For laravel 5.2, please set to ['web', 'auth']
15-
'middlewares' => ['auth'],
15+
'middlewares' => ['web','auth'],
1616

1717
// Add prefix for routes
1818
'prefix' => 'laravel-filemanager',
@@ -35,6 +35,9 @@
3535
'files_dir' => 'public/files/',
3636
'files_url' => '/files/',
3737

38+
'max_image_size' => 500,
39+
'max_file_size' => 1000,
40+
3841
// available since v1.3.0
3942
'valid_image_mimetypes' => [
4043
'image/jpeg',

src/controllers/LfmController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LfmController extends Controller {
1717
public $file_location = null;
1818
public $dir_location = null;
1919
public $file_type = null;
20-
20+
protected $user;
2121

2222
/**
2323
* Constructor
@@ -38,6 +38,7 @@ public function __construct()
3838

3939
$this->checkDefaultFolderExists('user');
4040
$this->checkDefaultFolderExists('share');
41+
$this->user = \Auth::user();
4142
}
4243

4344

@@ -83,8 +84,10 @@ private function formatLocation($location, $type = null, $get_thumb = false)
8384
{
8485
if ($type === 'share') {
8586
return $location . Config::get('lfm.shared_folder_name');
87+
8688
} elseif ($type === 'user') {
8789
return $location . $this->getUserSlug();
90+
8891
}
8992

9093
$working_dir = Input::get('working_dir');

src/controllers/UploadController.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class UploadController extends LfmController {
1919

2020
private $default_file_types = ['application/pdf'];
2121
private $default_image_types = ['image/jpeg', 'image/png', 'image/gif'];
22+
// unit is assumed to be kb
23+
private $default_max_file_size = 1000;
24+
private $default_max_image_size = 500;
2225

2326
/**
2427
* Upload an image/file and (for images) create thumbnail
@@ -73,7 +76,6 @@ public function upload()
7376
private function uploadValidator()
7477
{
7578
// when uploading a file with the POST named "upload"
76-
7779
$expected_file_type = $this->file_type;
7880
$is_valid = false;
7981
$force_invalid = false;
@@ -95,43 +97,43 @@ private function uploadValidator()
9597

9698
$mimetype = $file->getMimeType();
9799

100+
// size to kb unit is needed
101+
$size = $file->getSize() / 1000;
102+
98103
if ($expected_file_type === 'Files') {
99-
$config_name = 'lfm.valid_file_mimetypes';
100-
$valid_mimetypes = Config::get($config_name, $this->default_file_types);
104+
$mine_config = 'lfm.valid_file_mimetypes';
105+
$valid_mimetypes = Config::get($mine_config, $this->default_file_types);
106+
$max_size = Config::get('lfm.max_file_size', $this->default_max_file_size);
101107
} else {
102-
$config_name = 'lfm.valid_image_mimetypes';
103-
$valid_mimetypes = Config::get($config_name, $this->default_image_types);
108+
$mine_config = 'lfm.valid_image_mimetypes';
109+
$valid_mimetypes = Config::get($mine_config, $this->default_image_types);
110+
$max_size = Config::get('lfm.max_image_size', $this->default_max_image_size);
104111
}
105112

106113
if (!is_array($valid_mimetypes)) {
107-
$force_invalid = true;
108-
throw new \Exception('Config : ' . $config_name . ' is not set correctly');
109-
}
110-
111-
$is_valid = false;
112-
113-
if (in_array($mimetype, $valid_mimetypes)) {
114-
$is_valid = true;
114+
throw new \Exception('Config : ' . $mine_config . ' is not set correctly');
115115
}
116116

117-
if (false === $is_valid) {
118-
$force_invalid = true;
117+
if (false === in_array($mimetype, $valid_mimetypes)) {
119118
throw new \Exception(Lang::get('laravel-filemanager::lfm.error-mime') . $mimetype);
120119
}
121-
}
122120

123-
if($force_invalid)
124-
{
125-
return false;
121+
if ($size > $max_size) {
122+
throw new \Exception(Lang::get('laravel-filemanager::lfm.error-size') . $mimetype);
123+
}
126124
}
127125

128-
return $is_valid;
126+
return true;
129127
}
130128

131129
private function getNewName($file)
132130
{
133131
$new_filename = trim(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
134132

133+
// $file_full_name = $file->getClientOriginalName();
134+
135+
// $new_filename = substr($file_full_name,0,strrpos($file_full_name,'.'));
136+
135137
if (Config::get('lfm.rename_file') === true) {
136138
$new_filename = uniqid();
137139
} elseif (Config::get('lfm.alphanumeric_filename') === true) {

src/lang/en/lfm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'error-folder-exist'=> 'A folder with this name already exists!',
4545
'error-folder-alnum'=> 'Only alphanumeric folder names are allowed!',
4646
'error-mime' => 'Unexpected MimeType: ',
47+
'error-size' => 'Over limit size:',
4748
'error-instance' => 'The uploaded file should be an instance of UploadedFile',
4849
'error-invalid' => 'Invalid upload request',
4950
'error-other' => 'An error has occured: ',

src/lang/fa/lfm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'error-folder-exist'=> 'یک پوشه با این نام قبلا ایجاد شده است!',
4545
'error-folder-alnum'=> 'Only alphanumeric folder names are allowed!',
4646
'error-mime' => 'پسوند غیرمجاز: ',
47+
'error-size' => 'سایز بیش از حد:',
4748
'error-instance' => 'فایل آپلود شده باید نمونه ای از UploadedFile باشد',
4849
'error-invalid' => 'درخواست آپلود غیرمعتبر',
4950

0 commit comments

Comments
 (0)