Files
breadcrumb-the-shire/pages/api/v1/tenants/avatar($id).php

47 lines
1.4 KiB
PHP

<?php
use MintyPHP\Http\ApiAuth;
use MintyPHP\Http\ApiBootstrap;
use MintyPHP\Http\ApiResponse;
use MintyPHP\Service\Tenant\TenantServicesFactory;
define('MINTY_ALLOW_OUTPUT', true);
ApiBootstrap::init();
ApiResponse::requireMethod('GET');
ApiResponse::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_API_TENANTS_VIEW);
$request = requestInput();
$uuid = trim((string) ($id ?? ''));
if ($uuid === '') {
ApiResponse::notFound();
}
$tenantAvatarService = (app(TenantServicesFactory::class))->createTenantAvatarService();
if (!$tenantAvatarService->isValidUuid($uuid)) {
ApiResponse::notFound();
}
$tenant = app(\MintyPHP\Service\Directory\DirectoryServicesFactory::class)->createTenantService()->findByUuid($uuid);
$tenantId = (int) ($tenant['id'] ?? 0);
if ($tenantId <= 0) {
ApiResponse::notFound();
}
ApiAuth::requireResourceAccess('tenants', $tenantId);
$size = $request->hasQuery('size') ? $request->queryInt('size') : null;
$path = $tenantAvatarService->findAvatarPath($uuid, $size);
if (!$path || !is_file($path)) {
ApiResponse::notFound();
}
$mime = $tenantAvatarService->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);