From 776109f487413f02615ccccc6456f33c9291249d Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 28 Aug 2016 20:33:35 +0800 Subject: [PATCH] use Storage facade to manage files --- .gitignore | 2 +- app/Http/Controllers/SkinlibController.php | 12 ++++++---- app/Http/Controllers/TextureController.php | 26 +++++++++++++++------- app/Http/Middleware/CheckAuthenticated.php | 2 +- app/Http/routes.php | 3 ++- app/Models/Player.php | 14 ++++++------ app/Models/User.php | 8 ++++--- app/Providers/BootServiceProvider.php | 5 +++-- app/Services/Utils.php | 15 ++++++++----- config/filesystems.php | 4 ++-- setup/index.php | 4 ++-- 11 files changed, 59 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 187b2020..f7191068 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .env .sass-cache vendor/* -textures/* +storage/textures/* node_modules/* resources/cache/* resources/assets/bower_components/* diff --git a/app/Http/Controllers/SkinlibController.php b/app/Http/Controllers/SkinlibController.php index 4acb3577..d689e8c1 100644 --- a/app/Http/Controllers/SkinlibController.php +++ b/app/Http/Controllers/SkinlibController.php @@ -95,12 +95,16 @@ class SkinlibController extends BaseController public function show() { - if (!isset($_GET['tid'])) Http::abort(404, 'No specified tid.'); + if (!isset($_GET['tid'])) + Http::abort(404, 'No specified tid.'); + $texture = Texture::find($_GET['tid']); - if (!$texture || $texture && !\Storage::exists(BASE_DIR."/textures/".$texture->hash)) { + if (!$texture || $texture && !\Storage::disk('textures')->has($texture->hash)) { if (Option::get('auto_del_invalid_texture') == "1") { - if ($texture) $texture->delete(); + if ($texture) + $texture->delete(); + Http::abort(404, '请求的材质文件已经被删除'); } Http::abort(404, '请求的材质文件已经被删除,请联系管理员删除该条目'); @@ -184,7 +188,7 @@ class SkinlibController extends BaseController // check if file occupied if (Texture::where('hash', $result['hash'])->count() == 1) - \Storage::remove("./textures/".$result['hash']); + \Storage::delete($result['hash']); $this->user->setScore($result->size * Option::get('score_per_storage'), 'plus'); diff --git a/app/Http/Controllers/TextureController.php b/app/Http/Controllers/TextureController.php index 05faa906..e2bdc81b 100644 --- a/app/Http/Controllers/TextureController.php +++ b/app/Http/Controllers/TextureController.php @@ -7,6 +7,7 @@ use App\Models\User; use App\Models\Player; use App\Models\Texture; use App\Exceptions\E; +use Storage; use Minecraft; use Option; use Http; @@ -41,6 +42,19 @@ class TextureController extends BaseController $this->json($player_name, $api); } + public function texture($hash) { + if (Storage::disk('textures')->has($hash)) { + return response(Storage::disk('textures')->get($hash)) + ->header('Content-Type', 'image/png'); + } else { + abort(404); + } + } + + public function textureWithApi($api, $hash) { + return $this->texture($hash); + } + public function skin($player_name, $model = "") { $player_name = Option::get('allow_chinese_playername') ? $GLOBALS['player_name'] : $player_name; @@ -53,7 +67,7 @@ class TextureController extends BaseController if (!$this->checkCache($player_name)) { $model_preference = ($player->getPreference() == "default") ? "steve" : "alex"; $model = ($model == "") ? $model_preference : $model; - echo $player->getBinaryTexture($model); + return $player->getBinaryTexture($model); } } @@ -86,11 +100,12 @@ class TextureController extends BaseController { // output image directly if ($t = Texture::find($tid)) { - $filename = BASE_DIR."/textures/".$t->hash; - if (\Storage::exists($filename)) { + if (\Storage::disk('textures')->has($t->hash)) { header('Content-Type: image/png'); + $filename = BASE_DIR."/storage/textures/{$t->hash}"; + if ($t->type == "cape") { $png = Minecraft::generatePreviewFromCape($filename, $size); imagepng($png); @@ -134,11 +149,6 @@ class TextureController extends BaseController } - public static function redirectTextures($api, $hash) - { - Http::redirectPermanently('../../textures/'.$hash); - } - private function checkCache($player_name) { // Cache friendly diff --git a/app/Http/Middleware/CheckAuthenticated.php b/app/Http/Middleware/CheckAuthenticated.php index 39cfc209..0afb5d5e 100644 --- a/app/Http/Middleware/CheckAuthenticated.php +++ b/app/Http/Middleware/CheckAuthenticated.php @@ -18,7 +18,7 @@ class CheckAuthenticated Session::put('token', $_COOKIE['token']); } - if (session()->has('uid')) { + if (Session::has('uid')) { $user = new User(session('uid')); if (session('token') != $user->getToken()) diff --git a/app/Http/routes.php b/app/Http/routes.php index 8f467bdd..21e0f853 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -132,7 +132,8 @@ Route::group(['middleware' => 'App\Http\Middleware\CheckPlayer } }); -Route::get('/{api}/textures/{hash}', 'TextureController@redirectTextures'); +Route::get('/textures/{hash}', 'TextureController@texture'); +Route::get('/{api}/textures/{hash}', 'TextureController@textureWithApi')->where('api', 'usm|csl'); Route::get('/avatar/{base64_email}.png', 'TextureController@avatar'); Route::get('/avatar/{size}/{base64_email}.png', 'TextureController@avatarWithSize')->where(['base64_email' => '[^\\/]+?']); diff --git a/app/Models/Player.php b/app/Models/Player.php index dce16556..eea8a6de 100644 --- a/app/Models/Player.php +++ b/app/Models/Player.php @@ -82,15 +82,15 @@ class Player public function getBinaryTexture($type) { if ($this->getTexture($type) != "") { - $filename = BASE_DIR."/textures/".$this->getTexture($type); + $hash = $this->getTexture($type); + $path = BASE_DIR."/storage/textures/".$hash; - if (\Storage::exists($filename)) { - header('Content-Type: image/png'); + if (\Storage::disk('textures')->has($hash)) { // Cache friendly - header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT'); - header('Content-Length: '.filesize($filename)); - - return \Storage::get($filename); + return response(\Storage::disk('textures')->get($hash)) + ->header('Content-Type', 'image/png') + ->header('Last-Modified', gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT') + ->header('Content-Length', filesize($path)); } else { \Http::abort(404, '请求的贴图已被删除。'); } diff --git a/app/Models/User.php b/app/Models/User.php index c160c241..0729fd4c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -256,13 +256,15 @@ class User { // output image directly if (!is_null($this->model) && $this->getAvatarId()) { - $texture_path = BASE_DIR."/textures/".Texture::find($this->getAvatarId())->hash; + $hash = Texture::find($this->getAvatarId())->hash; + $path = BASE_DIR."/storage/textures/$hash"; - if (\Storage::has($texture_path)) { - $png = \Minecraft::generateAvatarFromSkin(BASE_DIR."/textures/".Texture::find($this->getAvatarId())->hash, $size); + if (\Storage::disk('textures')->has($hash)) { + $png = \Minecraft::generateAvatarFromSkin($path, $size); header('Content-Type: image/png'); imagepng($png); imagedestroy($png); + return; } } diff --git a/app/Providers/BootServiceProvider.php b/app/Providers/BootServiceProvider.php index 300d14ba..e34dfd6a 100644 --- a/app/Providers/BootServiceProvider.php +++ b/app/Providers/BootServiceProvider.php @@ -42,8 +42,9 @@ class BootServiceProvider extends ServiceProvider \Http::redirect(url('/setup/index.php')); } - if (!is_dir(BASE_DIR.'/textures/')) { - throw new E("检测到 `textures` 文件夹已被删除,请重新运行 安装程序,或者手动放置一个。", -1, true); + if (!is_dir(BASE_DIR.'/storage/textures/')) { + if (!mkdir(BASE_DIR.'/storage/textures/')) + throw new E('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1); } if (config('app.version') != \Option::get('version', '')) { diff --git a/app/Services/Utils.php b/app/Services/Utils.php index 80a47985..d04ff634 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -3,7 +3,7 @@ namespace App\Services; use App\Exceptions\E; -use Blessing\Storage; +use Storage; class Utils { @@ -39,13 +39,18 @@ class Utils */ public static function upload($file) { - $path = BASE_DIR.'/textures/tmp'.time(); + $path = 'tmp'.time(); + $absolute_path = BASE_DIR."/storage/textures/$path"; - if (false === move_uploaded_file($file['tmp_name'], $path)) { + if (false === move_uploaded_file($file['tmp_name'], $absolute_path)) { throw new App\Exceptions\E('Failed to remove uploaded files, please check the permission', 1); } else { - $hash = Storage::hash($path); - Storage::rename($path, BASE_DIR."/textures/$hash"); + $hash = hash_file('sha256', $absolute_path); + + if (!Storage::disk('textures')->has($hash)) { + Storage::disk('textures')->move($path, $hash); + } + return $hash; } } diff --git a/config/filesystems.php b/config/filesystems.php index 75b50022..d897de1a 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -48,9 +48,9 @@ return [ 'root' => storage_path('app'), ], - 'public' => [ + 'textures' => [ 'driver' => 'local', - 'root' => storage_path('app/public'), + 'root' => storage_path('textures'), 'visibility' => 'public', ], diff --git a/setup/index.php b/setup/index.php index 17332897..43eea247 100644 --- a/setup/index.php +++ b/setup/index.php @@ -71,8 +71,8 @@ switch ($step) { $user->register($_POST['password'], Http::getRealIP()); $user->setPermission('2'); - if (!is_dir(BASE_DIR.'/textures/')) { - if (!mkdir(BASE_DIR.'/textures/')) + if (!is_dir(BASE_DIR.'/storage/textures/')) { + if (!mkdir(BASE_DIR.'/storage/textures/')) throw new E('textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。', -1); }