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

226 lines
8.5 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 Minecraft;
use Exception;
2016-10-30 11:57:26 +08:00
use Carbon\Carbon;
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Http\Request;
use App\Events\GetSkinPreview;
use App\Events\GetAvatarPreview;
use App\Exceptions\PrettyPageException;
2016-10-23 11:41:52 +08:00
use App\Services\Repositories\UserRepository;
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
*/
2016-07-21 22:01:57 +08:00
public function json($player_name, $api = "")
{
2016-10-16 18:16:15 +08:00
$player = $this->getPlayerInstance($player_name);
2016-08-13 22:14:01 +08:00
if ($api == "csl") {
2016-10-30 11:57:26 +08:00
$content = $player->getJsonProfile(Player::CSL_API);
2016-08-13 22:14:01 +08:00
} else if ($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
}
2018-08-05 16:38:46 +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) {
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)
], trans('general.texture-deleted'));
} else {
abort(404, trans('general.texture-not-uploaded', ['type' => $type]));
}
2018-07-30 15:13:14 +08:00
} // @codeCoverageIgnore
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);
ob_start();
imagepng($png);
imagedestroy($png);
$image = ob_get_contents();
ob_end_clean();
return Response::png($image);
}
}
} 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
2017-11-17 07:40:29 +08:00
public function avatar($base64_email, UserRepository $users, $size = 128)
2016-08-13 22:14:01 +08:00
{
2016-10-23 11:41:52 +08:00
$user = $users->get(base64_decode($base64_email), 'email');
2016-08-29 14:25:24 +08:00
2016-10-23 11:41:52 +08:00
if ($user) {
2018-07-30 15:13:14 +08:00
return $this->avatarByTid($user->getAvatarId());
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-10-23 11:41:52 +08:00
public function avatarWithSize($size, $base64_email, UserRepository $users)
2016-07-21 22:01:57 +08:00
{
2017-11-17 07:40:29 +08:00
return $this->avatar($base64_email, $users, $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
ob_start();
imagepng($png);
imagedestroy($png);
$image = ob_get_contents();
ob_end_clean();
return Response::png($image);
}
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) {
if (!option('allow_downloading_texture')) {
abort(404);
}
return ($t = Texture::find($tid))
? $this->texture($t->hash)
: abort(404, trans('skinlib.non-existent'));
}
protected function getPlayerInstance($player_name)
2016-10-16 18:16:15 +08:00
{
$player = Player::where('player_name', $player_name)->first();
if ($player->isBanned()) {
abort(403, trans('general.player-banned'));
}
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/TahdSw2l0HUisr1UKIWJQBAMehDuqiDdzndsP2EZECAG1ZXaWMwOCODdXqysLf++uXUGv9MhUHIByDOijjdiSAoH3ErANQD73C7TXXuGOsFj1d4YH4OTJAEy8y9Hd0mCaeZ5z8dfp88zw1bVyiYhCLOg1ZeAqC0ybaDttHRGME1DhDeVWV26u17lRAPr2+mj7dvULfHw2q65fhQRrLXKDfIxkau3ZMCTGIRR3URR5toU38HbaPiMwUcKfBAkoun09Pzr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