Skip to content

Commit aa1216b

Browse files
committed
add upload validation error messages
1 parent 9d003e7 commit aa1216b

File tree

7 files changed

+42
-43
lines changed

7 files changed

+42
-43
lines changed

src/controllers/UploadController.php

+18-37
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class UploadController extends LfmController {
1717

18+
private $default_file_types = ['application/pdf'];
19+
private $default_image_types = ['image/jpeg', 'image/png', 'image/gif'];
20+
1821
/**
1922
* Upload an image/file and (for images) create thumbnail
2023
*
@@ -26,21 +29,17 @@ public function upload()
2629
try {
2730
$res = $this->uploadValidator();
2831
if (true !== $res) {
29-
return "Invalid upload request";
32+
return Lang::get('laravel-filemanager::lfm.error-invalid');
3033
}
3134
} catch (\Exception $e) {
3235
return $e->getMessage();
3336
}
3437

35-
if (!Input::hasFile('upload')) {
36-
return Lang::get('laravel-filemanager::lfm.error-file-empty');
37-
}
38-
3938
$file = Input::file('upload');
4039

4140
$new_filename = $this->getNewName($file);
4241

43-
$dest_path = parent::getPath();
42+
$dest_path = parent::getPath('directory');
4443

4544
if (File::exists($dest_path . $new_filename)) {
4645
return Lang::get('laravel-filemanager::lfm.error-file-exist');
@@ -64,55 +63,37 @@ private function uploadValidator()
6463
{
6564
// when uploading a file with the POST named "upload"
6665

67-
$file_array = Input::file();
6866
$expected_file_type = $this->file_type;
6967
$is_valid = false;
7068

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'];
69+
$file = Input::file('upload');
8070
if (!$file) {
81-
throw new \Exception('Unexpected, nothing in "upload"');
71+
throw new \Exception(Lang::get('laravel-filemanager::lfm.error-file-empty'));
8272
}
8373
if (!$file instanceof UploadedFile) {
84-
throw new \Exception('The uploaded file should be an instance of UploadedFile');
74+
throw new \Exception(Lang::get('laravel-filemanager::lfm.error-instance'));
8575
}
8676

8777
$mimetype = $file->getMimeType();
8878

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');
79+
if ($expected_file_type === 'Files') {
80+
$config_name = 'lfm.valid_file_mimetypes';
81+
$valid_mimetypes = Config::get($config_name, $this->default_file_types);
82+
} else {
83+
$config_name = 'lfm.valid_image_mimetypes';
84+
$valid_mimetypes = Config::get($config_name, $this->default_image_types);
9685
}
9786

98-
if (in_array($mimetype, $valid_file_mimetypes) && $expected_file_type === 'Files') {
99-
$is_valid = true;
87+
if (!is_array($valid_mimetypes)) {
88+
throw new \Exception('Config : ' . $config_name . ' is not set correctly');
10089
}
10190

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)) {
91+
if (in_array($mimetype, $valid_mimetypes)) {
11192
$is_valid = true;
11293
}
11394

11495
if (false === $is_valid) {
115-
throw new \Exception('Unexpected MimeType: ' . $mimetype);
96+
throw new \Exception(Lang::get('laravel-filemanager::lfm.error-mime') . $mimetype);
11697
}
11798
return $is_valid;
11899
}

src/lang/en/lfm.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'error-delete' => 'You cannot delete this folder because it is not empty!',
4141
'error-folder-name' => 'Folder name cannot be empty!',
4242
'error-folder-exist'=> 'A folder with this name already exists!',
43+
'error-mime' => 'Unexpected MimeType: ',
44+
'error-instance' => 'The uploaded file should be an instance of UploadedFile',
45+
'error-invalid' => 'Invalid upload request',
4346

4447
'btn-upload' => 'Upload File',
4548
'btn-uploading' => 'Uploading...',

src/lang/fr/lfm.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'error-delete' => "Vous ne pouvez pas supprimer ce dossier car il n'est pas vide",
4141
'error-folder-name' => 'Le nom du dossier ne peut pas être vide',
4242
'error-folder-exist'=> 'Un dossier avec ce nom existe déjà !',
43+
'error-mime' => 'Unexpected MimeType: ',
44+
'error-instance' => 'The uploaded file should be an instance of UploadedFile',
45+
'error-invalid' => 'Invalid upload request',
4346

4447
'btn-upload' => 'Envoyer le fichier',
4548
'btn-uploading' => 'Envoi...',

src/lang/pt-BR/lfm.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'error-delete' => 'Você não pode deletar esta pasta, pois ela não está vazia!',
4141
'error-folder-name' => 'Nome da pasta não pode ser vazio!',
4242
'error-folder-exist'=> 'Uma pasta com este nome já existe!',
43+
'error-mime' => 'Unexpected MimeType: ',
44+
'error-instance' => 'The uploaded file should be an instance of UploadedFile',
45+
'error-invalid' => 'Invalid upload request',
4346

4447
'btn-upload' => 'Enviar Arquivo',
4548
'btn-uploading' => 'Enviando...',

src/lang/tr/lfm.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'error-delete' => 'Klasör boş olmadığından, klasörü silemezsiniz!',
4141
'error-folder-name' => 'Klasör adı yazılmalıdır!',
4242
'error-folder-exist'=> 'Bu adda bir klasör zaten var!',
43+
'error-mime' => 'Unexpected MimeType: ',
44+
'error-instance' => 'The uploaded file should be an instance of UploadedFile',
45+
'error-invalid' => 'Invalid upload request',
4346

4447
'btn-upload' => 'Yükle',
4548
'btn-uploading' => 'Yükleniyor...',

src/lang/zh-CN/lfm.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@
3434
'message-name' => '文件夹名称:',
3535
'message-rename' => '重命名为:',
3636

37-
'error-rename' => '名稱重複,請重新輸入!',
38-
'error-file-empty' => '請選擇檔案!',
39-
'error-file-exist' => '相同檔名的檔案已存在!',
40-
'error-delete' => '資料夾未清空,無法刪除!',
41-
'error-folder-name' => '請輸入資料夾名稱!',
42-
'error-folder-exist'=> '相同名稱的資料夾已存在!',
37+
'error-rename' => '名称重复,请重新输入!',
38+
'error-file-empty' => '请选择档案!',
39+
'error-file-exist' => '相同档名的档案已存在!',
40+
'error-delete' => '资料夹未清空,无法删除!',
41+
'error-folder-name' => '请输入资料夹名称!',
42+
'error-folder-exist'=> '相同名称的资料夹已存在!',
43+
'error-mime' => 'Mime 格式错误 : ',
44+
'error-instance' => '上传档案的 instance 应为 UploadedFile',
45+
'error-invalid' => '验证失败,上传未成功',
4346

4447
'btn-upload' => '上传',
4548
'btn-uploading' => '上传中...',

src/lang/zh-TW/lfm.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
'error-delete' => '資料夾未清空,無法刪除!',
4141
'error-folder-name' => '請輸入資料夾名稱!',
4242
'error-folder-exist'=> '相同名稱的資料夾已存在!',
43+
'error-mime' => 'Mime 格式錯誤 : ',
44+
'error-instance' => '上傳檔案的 instance 應為 UploadedFile',
45+
'error-invalid' => '驗證失敗,上傳未成功',
4346

4447
'btn-upload' => '上傳',
4548
'btn-uploading' => '上傳中...',

0 commit comments

Comments
 (0)