where('user_id', $user_id)->value('icon'); } catch (\Throwable $e) { $icon = null; } $candidates = []; if (!empty($icon)) { // common legacy locations to check $candidates[] = base_path('oldSite/www/files/usericons/' . $icon); $candidates[] = base_path('oldSite/www/files/usericons/' . rawurlencode($icon)); $candidates[] = base_path('oldSite/www/files/usericons/' . basename($icon)); $candidates[] = public_path('avatar/' . $user_id . '/' . $icon); $candidates[] = public_path('avatar/' . $user_id . '/' . basename($icon)); $candidates[] = storage_path('app/public/usericons/' . $icon); $candidates[] = storage_path('app/public/usericons/' . basename($icon)); } // find first readable file $found = null; foreach ($candidates as $path) { if ($path && file_exists($path) && is_readable($path)) { $found = $path; break; } } if ($found) { $type = @exif_imagetype($found); if ($type) { $mime = image_type_to_mime_type($type); } else { $f = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($f, $found) ?: 'application/octet-stream'; finfo_close($f); } return response()->file($found, ['Content-Type' => $mime]); } // fallback to default if (file_exists($defaultAvatar) && is_readable($defaultAvatar)) { return response()->file($defaultAvatar, ['Content-Type' => 'image/jpeg']); } // final fallback: 404 abort(404); } }