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

228 lines
8.4 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
2016-08-29 14:25:24 +08:00
use Event;
use Option;
use Storage;
use Response;
use Exception;
2019-03-23 11:06:36 +08:00
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
2019-08-08 18:00:11 +08:00
use App\Services\Minecraft;
use App\Events\GetSkinPreview;
use App\Events\GetAvatarPreview;
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
{
/**
* Return Player Profile formatted in JSON.
*
* @param string $player_name
* @param string $api
* @return \Illuminate\Http\Response
*/
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 {
2016-10-30 11:57:26 +08:00
$content = $player->getJsonProfile(Option::get('api_type'));
2016-07-21 22:01:57 +08:00
}
2016-10-30 11:57:26 +08:00
2018-08-05 16:38:46 +08:00
return Response::jsonProfile($content, 200, [
'Last-Modified' => strtotime($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)) {
2018-08-05 16:38:46 +08:00
return Response::png(Storage::disk('textures')->get($hash), 200, 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);
}
public function skin($player_name)
2016-08-13 22:14:01 +08:00
{
return $this->getBinaryTextureFromPlayer($player_name, 'skin');
}
2016-08-07 17:09:34 +08:00
public function cape($player_name)
{
return $this->getBinaryTextureFromPlayer($player_name, 'cape');
}
/**
* Get the texture image of given type and player.
*
* @param string $player_name
* @param string $type "steve" or "alex" or "cape".
* @return Response
*/
protected function getBinaryTextureFromPlayer($player_name, $type)
{
2016-10-16 18:16:15 +08:00
$player = $this->getPlayerInstance($player_name);
if ($hash = $player->getTexture($type)) {
2018-08-05 16:38:46 +08:00
return $this->texture($hash, [
'Last-Modified' => strtotime($player->last_modified),
2018-08-05 16:38:46 +08:00
], trans('general.texture-deleted'));
} else {
abort(404, trans('general.texture-not-uploaded', ['type' => $type]));
}
}
// @codeCoverageIgnore
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 {
$png = Minecraft::generateAvatarFromSkin(Storage::disk('textures')->read($t->hash), $size);
2019-04-19 19:36:36 +08:00
return Response::png(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
}
public function avatarByTidWithSize($size, $tid)
{
return $this->avatarByTid($tid, $size);
2016-08-13 22:14:01 +08:00
}
2016-07-21 22:01:57 +08:00
2019-09-07 11:15:23 +08:00
public function avatar($base64_email, $size = 128)
2016-08-13 22:14:01 +08:00
{
2019-08-24 10:22:26 +08:00
$user = User::where('email', base64_decode($base64_email))->first();
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
2019-09-07 11:15:23 +08:00
public function avatarWithSize($size, $base64_email)
2016-07-21 22:01:57 +08:00
{
2019-09-07 11:15:23 +08:00
return $this->avatar($base64_email, $size);
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') {
$png = Minecraft::generatePreviewFromCape($binary, $size * 0.8, $size * 1.125, $size);
} else {
$png = Minecraft::generatePreviewFromSkin($binary, $size, ($t->type == 'alex'), 'both', 4);
}
2016-08-30 10:10:11 +08:00
return Response::png(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 previewWithSize($size, $tid)
{
2016-09-03 21:04:34 +08:00
return $this->preview($tid, $size);
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
);
return Response::png(png($png));
2019-03-16 17:40:04 +08:00
}
return abort(404);
}
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
* @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/TahdSw2l0HUisr1UKI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