forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use separate helper to handle user avatar
- Loading branch information
Showing
3 changed files
with
58 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,65 +3,77 @@ | |
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
namespace App\Models\Traits; | ||
declare(strict_types=1); | ||
|
||
namespace App\Libraries\User; | ||
|
||
use App\Libraries\ImageProcessor; | ||
use App\Libraries\StorageUrl; | ||
use ErrorException; | ||
use App\Models\User; | ||
|
||
trait UserAvatar | ||
class AvatarHelper | ||
{ | ||
private static function avatarDisk(): string | ||
public static function set(User $user, ?\SplFileInfo $src): bool | ||
{ | ||
return \Config::get('osu.avatar.storage'); | ||
} | ||
$id = $user->getKey(); | ||
$storage = \Storage::disk(static::disk()); | ||
|
||
public function setAvatar($file) | ||
{ | ||
$storage = \Storage::disk(static::avatarDisk()); | ||
if ($file === null) { | ||
$storage->delete($this->user_id); | ||
if ($src === null) { | ||
$storage->delete($id); | ||
} else { | ||
$filePath = $file->getRealPath(); | ||
$processor = new ImageProcessor($filePath, [256, 256], 100000); | ||
$srcPath = $src->getRealPath(); | ||
$processor = new ImageProcessor($srcPath, [256, 256], 100000); | ||
$processor->process(); | ||
|
||
$storage->put($this->user_id, file_get_contents($filePath), 'public'); | ||
|
||
$entry = $this->user_id.'_'.time().'.'.$processor->ext(); | ||
$storage->putFileAs('/', $src, $id, 'public'); | ||
$entry = $id.'_'.time().'.'.$processor->ext(); | ||
} | ||
|
||
if (present(\Config::get('osu.avatar.cache_purge_prefix'))) { | ||
try { | ||
$ctx = [ | ||
'http' => [ | ||
'method' => \Config::get('osu.avatar.cache_purge_method') ?? 'GET', | ||
'header' => present(\Config::get('osu.avatar.cache_purge_authorization_key')) | ||
? 'Authorization: '.\Config::get('osu.avatar.cache_purge_authorization_key') | ||
: null, | ||
], | ||
]; | ||
$prefix = \Config::get('osu.avatar.cache_purge_prefix'); | ||
$suffix = $ctx['http']['method'] === 'GET' ? '?'.time() : ''; // Bypass CloudFlare cache if using GET | ||
$url = $prefix.$this->user_id.$suffix; | ||
file_get_contents($url, false, stream_context_create($ctx)); | ||
} catch (ErrorException $e) { | ||
// ignores 404 errors, throws everything else | ||
if (!ends_with($e->getMessage(), "404 Not Found\r\n")) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
static::purgeCache($id); | ||
|
||
return $this->update(['user_avatar' => $entry ?? '']); | ||
return $user->update(['user_avatar' => $entry ?? '']); | ||
} | ||
|
||
protected function getUserAvatar() | ||
public static function url(User $user): string | ||
{ | ||
$value = $this->getRawAttribute('user_avatar'); | ||
$value = $user->getRawAttribute('user_avatar'); | ||
|
||
return present($value) | ||
? StorageUrl::make(static::avatarDisk(), strtr($value, '_', '?')) | ||
? StorageUrl::make(static::disk(), strtr($value, '_', '?')) | ||
: \Config::get('osu.avatar.default'); | ||
} | ||
|
||
private static function disk(): string | ||
{ | ||
return \Config::get('osu.avatar.storage'); | ||
} | ||
|
||
private static function purgeCache(int $id): void | ||
{ | ||
$prefix = presence(\Config::get('osu.avatar.cache_purge_prefix')); | ||
|
||
if ($prefix === null) { | ||
return; | ||
} | ||
|
||
$method = \Config::get('osu.avatar.cache_purge_method') ?? 'GET'; | ||
$auth = \Config::get('osu.avatar.cache_purge_authorization_key'); | ||
$ctx = [ | ||
'http' => [ | ||
'method' => $method, | ||
'header' => present($auth) ? "Authorization: {$auth}" : null, | ||
], | ||
]; | ||
$suffix = $method === 'GET' ? '?'.time() : ''; // Bypass CloudFlare cache if using GET | ||
$url = "{$prefix}{$id}{$suffix}"; | ||
|
||
try { | ||
file_get_contents($url, false, stream_context_create($ctx)); | ||
} catch (\ErrorException $e) { | ||
// ignores 404 errors, throws everything else | ||
if (!ends_with($e->getMessage(), "404 Not Found\r\n")) { | ||
throw $e; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters