Skip to content

Commit

Permalink
[yggdrasil-api] fix uploading duplicated texture
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Feb 17, 2021
1 parent 805de88 commit 0728387
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion plugins/yggdrasil-api/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
68 changes: 42 additions & 26 deletions plugins/yggdrasil-api/src/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 0728387

Please sign in to comment.