2026-02-24 08:49:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Http\ApiAuth;
|
|
|
|
|
use MintyPHP\Http\ApiBootstrap;
|
|
|
|
|
use MintyPHP\Http\ApiResponse;
|
|
|
|
|
use MintyPHP\Service\User\UserServicesFactory;
|
|
|
|
|
|
|
|
|
|
define('MINTY_ALLOW_OUTPUT', true);
|
|
|
|
|
|
|
|
|
|
ApiBootstrap::init();
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$request = requestInput();
|
|
|
|
|
$method = $request->method();
|
|
|
|
|
$userServicesFactory = app(UserServicesFactory::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$userAvatarService = $userServicesFactory->createUserAvatarService();
|
|
|
|
|
$user = ApiAuth::user();
|
|
|
|
|
$uuid = (string) ($user['uuid'] ?? '');
|
|
|
|
|
|
|
|
|
|
if ($method === 'GET') {
|
2026-03-04 15:56:58 +01:00
|
|
|
$size = $request->hasQuery('size') ? $request->queryInt('size') : null;
|
2026-02-24 08:49:40 +01:00
|
|
|
|
|
|
|
|
$path = $userAvatarService->findAvatarPath($uuid, $size);
|
|
|
|
|
if (!$path || !is_file($path)) {
|
2026-03-24 19:29:47 +01:00
|
|
|
ApiResponse::notFound();
|
2026-02-24 08:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mime = $userAvatarService->detectMime($path);
|
|
|
|
|
header('Content-Type: ' . $mime);
|
|
|
|
|
header('X-Content-Type-Options: nosniff');
|
|
|
|
|
header('Content-Security-Policy: sandbox');
|
|
|
|
|
header('Cache-Control: private, max-age=300');
|
|
|
|
|
header('Content-Length: ' . filesize($path));
|
|
|
|
|
readfile($path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($method === 'POST') {
|
2026-03-04 15:56:58 +01:00
|
|
|
ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_ME_SELF_UPDATE);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$result = $userAvatarService->saveUpload($uuid, $request->file('avatar', []));
|
2026-02-24 08:49:40 +01:00
|
|
|
if (!($result['ok'] ?? false)) {
|
|
|
|
|
$error = $result['error'] ?? 'upload_failed';
|
|
|
|
|
ApiResponse::error($error, 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ApiResponse::created(['data' => ['message' => 'avatar_updated']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($method === 'DELETE') {
|
2026-03-04 15:56:58 +01:00
|
|
|
ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_ME_SELF_UPDATE);
|
2026-02-24 08:49:40 +01:00
|
|
|
|
|
|
|
|
$userAvatarService->delete($uuid);
|
|
|
|
|
ApiResponse::noContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ApiResponse::methodNotAllowed();
|