|
|
@@ -8,6 +8,7 @@ use App\Services\UserService;
|
|
|
use App\Http\Requests\UserRequest;
|
|
|
use App\Http\Resources\UserResource;
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
+use Illuminate\Http\UploadedFile;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
@@ -72,9 +73,14 @@ class UserController extends Controller
|
|
|
$hasAvatar = $request->hasFile('avatar');
|
|
|
$avatar = $request->file('avatar');
|
|
|
|
|
|
+ $data = $request->validated();
|
|
|
+ $hasBase64 = ! empty($data['avatar_base64']);
|
|
|
+
|
|
|
Log::warning('[avatar-upload] Requisição recebida em updateMe', [
|
|
|
'user_id' => auth()->id(),
|
|
|
'has_avatar' => $hasAvatar,
|
|
|
+ 'has_avatar_base64'=> $hasBase64,
|
|
|
+ 'avatar_base64_len'=> $hasBase64 ? strlen($data['avatar_base64']) : null,
|
|
|
'avatar_valid' => $hasAvatar ? $avatar->isValid() : null,
|
|
|
'avatar_size' => $hasAvatar ? $avatar->getSize() : null,
|
|
|
'avatar_mime' => $hasAvatar ? $avatar->getMimeType() : null,
|
|
|
@@ -84,7 +90,23 @@ class UserController extends Controller
|
|
|
'content_type' => $request->header('Content-Type'),
|
|
|
]);
|
|
|
|
|
|
- $user = $this->service->updateMe($request->validated());
|
|
|
+ if ($hasBase64) {
|
|
|
+ $decoded = $this->decodeBase64ToUploadedFile($data['avatar_base64']);
|
|
|
+
|
|
|
+ Log::warning('[avatar-upload] Avatar base64 decodificado', [
|
|
|
+ 'decode_ok' => $decoded !== null,
|
|
|
+ 'decoded_size'=> $decoded?->getSize(),
|
|
|
+ 'decoded_mime'=> $decoded?->getClientMimeType(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if ($decoded) {
|
|
|
+ $data['avatar'] = $decoded;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ unset($data['avatar_base64']);
|
|
|
+
|
|
|
+ $user = $this->service->updateMe($data);
|
|
|
|
|
|
Log::warning('[avatar-upload] updateMe concluído com sucesso', [
|
|
|
'user_id' => $user->id,
|
|
|
@@ -100,6 +122,41 @@ class UserController extends Controller
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ private function decodeBase64ToUploadedFile(string $base64): ?UploadedFile
|
|
|
+ {
|
|
|
+ if (preg_match('/^data:(image\/[a-zA-Z0-9.+-]+);base64,(.+)$/s', $base64, $matches)) {
|
|
|
+ $mime = $matches[1];
|
|
|
+ $content = base64_decode($matches[2], true);
|
|
|
+ } else {
|
|
|
+ $mime = 'image/jpeg';
|
|
|
+ $content = base64_decode($base64, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($content === false || $content === '') {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ $extension = match ($mime) {
|
|
|
+ 'image/png' => 'png',
|
|
|
+ 'image/webp' => 'webp',
|
|
|
+ 'image/jpg', 'image/jpeg' => 'jpg',
|
|
|
+ default => 'jpg',
|
|
|
+ };
|
|
|
+
|
|
|
+ $tmpPath = tempnam(sys_get_temp_dir(), 'avatar_');
|
|
|
+
|
|
|
+ if ($tmpPath === false || file_put_contents($tmpPath, $content) === false) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return new UploadedFile(
|
|
|
+ path: $tmpPath,
|
|
|
+ originalName: 'avatar.'.$extension,
|
|
|
+ mimeType: $mime,
|
|
|
+ test: true,
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
public function getUserTypes(): JsonResponse
|
|
|
{
|
|
|
$user_types = $this->service->getUserTypes();
|