|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Atomjoy\Apilogin\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | +use Atomjoy\Apilogin\Events\UploadAvatar; |
| 8 | +use Atomjoy\Apilogin\Exceptions\JsonException; |
| 9 | +use Atomjoy\Apilogin\Http\Requests\UploadAvatarRequest; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Illuminate\Support\Facades\Auth; |
| 12 | +use Illuminate\Support\Facades\Storage; |
| 13 | +use Response; |
| 14 | + |
| 15 | +class UploadAvatarController extends Controller |
| 16 | +{ |
| 17 | + protected $disk = 's3'; |
| 18 | + |
| 19 | + function index(UploadAvatarRequest $request) |
| 20 | + { |
| 21 | + try { |
| 22 | + Auth::shouldUse('admin'); |
| 23 | + |
| 24 | + $user = Auth::user(); |
| 25 | + |
| 26 | + $filename = $user->id . '.webp'; |
| 27 | + |
| 28 | + // $path = $request->file('avatar')->storeAs('avatars/admin', $filename, 'public'); |
| 29 | + |
| 30 | + $path = Storage::disk($this->disk) |
| 31 | + ->putFileAs( |
| 32 | + 'avatars/admin', |
| 33 | + $request->file('avatar'), |
| 34 | + $filename |
| 35 | + ); |
| 36 | + |
| 37 | + $user->avatar = $path; |
| 38 | + $user->save(); |
| 39 | + |
| 40 | + UploadAvatar::dispatch($user, $path); |
| 41 | + |
| 42 | + return response()->json([ |
| 43 | + 'message' => __('apilogin.upload.avatar.success'), |
| 44 | + 'avatar' => $path, |
| 45 | + ], 200); |
| 46 | + } catch (Exception $e) { |
| 47 | + report($e); |
| 48 | + throw new JsonException(__('apilogin.upload.avatar.error'), 422); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function remove(Request $request) |
| 53 | + { |
| 54 | + try { |
| 55 | + Auth::shouldUse('admin'); |
| 56 | + |
| 57 | + $filename = 'avatars/admin/' . Auth::id() . '.webp'; |
| 58 | + |
| 59 | + if (Storage::disk($this->disk)->exists($filename)) { |
| 60 | + Storage::disk($this->disk)->delete($filename); |
| 61 | + Auth::user()->update(['avatar' => null]); |
| 62 | + } |
| 63 | + |
| 64 | + return response()->json([ |
| 65 | + 'message' => __('apilogin.remove.avatar.success'), |
| 66 | + ], 200); |
| 67 | + } catch (Exception $e) { |
| 68 | + report($e); |
| 69 | + throw new JsonException(__('apilogin.remove.avatar.error'), 422); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function show() |
| 74 | + { |
| 75 | + return $this->showAvatar(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Show avatar only for logged in users. |
| 80 | + */ |
| 81 | + public function showAvatar($default_avatar = 'js/components/input/profil/avatar.png') |
| 82 | + { |
| 83 | + try { |
| 84 | + Auth::shouldUse('admin'); |
| 85 | + |
| 86 | + $id = Auth::id() ?? 'error'; |
| 87 | + |
| 88 | + $filename = '/avatars/admin/' . $id . '.webp'; |
| 89 | + |
| 90 | + $exists = Storage::disk($this->disk)->exists($filename); |
| 91 | + |
| 92 | + if ($exists) { |
| 93 | + $mime = Storage::disk($this->disk)->mimeType($filename); |
| 94 | + |
| 95 | + $content = Storage::disk($this->disk)->get($filename); |
| 96 | + |
| 97 | + $response = Response::make($content, 200); |
| 98 | + |
| 99 | + $response->header("Content-Type", $mime); |
| 100 | + |
| 101 | + return $response; |
| 102 | + } else { |
| 103 | + $default = resource_path($default_avatar); |
| 104 | + |
| 105 | + if (!file_exists($default)) { |
| 106 | + $default = fake()->image( |
| 107 | + null, |
| 108 | + 128, |
| 109 | + 128, |
| 110 | + null, |
| 111 | + true, |
| 112 | + true, |
| 113 | + 'avatar', |
| 114 | + true, |
| 115 | + 'png' |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return response( |
| 120 | + file_get_contents($default) |
| 121 | + )->header('Content-Type', 'image/png'); |
| 122 | + } |
| 123 | + } catch (Exception $e) { |
| 124 | + report($e); |
| 125 | + throw new JsonException(__('apilogin.show.avatar.error'), 422); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments