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-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Option;
|
|
|
|
use Session;
|
2019-03-02 22:58:37 +08:00
|
|
|
use Storage;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
2017-04-21 18:11:17 +08:00
|
|
|
use App\Models\Player;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\Texture;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-12-25 23:21:13 +08:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class SkinlibController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-08-17 22:54:26 +08:00
|
|
|
/**
|
|
|
|
* Map error code of file uploading to human-readable text.
|
|
|
|
*
|
|
|
|
* @see http://php.net/manual/en/features.file-upload.errors.php
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $phpFileUploadErrors = [
|
|
|
|
0 => 'There is no error, the file uploaded with success',
|
|
|
|
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
|
|
|
|
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
|
|
|
|
3 => 'The uploaded file was only partially uploaded',
|
|
|
|
4 => 'No file was uploaded',
|
|
|
|
6 => 'Missing a temporary folder',
|
|
|
|
7 => 'Failed to write file to disk.',
|
|
|
|
8 => 'A PHP extension stopped the file upload.',
|
|
|
|
];
|
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
public function index()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
return view('skinlib.index', ['user' => Auth::user()]);
|
2017-06-30 10:33:41 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
/**
|
|
|
|
* Get skin library data filtered.
|
|
|
|
* Available Query String: filter, uploader, page, sort, keyword, items_per_page.
|
|
|
|
*
|
|
|
|
* @param Request $request [description]
|
2017-11-24 18:54:30 +08:00
|
|
|
* @return JsonResponse
|
2017-06-30 10:33:41 +08:00
|
|
|
*/
|
|
|
|
public function getSkinlibFiltered(Request $request)
|
|
|
|
{
|
2019-03-14 23:55:49 +08:00
|
|
|
$user = Auth::user();
|
2016-07-26 13:36:24 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
// Available filters: skin, steve, alex, cape
|
|
|
|
$filter = $request->input('filter', 'skin');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
// Filter result by uploader's uid
|
|
|
|
$uploader = intval($request->input('uploader', 0));
|
2017-05-05 12:43:53 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
// Current page
|
|
|
|
$page = $request->input('page', 1);
|
|
|
|
$currentPage = ($page <= 0) ? 1 : $page;
|
2016-08-13 22:31:24 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
// How many items to show in one page
|
|
|
|
$itemsPerPage = $request->input('items_per_page', 20);
|
2017-11-24 18:54:30 +08:00
|
|
|
$itemsPerPage = $itemsPerPage <= 0 ? 20 : $itemsPerPage;
|
2016-07-24 11:12:52 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
// Keyword to search
|
|
|
|
$keyword = $request->input('keyword', '');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($filter == 'skin') {
|
2017-06-30 10:33:41 +08:00
|
|
|
$query = Texture::where(function ($innerQuery) {
|
|
|
|
// Nested condition, DO NOT MODIFY
|
2019-03-14 23:55:49 +08:00
|
|
|
$innerQuery->where('type', 'steve')->orWhere('type', 'alex');
|
2017-06-30 10:33:41 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$query = Texture::where('type', $filter);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($keyword !== '') {
|
2017-06-30 10:33:41 +08:00
|
|
|
$query = $query->like('name', $keyword);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
if ($uploader !== 0) {
|
|
|
|
$query = $query->where('uploader', $uploader);
|
2017-04-25 08:51:30 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
if (! $user) {
|
2017-06-30 10:33:41 +08:00
|
|
|
// Show public textures only to anonymous visitors
|
2018-07-16 11:10:01 +08:00
|
|
|
$query = $query->where('public', true);
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2017-06-30 10:33:41 +08:00
|
|
|
// Show private textures when show uploaded textures of current user
|
2019-03-14 23:55:49 +08:00
|
|
|
if ($uploader != $user->uid && ! $user->isAdmin()) {
|
|
|
|
$query = $query->where(function ($innerQuery) use ($user) {
|
|
|
|
$innerQuery->where('public', true)->orWhere('uploader', '=', $user->uid);
|
2017-06-30 10:33:41 +08:00
|
|
|
});
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-24 18:54:30 +08:00
|
|
|
$totalPages = ceil($query->count() / $itemsPerPage);
|
2017-06-30 10:33:41 +08:00
|
|
|
|
2019-03-19 15:19:33 +08:00
|
|
|
// Available sorting methods: time
|
|
|
|
$sort = $request->input('sort', 'time');
|
|
|
|
$sortBy = ($sort == 'time') ? 'upload_at' : $sort;
|
|
|
|
// `likes` property is not actually existed in database.
|
|
|
|
if ($sortBy != 'likes') {
|
|
|
|
$query = $query->orderBy($sortBy, 'desc');
|
|
|
|
}
|
|
|
|
|
|
|
|
$textures = $query->skip(($currentPage - 1) * $itemsPerPage)
|
2017-06-30 10:33:41 +08:00
|
|
|
->take($itemsPerPage)
|
|
|
|
->get();
|
|
|
|
|
2019-03-19 15:19:33 +08:00
|
|
|
if ($sortBy == 'likes') {
|
|
|
|
$textures = $textures->sortByDesc('likes')->values();
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
if ($user) {
|
|
|
|
$closet = $user->closet()->get();
|
2017-06-30 10:33:41 +08:00
|
|
|
foreach ($textures as $item) {
|
2019-03-14 23:55:49 +08:00
|
|
|
$item->liked = $closet->contains('tid', $item->tid);
|
2017-04-24 17:13:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 10:33:41 +08:00
|
|
|
return response()->json([
|
|
|
|
'items' => $textures,
|
2019-03-14 23:55:49 +08:00
|
|
|
'current_uid' => $user ? $user->uid : 0,
|
2019-03-02 22:58:37 +08:00
|
|
|
'total_pages' => $totalPages,
|
2017-06-30 10:33:41 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2017-01-22 15:45:10 +08:00
|
|
|
public function show($tid)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-01-22 15:45:10 +08:00
|
|
|
$texture = Texture::find($tid);
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2016-08-20 21:08:46 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $texture || $texture && ! Storage::disk('textures')->has($texture->hash)) {
|
2018-02-10 12:55:10 +08:00
|
|
|
if (option('auto_del_invalid_texture')) {
|
|
|
|
if ($texture) {
|
2016-08-28 20:33:35 +08:00
|
|
|
$texture->delete();
|
2018-02-10 12:55:10 +08:00
|
|
|
}
|
2016-08-28 20:33:35 +08:00
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
abort(404, trans('skinlib.show.deleted'));
|
2016-08-17 18:08:51 +08:00
|
|
|
}
|
2016-09-24 22:49:20 +08:00
|
|
|
abort(404, trans('skinlib.show.deleted').trans('skinlib.show.contact-admin'));
|
2016-08-16 22:58:21 +08:00
|
|
|
}
|
2016-08-20 21:08:46 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $texture->public) {
|
|
|
|
if (! Auth::check() || ($user->uid != $texture->uploader && ! $user->isAdmin())) {
|
2018-02-10 12:55:10 +08:00
|
|
|
abort(403, trans('skinlib.show.private'));
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
return view('skinlib.show')
|
|
|
|
->with('texture', $texture)
|
|
|
|
->with('with_out_filter', true)
|
2019-03-23 19:52:14 +08:00
|
|
|
->with('user', $user)
|
|
|
|
->with('extra', [
|
|
|
|
'download' => option('allow_downloading_texture'),
|
|
|
|
'currentUid' => $user ? $user->uid : 0,
|
|
|
|
'admin' => $user && $user->isAdmin(),
|
|
|
|
'inCloset' => $user && $user->closet()->where('tid', $texture->tid)->count() > 0,
|
|
|
|
'nickname' => ($up = User::find($texture->uploader)) ? $up->nickname : null
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function info($tid)
|
|
|
|
{
|
2016-12-10 19:49:45 +08:00
|
|
|
if ($t = Texture::find($tid)) {
|
2019-03-15 13:14:59 +08:00
|
|
|
return json($t->toArray());
|
2016-12-10 19:49:45 +08:00
|
|
|
} else {
|
|
|
|
return json([]);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function upload()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
return view('skinlib.upload')
|
|
|
|
->with('user', Auth::user())
|
2019-03-23 19:52:14 +08:00
|
|
|
->with('extra', [
|
|
|
|
'rule' => ($regexp = option('texture_name_regexp'))
|
|
|
|
? trans('skinlib.upload.name-rule-regexp', compact('regexp'))
|
|
|
|
: trans('skinlib.upload.name-rule'),
|
|
|
|
'privacyNotice' => trans(
|
|
|
|
'skinlib.upload.private-score-notice',
|
|
|
|
['score' => option('private_score_per_storage')]
|
|
|
|
),
|
|
|
|
'scorePublic' => intval(option('score_per_storage')),
|
|
|
|
'scorePrivate' => intval(option('private_score_per_storage')),
|
|
|
|
'award' => intval(option('score_award_per_texture')),
|
|
|
|
])
|
2018-07-20 14:42:43 +08:00
|
|
|
->with('with_out_filter', true);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function handleUpload(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2018-07-13 19:02:16 +08:00
|
|
|
|
2016-12-25 23:21:13 +08:00
|
|
|
if (($response = $this->checkUpload($request)) instanceof JsonResponse) {
|
|
|
|
return $response;
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
$t = new Texture();
|
|
|
|
$t->name = $request->input('name');
|
|
|
|
$t->type = $request->input('type');
|
|
|
|
$t->hash = bs_hash_file($request->file('file'));
|
|
|
|
$t->size = ceil($request->file('file')->getSize() / 1024);
|
|
|
|
$t->public = $request->input('public') == 'true';
|
|
|
|
$t->uploader = $user->uid;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-07-16 11:10:01 +08:00
|
|
|
$cost = $t->size * ($t->public ? Option::get('score_per_storage') : Option::get('private_score_per_storage'));
|
2017-04-22 23:35:25 +08:00
|
|
|
$cost += option('score_per_closet_item');
|
2019-03-20 23:28:04 +08:00
|
|
|
$cost -= option('score_award_per_texture', 0);
|
2016-08-13 23:25:10 +08:00
|
|
|
|
2019-03-23 00:20:28 +08:00
|
|
|
if ($user->score < $cost) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.lack-score'), 7);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
|
|
|
$results = Texture::where('hash', $t->hash)->get();
|
2016-08-13 23:25:10 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! $results->isEmpty()) {
|
2016-07-21 22:01:57 +08:00
|
|
|
foreach ($results as $result) {
|
2017-11-24 18:54:30 +08:00
|
|
|
// if the texture already uploaded was set to private,
|
2016-09-30 16:31:45 +08:00
|
|
|
// then allow to re-upload it.
|
2018-07-16 11:10:01 +08:00
|
|
|
if ($result->type == $t->type && $result->public) {
|
2016-09-30 16:31:45 +08:00
|
|
|
return json(trans('skinlib.upload.repeated'), 0, [
|
2019-03-02 22:58:37 +08:00
|
|
|
'tid' => $result->tid,
|
2016-07-22 10:45:36 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 16:05:00 +08:00
|
|
|
if (! Storage::disk('textures')->exists($t->hash)) {
|
|
|
|
Storage::disk('textures')->put($t->hash, file_get_contents($request->file('file')));
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
$t->save();
|
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$user->setScore($cost, 'minus');
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$user->closet()->attach($t->tid, ['item_name' => $t->name]);
|
2019-03-15 00:03:54 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
return json(trans('skinlib.upload.success', ['name' => $request->input('name')]), 0, [
|
|
|
|
'tid' => $t->tid,
|
|
|
|
]);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
public function delete(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$result = Texture::find($request->tid);
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! $result) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($result->uploader != $user->uid && ! $user->isAdmin()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-07-24 15:56:23 +08:00
|
|
|
// check if file occupied
|
2017-11-24 18:54:30 +08:00
|
|
|
if (Texture::where('hash', $result->hash)->count() == 1) {
|
|
|
|
Storage::disk('textures')->delete($result->hash);
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-20 23:28:04 +08:00
|
|
|
$result->likers()->get()->each(function ($user) use ($result) {
|
|
|
|
$user->closet()->detach($result->tid);
|
|
|
|
if (option('return_score')) {
|
|
|
|
$user->setScore(option('score_per_closet_item'), 'plus');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
if ($u = User::find($result->uploader)) {
|
2019-03-20 23:28:04 +08:00
|
|
|
$ret = 0;
|
|
|
|
if (option('return_score')) {
|
|
|
|
$ret += $result->size * (
|
|
|
|
$result->public
|
|
|
|
? option('score_per_storage')
|
|
|
|
: option('private_score_per_storage')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result->public && option('take_back_scores_after_deletion', true)) {
|
|
|
|
$ret -= option('score_award_per_texture', 0);
|
2017-04-22 23:35:25 +08:00
|
|
|
}
|
2019-03-20 23:28:04 +08:00
|
|
|
|
|
|
|
$u->setScore($ret, 'plus');
|
2016-12-21 22:53:53 +08:00
|
|
|
}
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2017-06-28 17:13:07 +08:00
|
|
|
if ($result->delete()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.delete.success'), 0);
|
2017-06-28 17:13:07 +08:00
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
public function privacy(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-01-22 15:45:10 +08:00
|
|
|
$t = Texture::find($request->input('tid'));
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $t) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($t->uploader != $user->uid && ! $user->isAdmin()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
$uploader = User::find($t->uploader);
|
2018-07-16 11:10:01 +08:00
|
|
|
$score_diff = $t->size * (option('private_score_per_storage') - option('score_per_storage')) * ($t->public ? -1 : 1);
|
2019-03-20 23:28:04 +08:00
|
|
|
if ($t->public && option('take_back_scores_after_deletion', true)) {
|
|
|
|
$score_diff -= option('score_award_per_texture', 0);
|
|
|
|
}
|
2019-03-23 11:06:36 +08:00
|
|
|
if ($uploader->score + $score_diff < 0) {
|
2017-09-18 19:28:38 +08:00
|
|
|
return json(trans('skinlib.upload.lack-score'), 1);
|
|
|
|
}
|
|
|
|
|
2019-03-02 21:13:17 +08:00
|
|
|
$type = $t->type == 'cape' ? 'cape' : 'skin';
|
2017-11-24 18:54:30 +08:00
|
|
|
Player::where("tid_$type", $t->tid)
|
|
|
|
->where('uid', '<>', session('uid'))
|
2019-03-14 23:55:49 +08:00
|
|
|
->update(["tid_$type" => 0]);
|
|
|
|
|
|
|
|
$t->likers()->get()->each(function ($user) use ($t) {
|
|
|
|
$user->closet()->detach($t->tid);
|
|
|
|
if (option('return_score')) {
|
|
|
|
$user->setScore(option('score_per_closet_item'), 'plus');
|
|
|
|
}
|
|
|
|
});
|
2017-04-21 18:11:17 +08:00
|
|
|
|
2019-03-23 11:06:36 +08:00
|
|
|
@$uploader->setScore($score_diff, 'plus');
|
2017-04-21 18:44:11 +08:00
|
|
|
|
2019-03-03 00:38:44 +08:00
|
|
|
$t->public = ! $t->public;
|
2019-03-02 23:58:35 +08:00
|
|
|
$t->save();
|
2019-03-03 00:38:44 +08:00
|
|
|
|
2019-03-02 23:58:35 +08:00
|
|
|
return json([
|
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('skinlib.privacy.success', ['privacy' => (! $t->public ? trans('general.private') : trans('general.public'))]),
|
|
|
|
'public' => $t->public,
|
|
|
|
]);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
public function rename(Request $request)
|
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_name' => 'required|no_special_chars',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2016-09-04 15:35:12 +08:00
|
|
|
$t = Texture::find($request->input('tid'));
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $t) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($t->uploader != $user->uid && ! $user->isAdmin()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$t->name = $request->input('new_name');
|
2016-07-24 15:56:23 +08:00
|
|
|
|
|
|
|
if ($t->save()) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.rename.success', ['name' => $request->input('new_name')]), 0);
|
2016-07-24 15:56:23 +08:00
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// @codeCoverageIgnore
|
2016-07-24 15:56:23 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
public function model(Request $request)
|
|
|
|
{
|
2018-08-16 17:42:54 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
$data = $this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
2019-03-02 22:58:37 +08:00
|
|
|
'model' => 'required|in:steve,alex,cape',
|
2018-08-16 17:42:54 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$t = Texture::find($request->input('tid'));
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $t) {
|
2018-08-16 17:42:54 +08:00
|
|
|
return json(trans('skinlib.non-existent'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2018-08-16 17:42:54 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($t->uploader != $user->uid && ! $user->isAdmin()) {
|
2018-08-16 17:42:54 +08:00
|
|
|
return json(trans('skinlib.no-permission'), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2018-08-16 17:42:54 +08:00
|
|
|
|
|
|
|
$duplicate = Texture::where('hash', $t->hash)
|
|
|
|
->where('type', $request->input('model'))
|
|
|
|
->where('tid', '<>', $t->tid)
|
|
|
|
->first();
|
|
|
|
if ($duplicate && $duplicate->public) {
|
|
|
|
return json(trans('skinlib.model.duplicate', ['name' => $duplicate->name]), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$t->type = $request->input('model');
|
|
|
|
$t->save();
|
|
|
|
|
|
|
|
return json(trans('skinlib.model.success', ['model' => $data['model']]), 0);
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
2019-03-02 22:58:37 +08:00
|
|
|
* Check Uploaded Files.
|
2016-09-04 15:35:12 +08:00
|
|
|
*
|
|
|
|
* @param Request $request
|
2017-11-24 18:54:30 +08:00
|
|
|
* @return JsonResponse
|
2016-09-04 15:35:12 +08:00
|
|
|
*/
|
2018-02-16 17:31:04 +08:00
|
|
|
protected function checkUpload(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-12-25 23:21:13 +08:00
|
|
|
if ($file = $request->files->get('file')) {
|
|
|
|
if ($file->getError() !== UPLOAD_ERR_OK) {
|
2018-08-17 22:54:26 +08:00
|
|
|
return json(static::$phpFileUploadErrors[$file->getError()], $file->getError());
|
2016-12-25 23:21:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2018-07-20 15:41:15 +08:00
|
|
|
'name' => [
|
|
|
|
'required',
|
2019-03-02 22:58:37 +08:00
|
|
|
option('texture_name_regexp') ? 'regex:'.option('texture_name_regexp') : 'no_special_chars',
|
2018-07-20 15:41:15 +08:00
|
|
|
],
|
2016-10-16 20:18:40 +08:00
|
|
|
'file' => 'required|max:'.option('max_upload_file_size'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'public' => 'required',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
|
|
|
|
2018-07-20 17:32:27 +08:00
|
|
|
$mime = $request->file('file')->getMimeType();
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($mime != 'image/png' && $mime != 'image/x-png') {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.type-error'), 1);
|
2016-09-22 22:00:12 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
$type = $request->input('type');
|
|
|
|
$size = getimagesize($request->file('file'));
|
2016-09-04 15:35:12 +08:00
|
|
|
$ratio = $size[0] / $size[1];
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
if ($type == 'steve' || $type == 'alex') {
|
|
|
|
if ($ratio != 2 && $ratio != 1) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.invalid-size', ['type' => trans('general.skin'), 'width' => $size[0], 'height' => $size[1]]), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
if ($size[0] % 64 != 0 || $size[1] % 32 != 0) {
|
2017-01-25 15:34:43 +08:00
|
|
|
return json(trans('skinlib.upload.invalid-hd-skin', ['type' => trans('general.skin'), 'width' => $size[0], 'height' => $size[1]]), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
|
|
|
} elseif ($type == 'cape') {
|
|
|
|
if ($ratio != 2) {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('skinlib.upload.invalid-size', ['type' => trans('general.cape'), 'width' => $size[0], 'height' => $size[1]]), 1);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
// @codeCoverageIgnore
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|