|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace App\GraphQL\Mutations; |
| 5 | + |
| 6 | +use App\Models\Permission; |
| 7 | +use App\Models\User; |
| 8 | +use GraphQL\Error\Error; |
| 9 | +use Illuminate\Support\Facades\Validator; |
| 10 | +use Intervention\Image\Drivers\Gd\Driver; |
| 11 | +use Intervention\Image\ImageManager; |
| 12 | +use Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded; |
| 13 | + |
| 14 | +class UpdateUserAvatar |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Max upload size in kilobytes (5 MB). |
| 18 | + */ |
| 19 | + private const MAX_SIZE_KB = 5 * 1024; |
| 20 | + |
| 21 | + /** |
| 22 | + * Upload or replace a user's avatar. |
| 23 | + * |
| 24 | + * @param array{id: string, avatar: \Illuminate\Http\UploadedFile} $args |
| 25 | + */ |
| 26 | + public function upload(null $_, array $args): User |
| 27 | + { |
| 28 | + $user = User::findOrFail($args['id']); |
| 29 | + |
| 30 | + if (!$user->hasPermissionTo(Permission::UPLOAD_AVATAR)) { |
| 31 | + throw new Error('This user is not permitted to upload an avatar.'); |
| 32 | + } |
| 33 | + |
| 34 | + $file = $args['avatar']; |
| 35 | + |
| 36 | + Validator::make( |
| 37 | + ['avatar' => $file], |
| 38 | + ['avatar' => [ |
| 39 | + 'required', |
| 40 | + 'file', |
| 41 | + 'mimetypes:' . implode(',', User::AVATAR_MIME_TYPES), |
| 42 | + 'max:' . self::MAX_SIZE_KB, |
| 43 | + ]] |
| 44 | + )->validate(); |
| 45 | + |
| 46 | + // Re-encode the image to strip EXIF (which may contain GPS |
| 47 | + // coordinates or device info) and anything a crafted file might |
| 48 | + // carry beyond pixel data. |
| 49 | + $ext = strtolower($file->getClientOriginalExtension()); |
| 50 | + $cleanPath = $this->stripMetadata($file->getRealPath(), $ext); |
| 51 | + |
| 52 | + try { |
| 53 | + $user->addMedia($cleanPath) |
| 54 | + ->usingFileName("avatar.{$ext}") |
| 55 | + ->toMediaCollection(User::AVATAR_COLLECTION); |
| 56 | + } catch (FileCannotBeAdded $e) { |
| 57 | + throw new Error('Unable to store avatar: ' . $e->getMessage()); |
| 58 | + } finally { |
| 59 | + if (file_exists($cleanPath)) { |
| 60 | + unlink($cleanPath); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return $user->refresh(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Decode → re-encode an image using intervention/image, which drops |
| 69 | + * EXIF and any ancillary metadata. Returns a path to a temp file the |
| 70 | + * caller must clean up. |
| 71 | + */ |
| 72 | + private function stripMetadata(string $sourcePath, string $extension): string |
| 73 | + { |
| 74 | + $manager = new ImageManager(new Driver()); |
| 75 | + $image = $manager->read($sourcePath); |
| 76 | + |
| 77 | + $encoded = match ($extension) { |
| 78 | + 'jpg', 'jpeg' => $image->toJpeg(90), |
| 79 | + 'webp' => $image->toWebp(90), |
| 80 | + default => $image->toPng(), |
| 81 | + }; |
| 82 | + |
| 83 | + $tempPath = tempnam(sys_get_temp_dir(), 'avatar_'); |
| 84 | + file_put_contents($tempPath, (string)$encoded); |
| 85 | + |
| 86 | + return $tempPath; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Remove a user's avatar. |
| 91 | + * |
| 92 | + * @param array{id: string} $args |
| 93 | + */ |
| 94 | + public function delete(null $_, array $args): User |
| 95 | + { |
| 96 | + $user = User::findOrFail($args['id']); |
| 97 | + $user->clearMediaCollection(User::AVATAR_COLLECTION); |
| 98 | + |
| 99 | + return $user->refresh(); |
| 100 | + } |
| 101 | +} |
0 commit comments