diff --git a/plugins/yggdrasil-api/package.json b/plugins/yggdrasil-api/package.json index e66ee854..9dbeb6c4 100644 --- a/plugins/yggdrasil-api/package.json +++ b/plugins/yggdrasil-api/package.json @@ -1,6 +1,6 @@ { "name": "yggdrasil-api", - "version": "5.1.1", + "version": "5.1.2", "title": "Yggdrasil API", "description": "Yggdrasil API + authlib-injector = ✨", "author": "printempw", diff --git a/plugins/yggdrasil-api/src/Controllers/ProfileController.php b/plugins/yggdrasil-api/src/Controllers/ProfileController.php index a02153e9..6bd669ad 100644 --- a/plugins/yggdrasil-api/src/Controllers/ProfileController.php +++ b/plugins/yggdrasil-api/src/Controllers/ProfileController.php @@ -156,35 +156,51 @@ public function uploadTexture(Request $request, Dispatcher $dispatcher, Filter $ /** @var User */ $user = $profile->player->user; - $size = ceil($file->getSize() / 1024); - $cost = (int) option('private_score_per_storage') * $size + (int) option('score_per_closet_item'); - if ($cost > $user->score) { - throw new ForbiddenOperationException(trans('skinlib.upload.lack-score')); - } - $dispatcher->dispatch('texture.uploading', [$file, $name, $hash]); - - $texture = new Texture(); - $texture->name = $name; - $texture->type = $type === 'cape' ? 'cape' : ($isAlex ? 'alex' : 'steve'); - $texture->hash = $hash; - $texture->size = $size; - $texture->public = false; - $texture->uploader = $user->uid; - $texture->likes = 1; - $texture->save(); - - /** @var FilesystemAdapter */ - $disk = Storage::disk('textures'); - if ($disk->missing($hash)) { - $file->storePubliclyAs('', $hash, ['disk' => 'textures']); - } + $duplicate = Texture::where('hash', $hash)->where('uploader', $user->uid)->first(); + if ($duplicate) { + $texture = $duplicate; + + if ($user->closet->where('hash', $hash)->isEmpty()) { + $cost = (int) option('score_per_closet_item'); + if ($cost > $user->score) { + throw new ForbiddenOperationException(trans('skinlib.upload.lack-score')); + } + + $user->closet()->attach($texture->tid, ['item_name' => $name]); + $user->save(); + } + } else { + $size = ceil($file->getSize() / 1024); + $cost = (int) option('private_score_per_storage') * $size + (int) option('score_per_closet_item'); + if ($cost > $user->score) { + throw new ForbiddenOperationException(trans('skinlib.upload.lack-score')); + } - $user->score -= $cost; - $user->closet()->attach($texture->tid, ['item_name' => $name]); - $user->save(); + $dispatcher->dispatch('texture.uploading', [$file, $name, $hash]); + + $texture = new Texture(); + $texture->name = $name; + $texture->type = $type === 'cape' ? 'cape' : ($isAlex ? 'alex' : 'steve'); + $texture->hash = $hash; + $texture->size = $size; + $texture->public = false; + $texture->uploader = $user->uid; + $texture->likes = 1; + $texture->save(); + + /** @var FilesystemAdapter */ + $disk = Storage::disk('textures'); + if ($disk->missing($hash)) { + $file->storePubliclyAs('', $hash, ['disk' => 'textures']); + } - $dispatcher->dispatch('texture.uploaded', [$texture, $file]); + $user->score -= $cost; + $user->closet()->attach($texture->tid, ['item_name' => $name]); + $user->save(); + + $dispatcher->dispatch('texture.uploaded', [$texture, $file]); + } $player = $profile->player; $can = $filter->apply('can_set_texture', true, [$player, $type, $texture->tid]);