From 8b43066632f138e56210bc7792e38176145b3f9c Mon Sep 17 00:00:00 2001
From: waclaw66 <waclaw66@seznam.cz>
Date: Wed, 5 Mar 2025 03:25:57 +0100
Subject: [PATCH] fix(mobile): .well-known usage (#16577)

fix: .well-known

Co-authored-by: Alex <alex.tran1502@gmail.com>
---
 mobile/lib/services/api.service.dart | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/mobile/lib/services/api.service.dart b/mobile/lib/services/api.service.dart
index 76701491c7..b87e10f020 100644
--- a/mobile/lib/services/api.service.dart
+++ b/mobile/lib/services/api.service.dart
@@ -84,15 +84,17 @@ class ApiService implements Authentication {
   ///  port   - optional (default: based on schema)
   ///  path   - optional
   Future<String> resolveEndpoint(String serverUrl) async {
-    final url = sanitizeUrl(serverUrl);
-
-    if (!await _isEndpointAvailable(serverUrl)) {
-      throw ApiException(503, "Server is not reachable");
-    }
+    String url = sanitizeUrl(serverUrl);
 
     // Check for /.well-known/immich
     final wellKnownEndpoint = await _getWellKnownEndpoint(url);
-    if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
+    if (wellKnownEndpoint.isNotEmpty) {
+      url = sanitizeUrl(wellKnownEndpoint);
+    }
+
+    if (!await _isEndpointAvailable(url)) {
+      throw ApiException(503, "Server is not reachable");
+    }
 
     // Otherwise, assume the URL provided is the api endpoint
     return url;
@@ -128,10 +130,12 @@ class ApiService implements Authentication {
       var headers = {"Accept": "application/json"};
       headers.addAll(getRequestHeaders());
 
-      final res = await client.get(
-        Uri.parse("$baseUrl/.well-known/immich"),
-        headers: headers,
-      );
+      final res = await client
+          .get(
+            Uri.parse("$baseUrl/.well-known/immich"),
+            headers: headers,
+          )
+          .timeout(const Duration(seconds: 5));
 
       if (res.statusCode == 200) {
         final data = jsonDecode(res.body);