trypost/app/Services/Brand/LogoAttacher.php
Paulo Castellano 3bc481ac07 Make LogoAttacher honor its swallow-all contract; drop the caller try/catch
LogoAttacher::attach promised in its docblock that any failure — including a
persistence error — is logged and swallowed so the caller need not handle it.
But the persistence block was try/finally with no catch, so a Throwable from
clearMediaCollection/addMediaFromPath escaped. Both call sites (store and
updateSettings) each wrapped the call in an identical try/catch + Log::warning
to compensate — a band-aid duplicated across the controller.

Fix it at the root: the persistence block now catches Throwable, logs it, and
returns false, matching the documented contract. Both controller call sites
collapse to a single attach() line, and the now-unused Log/Throwable imports
are dropped.

Adds LogoAttacherTest covering the success path, the swallowed persistence
failure, a failed fetch, and a rejected mime type.
2026-07-03 20:15:37 -03:00

127 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Brand;
use App\Models\Workspace;
use Illuminate\Support\Facades\Log;
use Throwable;
class LogoAttacher
{
private const int MAX_LOGO_BYTES = 2 * 1024 * 1024;
private const array ALLOWED_MIMES = [
'image/png',
'image/jpeg',
'image/webp',
'image/gif',
'image/x-icon',
'image/vnd.microsoft.icon',
];
public function __construct(
private readonly SafeHttpFetcher $fetcher,
) {}
/**
* Download the logo at $logoUrl, validate it, and attach it to the workspace's
* 'logo' media collection. Returns true if the logo was attached.
*
* Any failure (unreachable, wrong mime, oversized, persistence error) is logged
* and swallowed — the caller does not need to handle it.
*/
public function attach(Workspace $workspace, string $logoUrl): bool
{
$response = $this->fetcher->tryGet($logoUrl);
if ($response === null) {
Log::debug('Logo fetch failed', ['url' => $logoUrl]);
return false;
}
$contentType = $this->parseMimeType($response->header('Content-Type'));
if (! in_array($contentType, self::ALLOWED_MIMES, true)) {
Log::debug('Logo rejected — disallowed mime', ['url' => $logoUrl, 'mime' => $contentType]);
return false;
}
$declaredLength = (int) $response->header('Content-Length');
if ($declaredLength > self::MAX_LOGO_BYTES) {
Log::debug('Logo rejected — content-length too large', ['url' => $logoUrl, 'bytes' => $declaredLength]);
return false;
}
$body = $response->body();
if (strlen($body) > self::MAX_LOGO_BYTES) {
Log::debug('Logo rejected — body too large', ['url' => $logoUrl, 'bytes' => strlen($body)]);
return false;
}
$tempPath = $this->writeToTempFile($body, $this->extensionForMime($contentType));
try {
$workspace->clearMediaCollection('logo');
$workspace->addMediaFromPath(
$tempPath,
'logo.'.$this->extensionForMime($contentType),
'logo',
['ai_autofill' => true],
);
return true;
} catch (Throwable $e) {
Log::warning('Logo rejected — persistence error', ['url' => $logoUrl, 'error' => $e->getMessage()]);
return false;
} finally {
if (file_exists($tempPath)) {
@unlink($tempPath);
}
}
}
private function writeToTempFile(string $body, string $extension): string
{
$base = tempnam(sys_get_temp_dir(), 'logo_');
// tempnam() creates an empty file without extension. Add the extension
// by renaming so we don't leave the extension-less file behind.
$withExtension = $base.'.'.$extension;
rename($base, $withExtension);
file_put_contents($withExtension, $body);
return $withExtension;
}
private function parseMimeType(?string $header): string
{
if ($header === null || $header === '') {
return '';
}
$first = explode(';', $header)[0];
return strtolower(trim($first));
}
private function extensionForMime(string $mime): string
{
return match ($mime) {
'image/jpeg' => 'jpg',
'image/webp' => 'webp',
'image/gif' => 'gif',
'image/x-icon', 'image/vnd.microsoft.icon' => 'ico',
default => 'png',
};
}
}