Files
breadcrumb-the-shire/pages/api/v1/me/avatar().php

57 lines
1.7 KiB
PHP
Raw Normal View History

<?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);
$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;
$path = $userAvatarService->findAvatarPath($uuid, $size);
if (!$path || !is_file($path)) {
ApiResponse::notFound();
}
$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-03-04 15:56:58 +01:00
$result = $userAvatarService->saveUpload($uuid, $request->file('avatar', []));
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);
$userAvatarService->delete($uuid);
ApiResponse::noContent();
}
ApiResponse::methodNotAllowed();