52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\BroadcastEmail;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
class BroadcastEmailImageService
|
|
{
|
|
private const MAX_SIZE_BYTES = 5242880;
|
|
|
|
private array $allowedTypes = [
|
|
'image/jpeg' => 'jpg',
|
|
'image/png' => 'png',
|
|
'image/gif' => 'gif',
|
|
'image/webp' => 'webp',
|
|
];
|
|
|
|
public function store(UploadedFile $file): array
|
|
{
|
|
$mime = strtolower((string) $file->getMimeType());
|
|
if (!isset($this->allowedTypes[$mime])) {
|
|
return [
|
|
'ok' => false,
|
|
'status' => 415,
|
|
'error' => 'Unsupported image type',
|
|
];
|
|
}
|
|
|
|
if ($file->getSize() > self::MAX_SIZE_BYTES) {
|
|
return [
|
|
'ok' => false,
|
|
'status' => 413,
|
|
'error' => 'Image too large (max 5MB)',
|
|
];
|
|
}
|
|
|
|
$targetDir = public_path('uploads/email');
|
|
if (!is_dir($targetDir)) {
|
|
@mkdir($targetDir, 0755, true);
|
|
}
|
|
|
|
$ext = $this->allowedTypes[$mime];
|
|
$newName = uniqid('em_', true) . '.' . $ext;
|
|
$file->move($targetDir, $newName);
|
|
|
|
return [
|
|
'ok' => true,
|
|
'url' => asset('uploads/email/' . $newName),
|
|
];
|
|
}
|
|
}
|