From c37091d0f12256801e42a2dcb299992e07a2db15 Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 25 Dec 2016 23:21:13 +0800 Subject: [PATCH] fix showing empty file when UploadFile::error !== UPLOAD_ERR_OK --- app/Http/Controllers/SkinlibController.php | 11 ++++++++++- app/Services/Utils.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/SkinlibController.php b/app/Http/Controllers/SkinlibController.php index 7f993297..0a9a533e 100644 --- a/app/Http/Controllers/SkinlibController.php +++ b/app/Http/Controllers/SkinlibController.php @@ -10,6 +10,7 @@ use Session; use App\Models\User; use App\Models\Texture; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; use App\Exceptions\PrettyPageException; use App\Services\Repositories\UserRepository; @@ -138,7 +139,9 @@ class SkinlibController extends Controller public function handleUpload(Request $request) { - $this->checkUpload($request); + if (($response = $this->checkUpload($request)) instanceof JsonResponse) { + return $response; + } $t = new Texture(); $t->name = $request->input('name'); @@ -250,6 +253,12 @@ class SkinlibController extends Controller */ private function checkUpload(Request $request) { + if ($file = $request->files->get('file')) { + if ($file->getError() !== UPLOAD_ERR_OK) { + return json(Utils::convertUploadFileError($file->getError()), $file->getError()); + } + } + $this->validate($request, [ 'name' => 'required|no_special_chars', 'file' => 'required|max:'.option('max_upload_file_size'), diff --git a/app/Services/Utils.php b/app/Services/Utils.php index 25662448..7034c0fe 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -140,4 +140,20 @@ class Utils return $str; } + public static function convertUploadFileError($errno = 0) + { + $phpFileUploadErrors = [ + 0 => 'There is no error, the file uploaded with success', + 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', + 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', + 3 => 'The uploaded file was only partially uploaded', + 4 => 'No file was uploaded', + 6 => 'Missing a temporary folder', + 7 => 'Failed to write file to disk.', + 8 => 'A PHP extension stopped the file upload.', + ]; + + return $phpFileUploadErrors[$errno]; + } + }