use Storage facade to manage files

This commit is contained in:
printempw 2016-08-28 20:33:35 +08:00
parent bd1579d7fb
commit 776109f487
11 changed files with 59 additions and 36 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
.env
.sass-cache
vendor/*
textures/*
storage/textures/*
node_modules/*
resources/cache/*
resources/assets/bower_components/*

View File

@ -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');

View File

@ -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

View File

@ -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())

View File

@ -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' => '[^\\/]+?']);

View File

@ -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, '请求的贴图已被删除。');
}

View File

@ -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;
}
}

View File

@ -42,8 +42,9 @@ class BootServiceProvider extends ServiceProvider
\Http::redirect(url('/setup/index.php'));
}
if (!is_dir(BASE_DIR.'/textures/')) {
throw new E("检测到 `textures` 文件夹已被删除,请重新运行 <a href='".url('setup')."'>安装程序</a>,或者手动放置一个。", -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', '')) {

View File

@ -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;
}
}

View File

@ -48,9 +48,9 @@ return [
'root' => storage_path('app'),
],
'public' => [
'textures' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'root' => storage_path('textures'),
'visibility' => 'public',
],

View File

@ -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);
}