blessing-skin-server/app/Http/Controllers/TextureController.php

206 lines
8.0 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-21 22:01:57 +08:00
2019-12-14 11:10:37 +08:00
use App\Events\GetAvatarPreview;
use App\Events\GetSkinPreview;
use App\Models\Player;
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
2019-08-08 18:00:11 +08:00
use App\Services\Minecraft;
2019-12-14 11:10:37 +08:00
use Carbon\Carbon;
use Event;
use Exception;
2019-09-10 19:52:17 +08:00
use Illuminate\Support\Arr;
2019-12-14 11:10:37 +08:00
use Option;
use Response;
use Storage;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
2016-07-21 22:01:57 +08:00
class TextureController extends Controller
2016-07-21 22:01:57 +08:00
{
public function json($player_name, $api = '')
2016-07-21 22:01:57 +08:00
{
2016-10-16 18:16:15 +08:00
$player = $this->getPlayerInstance($player_name);
if ($api == 'csl') {
2016-10-30 11:57:26 +08:00
$content = $player->getJsonProfile(Player::CSL_API);
} elseif ($api == 'usm') {
2016-10-30 11:57:26 +08:00
$content = $player->getJsonProfile(Player::USM_API);
2016-08-13 22:14:01 +08:00
} else {
2019-09-10 19:52:17 +08:00
$content = $player->getJsonProfile(option('api_type'));
2016-07-21 22:01:57 +08:00
}
2016-10-30 11:57:26 +08:00
2019-09-10 19:52:17 +08:00
return response($content, 200, [
'Content-type' => 'application/json',
'Last-Modified' => $player->last_modified,
2016-10-30 11:57:26 +08:00
]);
2016-07-21 22:01:57 +08:00
}
public function jsonWithApi($api, $player_name)
{
2016-08-29 15:28:20 +08:00
return $this->json($player_name, $api);
2016-07-21 22:01:57 +08:00
}
public function texture($hash, $headers = [], $message = '')
{
try {
if (Storage::disk('textures')->has($hash)) {
2019-09-10 19:52:17 +08:00
return $this->outputImage(Storage::disk('textures')->get($hash), array_merge([
'Last-Modified' => Storage::disk('textures')->lastModified($hash),
'Accept-Ranges' => 'bytes',
'Content-Length' => Storage::disk('textures')->size($hash),
2018-08-05 16:38:46 +08:00
], $headers));
}
} catch (Exception $e) {
report($e);
2016-08-28 20:33:35 +08:00
}
2018-08-05 16:38:46 +08:00
return abort(404, $message);
2016-08-28 20:33:35 +08:00
}
public function textureWithApi($api, $hash)
{
2016-08-28 20:33:35 +08:00
return $this->texture($hash);
}
2018-07-30 15:13:14 +08:00
public function avatarByTid($tid, $size = 128)
{
if ($t = Texture::find($tid)) {
try {
if (Storage::disk('textures')->has($t->hash)) {
$responses = event(new GetAvatarPreview($t, $size));
if (isset($responses[0]) && $responses[0] instanceof SymfonyResponse) {
return $responses[0]; // @codeCoverageIgnore
} else {
2019-09-10 19:52:17 +08:00
$png = Minecraft::generateAvatarFromSkin(
Storage::disk('textures')->read($t->hash), $size
);
2019-04-19 19:36:36 +08:00
2019-09-10 19:52:17 +08:00
return $this->outputImage(png($png));
2018-07-30 15:13:14 +08:00
}
}
} catch (Exception $e) {
report($e);
}
}
2019-02-28 16:34:16 +08:00
return response()->file(storage_path('static_textures/avatar.png'));
2018-07-30 15:13:14 +08:00
}
2019-12-22 17:28:55 +08:00
public function avatar($uid, $size = 128)
2018-07-30 15:13:14 +08:00
{
2019-12-22 17:28:55 +08:00
$user = User::find($uid);
2016-08-29 14:25:24 +08:00
2016-10-23 11:41:52 +08:00
if ($user) {
2019-03-23 00:20:28 +08:00
return $this->avatarByTid($user->avatar, $size);
2016-08-29 14:25:24 +08:00
}
2019-02-28 16:34:16 +08:00
return response()->file(storage_path('static_textures/avatar.png'));
2016-08-13 22:14:01 +08:00
}
2016-07-21 22:01:57 +08:00
2016-08-13 22:14:01 +08:00
public function preview($tid, $size = 250)
{
2016-08-29 14:25:24 +08:00
if ($t = Texture::find($tid)) {
try {
if (Storage::disk('textures')->has($t->hash)) {
$responses = event(new GetSkinPreview($t, $size));
2016-08-30 10:10:11 +08:00
if (isset($responses[0]) && $responses[0] instanceof \Symfony\Component\HttpFoundation\Response) {
return $responses[0]; // @codeCoverageIgnore
2016-08-30 10:10:11 +08:00
} else {
$binary = Storage::disk('textures')->read($t->hash);
if ($t->type == 'cape') {
2019-09-10 19:52:17 +08:00
$png = Minecraft::generatePreviewFromCape(
$binary, $size * 0.8, $size * 1.125, $size
);
} else {
2019-09-10 19:52:17 +08:00
$png = Minecraft::generatePreviewFromSkin(
$binary, $size, ($t->type == 'alex'), 'both', 4
);
}
2016-08-30 10:10:11 +08:00
2019-09-10 19:52:17 +08:00
return $this->outputImage(png($png));
}
2016-08-30 10:10:11 +08:00
}
} catch (Exception $e) {
report($e);
2016-07-21 22:01:57 +08:00
}
}
2016-08-29 14:25:24 +08:00
2018-02-16 17:31:04 +08:00
// Show this if given texture is invalid.
2019-02-28 16:34:16 +08:00
return response()->file(storage_path('static_textures/broken.png'));
2016-08-13 22:14:01 +08:00
}
2016-07-21 22:01:57 +08:00
public function raw($tid)
{
2019-03-31 11:00:07 +08:00
abort_unless(option('allow_downloading_texture'), 404);
return ($t = Texture::find($tid))
? $this->texture($t->hash)
: abort(404, trans('skinlib.non-existent'));
}
2019-03-16 17:40:04 +08:00
public function avatarByPlayer($size, $name)
2016-10-16 18:16:15 +08:00
{
2019-03-16 17:40:04 +08:00
$player = Player::where('name', $name)->first();
2019-03-31 11:00:07 +08:00
abort_unless($player, 404);
2019-03-16 17:40:04 +08:00
$hash = $player->getTexture('skin');
if (Storage::disk('textures')->has($hash)) {
2019-03-16 18:49:51 +08:00
$png = Minecraft::generateAvatarFromSkin(
Storage::disk('textures')->read($hash),
$size
);
2019-09-10 19:52:17 +08:00
return $this->outputImage(png($png));
2019-03-16 17:40:04 +08:00
}
return abort(404);
}
2019-09-10 19:52:17 +08:00
protected function outputImage($content, $headers = [])
{
$request = request();
$ifNoneMatch = $request->header('If-None-Match');
$eTag = md5($content);
$ifModifiedSince = Carbon::parse($request->header('If-Modified-Since', 0));
$lastModified = Carbon::parse(Arr::pull($headers, 'Last-Modified', time()));
if ($eTag === $ifNoneMatch || $lastModified <= $ifModifiedSince) {
return response(null)->withHeaders($headers)->setNotModified();
}
return response($content, 200, $headers)->withHeaders([
'Content-Type' => 'image/png',
'ETag' => $eTag,
'Last-Modified' => $lastModified->toRfc7231String(),
'Cache-Control' => 'max-age='.option('cache_expire_time').', public',
]);
}
2019-03-16 17:43:57 +08:00
protected function getPlayerInstance($player_name)
2019-03-16 17:40:04 +08:00
{
2019-03-16 17:43:57 +08:00
$player = Player::where('name', $player_name)->first();
2019-03-31 11:00:07 +08:00
abort_if($player->isBanned(), 403, trans('general.player-banned'));
2019-04-19 19:36:36 +08:00
2016-10-16 18:16:15 +08:00
return $player;
}
/**
* Default steve skin, base64 encoded.
*
2018-02-23 23:17:29 +08:00
* @see https://minecraft.gamepedia.com/File:Steve_skin.png
2019-12-14 11:10:37 +08:00
*
* @return string
*/
2018-02-23 23:17:29 +08:00
public static function getDefaultSteveSkin()
{
2018-02-23 23:17:29 +08:00
return 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAFDUlEQVR42u2a20sUURzH97G0LKMotPuWbVpslj1olJXdjCgyisowsSjzgrB0gSKyC5UF1ZNQWEEQSBQ9dHsIe+zJ/+nXfM/sb/rN4ZwZ96LOrnPgyxzP/M7Z+X7OZc96JpEISfWrFhK0YcU8knlozeJKunE4HahEqSc2nF6zSEkCgGCyb+82enyqybtCZQWAzdfVVFgBJJNJn1BWFgC49/VpwGVlD0CaxQiA5HSYEwBM5sMAdKTqygcAG9+8coHKY/XXAZhUNgDYuBSPjJL/GkzVVhAEU5tqK5XZ7cnFtHWtq/TahdSw2l0HUisr1UKIWJQBAMehDuqiDdzndsP2EZECAG1ZXaWMwOCODdXqysLf++uXUGv9MhUHIByDOijjdiSAoH3ErANQD73C7TXXuGOsFj1d4YH4OTJAEy8y9Hd0mCaeZ5z8dfp88zw1bVyiYhCLOg1ZeAqC0ybaDttHRGME1DhDeVWV26u17lRAPr2+mj7dvULfHw2q65fhQRrLXKDfIxkau3ZM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