Files
breadcrumb-the-shire/lib/Service/Tenant/TenantFaviconService.php

147 lines
4.3 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Service\Tenant;
2026-02-04 23:31:53 +01:00
use MintyPHP\Service\Image\ImageUploadTrait;
2026-02-04 23:31:53 +01:00
class TenantFaviconService
{
use ImageUploadTrait;
2026-02-04 23:31:53 +01:00
private const MAX_SIZE = 5242880; // 5 MB
private const SIZES = [
16 => 'favicon-16x16.png',
32 => 'favicon-32x32.png',
96 => 'favicon-96x96.png',
180 => 'apple-touch-icon.png',
192 => 'web-app-manifest-192x192.png',
512 => 'web-app-manifest-512x512.png',
];
2026-02-23 12:58:19 +01:00
public function isValidUuid(string $uuid): bool
2026-02-04 23:31:53 +01:00
{
return self::imageIsValidUuid($uuid);
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function storageBase(): string
2026-02-04 23:31:53 +01:00
{
return self::imageStorageBase();
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function storageDir(string $uuid): string
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
return $this->storageBase() . '/tenants/' . $uuid . '/favicon';
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function publicDir(string $uuid): string
2026-02-04 23:31:53 +01:00
{
2026-02-11 19:28:12 +01:00
return rtrim(dirname(__DIR__, 3) . '/web/favicon/tenants/' . $uuid . '/favicon', '/');
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
public function hasFavicon(string $uuid): bool
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
if (!$this->isValidUuid($uuid)) {
2026-02-04 23:31:53 +01:00
return false;
}
2026-02-23 12:58:19 +01:00
$path = $this->publicDir($uuid) . '/favicon-32x32.png';
2026-02-04 23:31:53 +01:00
return is_file($path);
}
2026-02-23 12:58:19 +01:00
public function delete(string $uuid): void
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
if (!$this->isValidUuid($uuid)) {
2026-02-04 23:31:53 +01:00
return;
}
2026-02-23 12:58:19 +01:00
foreach ($this->storageDirs($uuid) as $dir) {
2026-02-04 23:31:53 +01:00
if (!is_dir($dir)) {
continue;
}
$matches = glob($dir . '/*') ?: [];
foreach ($matches as $file) {
if (is_file($file)) {
@unlink($file);
}
}
}
2026-02-23 12:58:19 +01:00
$public = $this->publicDir($uuid);
2026-02-04 23:31:53 +01:00
foreach (self::SIZES as $file) {
$target = $public . '/' . $file;
if (is_file($target)) {
@unlink($target);
}
}
}
2026-02-23 12:58:19 +01:00
public function saveUpload(string $uuid, array $file): array
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
if (!$this->isValidUuid($uuid)) {
2026-02-04 23:31:53 +01:00
return ['ok' => false, 'error' => t('Tenant not found')];
}
if (empty($file) || !isset($file['tmp_name'])) {
return ['ok' => false, 'error' => t('No file uploaded')];
}
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
return ['ok' => false, 'error' => t('Upload failed')];
}
if (($file['size'] ?? 0) > self::MAX_SIZE) {
return ['ok' => false, 'error' => t('File is too large')];
}
$tmpPath = $file['tmp_name'];
2026-02-23 12:58:19 +01:00
$mime = $this->detectMime($tmpPath);
2026-02-04 23:31:53 +01:00
if ($mime !== 'image/png') {
return ['ok' => false, 'error' => t('Invalid image file')];
}
if (!self::imageCanResize()) {
2026-02-04 23:31:53 +01:00
return ['ok' => false, 'error' => t('Upload failed')];
}
2026-02-23 12:58:19 +01:00
$dir = $this->storageDir($uuid);
2026-02-04 23:31:53 +01:00
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
return ['ok' => false, 'error' => t('Upload failed')];
}
2026-02-23 12:58:19 +01:00
$this->delete($uuid);
2026-02-04 23:31:53 +01:00
$originalPath = $dir . '/original.png';
if (!move_uploaded_file($tmpPath, $originalPath)) {
return ['ok' => false, 'error' => t('Upload failed')];
}
@chmod($originalPath, 0644);
2026-02-23 12:58:19 +01:00
$publicDir = $this->publicDir($uuid);
2026-02-04 23:31:53 +01:00
if (!is_dir($publicDir) && !mkdir($publicDir, 0755, true) && !is_dir($publicDir)) {
return ['ok' => false, 'error' => t('Upload failed')];
}
foreach (self::SIZES as $size => $fileName) {
$target = $publicDir . '/' . $fileName;
if (!self::imageResizeSquare($originalPath, $target, $size)) {
2026-02-04 23:31:53 +01:00
return ['ok' => false, 'error' => t('Upload failed')];
}
@chmod($target, 0644);
}
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
}
2026-02-23 12:58:19 +01:00
public function detectMime(string $path): string
2026-02-04 23:31:53 +01:00
{
return self::imageDetectMimeSimple($path);
2026-02-04 23:31:53 +01:00
}
2026-02-23 12:58:19 +01:00
private function storageDirs(string $uuid): array
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
$dirs = [$this->storageDir($uuid)];
$legacy = $this->legacyStorageDir($uuid);
2026-02-04 23:31:53 +01:00
if ($legacy !== $dirs[0]) {
$dirs[] = $legacy;
}
return $dirs;
}
2026-02-23 12:58:19 +01:00
private function legacyStorageDir(string $uuid): string
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
return $this->storageBase() . '/branding/tenants/' . $uuid . '/favicon';
2026-02-04 23:31:53 +01:00
}
}