From a51b06b453efac5e1fc65ad31344d9f99144bf81 Mon Sep 17 00:00:00 2001 From: Connery Noble Date: Thu, 12 Jan 2023 13:33:03 -0800 Subject: [PATCH] feat(.well-known): make schema optional (defaults to https) --- mobile/assets/i18n/cs-CZ.json | 1 - mobile/assets/i18n/da-DK.json | 1 - mobile/assets/i18n/de-DE.json | 1 - mobile/assets/i18n/en-US.json | 1 - mobile/assets/i18n/es-ES.json | 1 - mobile/assets/i18n/fi-FI.json | 1 - mobile/assets/i18n/fr-FR.json | 1 - mobile/assets/i18n/it-IT.json | 1 - mobile/assets/i18n/ja-JP.json | 1 - mobile/assets/i18n/ko-KR.json | 1 - mobile/assets/i18n/nl-NL.json | 1 - mobile/assets/i18n/pl-PL.json | 1 - mobile/assets/i18n/pt-BR.json | 1 - mobile/assets/i18n/pt-PT.json | 1 - mobile/assets/i18n/ru-RU.json | 1 - mobile/assets/i18n/sk-SK.json | 1 - mobile/assets/i18n/zh-CN.json | 1 - mobile/lib/modules/login/ui/login_form.dart | 7 ++-- mobile/lib/shared/services/api.service.dart | 36 ++++++++++++++------- 19 files changed, 27 insertions(+), 33 deletions(-) diff --git a/mobile/assets/i18n/cs-CZ.json b/mobile/assets/i18n/cs-CZ.json index ddb626e89d..c4dbc359ad 100644 --- a/mobile/assets/i18n/cs-CZ.json +++ b/mobile/assets/i18n/cs-CZ.json @@ -116,7 +116,6 @@ "login_form_email_hint": "tvůjmail@email.com", "login_form_endpoint_hint": "http://ip-tvého-serveru:port/api", "login_form_endpoint_url": "URL adresa serveru", - "login_form_err_http": "Prosím, uveďte http:// nebo https://", "login_form_err_invalid_email": "Neplatný e-mail", "login_form_err_leading_whitespace": "Úvodní mezera", "login_form_err_trailing_whitespace": "Koncová mezera", diff --git a/mobile/assets/i18n/da-DK.json b/mobile/assets/i18n/da-DK.json index 97f3ef7c66..6bfae49c1f 100644 --- a/mobile/assets/i18n/da-DK.json +++ b/mobile/assets/i18n/da-DK.json @@ -116,7 +116,6 @@ "login_form_email_hint": "din-email@email.com", "login_form_endpoint_hint": "http://din-server-ip:port/api", "login_form_endpoint_url": "Server Endpoint URL", - "login_form_err_http": "Angiv venligst http:// eller https://", "login_form_err_invalid_email": "Ugyldig email", "login_form_err_leading_whitespace": "Mellemrum før", "login_form_err_trailing_whitespace": "Mellemrum efter", diff --git a/mobile/assets/i18n/de-DE.json b/mobile/assets/i18n/de-DE.json index 42f2910608..662bdb3dc4 100644 --- a/mobile/assets/i18n/de-DE.json +++ b/mobile/assets/i18n/de-DE.json @@ -116,7 +116,6 @@ "login_form_email_hint": "deine@email.de", "login_form_endpoint_hint": "http://deine-server-ip:port/api", "login_form_endpoint_url": "Server URL", - "login_form_err_http": "Bitte gebe http:// oder https:// an", "login_form_err_invalid_email": "Ungültige E-Mail", "login_form_err_leading_whitespace": "Führendes Leerzichen", "login_form_err_trailing_whitespace": "Folgendes Leerzeichen", diff --git a/mobile/assets/i18n/en-US.json b/mobile/assets/i18n/en-US.json index 463a3f0c3b..f322976eed 100644 --- a/mobile/assets/i18n/en-US.json +++ b/mobile/assets/i18n/en-US.json @@ -116,7 +116,6 @@ "login_form_email_hint": "youremail@email.com", "login_form_endpoint_hint": "http://your-server-ip:port/api", "login_form_endpoint_url": "Server Endpoint URL", - "login_form_err_http": "Please specify http:// or https://", "login_form_err_invalid_email": "Invalid Email", "login_form_err_leading_whitespace": "Leading whitespace", "login_form_err_trailing_whitespace": "Trailing whitespace", diff --git a/mobile/assets/i18n/es-ES.json b/mobile/assets/i18n/es-ES.json index 9654b56733..9a1b2f9cf4 100644 --- a/mobile/assets/i18n/es-ES.json +++ b/mobile/assets/i18n/es-ES.json @@ -116,7 +116,6 @@ "login_form_email_hint": "tucorreo@correo.com", "login_form_endpoint_hint": "http://tu-ip-de-servidor:puerto/api", "login_form_endpoint_url": "URL del servidor", - "login_form_err_http": "Por favor, especifique http:// o https://", "login_form_err_invalid_email": "Correo electrónico no válido", "login_form_err_leading_whitespace": "Espacio en blanco inicial", "login_form_err_trailing_whitespace": "Espacio en blanco al final", diff --git a/mobile/assets/i18n/fi-FI.json b/mobile/assets/i18n/fi-FI.json index affa948bd1..8787f5f490 100644 --- a/mobile/assets/i18n/fi-FI.json +++ b/mobile/assets/i18n/fi-FI.json @@ -116,7 +116,6 @@ "login_form_email_hint": "sahkopostisi@esimerkki.fi", "login_form_endpoint_hint": "http://palvelimesi-osoite:portti/api", "login_form_endpoint_url": "Palvelimen URL", - "login_form_err_http": "Lisää http:// tai https://", "login_form_err_invalid_email": "Virheellinen sähköpostiosoite", "login_form_err_leading_whitespace": "Alussa välilyönti", "login_form_err_trailing_whitespace": "Lopussa välilyönti", diff --git a/mobile/assets/i18n/fr-FR.json b/mobile/assets/i18n/fr-FR.json index 6c19900ce3..58822a12c2 100644 --- a/mobile/assets/i18n/fr-FR.json +++ b/mobile/assets/i18n/fr-FR.json @@ -116,7 +116,6 @@ "login_form_email_hint": "votreemail@email.com", "login_form_endpoint_hint": "http://adresse-ip-serveur:port/api", "login_form_endpoint_url": "URL du point d'accès au serveur", - "login_form_err_http": "Veuillez préciser http:// ou https://", "login_form_err_invalid_email": "Email invalide", "login_form_err_leading_whitespace": "Espace en début de ligne", "login_form_err_trailing_whitespace": "Espace de fin de ligne", diff --git a/mobile/assets/i18n/it-IT.json b/mobile/assets/i18n/it-IT.json index 13fb9f54fd..531941f0f8 100644 --- a/mobile/assets/i18n/it-IT.json +++ b/mobile/assets/i18n/it-IT.json @@ -116,7 +116,6 @@ "login_form_email_hint": "tuaemail@email.com", "login_form_endpoint_hint": "http://ip-del-tuo-server:port/api", "login_form_endpoint_url": "Server Endpoint URL", - "login_form_err_http": "Per favore specificare http:// o https://", "login_form_err_invalid_email": "Email non valida", "login_form_err_leading_whitespace": "Whitespace all'inizio ", "login_form_err_trailing_whitespace": "Whitespace alla fine", diff --git a/mobile/assets/i18n/ja-JP.json b/mobile/assets/i18n/ja-JP.json index 9ca9959ea3..08125ff5e6 100644 --- a/mobile/assets/i18n/ja-JP.json +++ b/mobile/assets/i18n/ja-JP.json @@ -116,7 +116,6 @@ "login_form_email_hint": "example@email.com", "login_form_endpoint_hint": "https://example.com:port/api", "login_form_endpoint_url": "サーバーエンドポイントURL", - "login_form_err_http": "http://かhttps://かを指定してね", "login_form_err_invalid_email": "メールアドレスが有効じゃないよ", "login_form_err_leading_whitespace": "最初に半角スペースが含まれてるよ", "login_form_err_trailing_whitespace": "最後に半角スペースが含まれてるよ", diff --git a/mobile/assets/i18n/ko-KR.json b/mobile/assets/i18n/ko-KR.json index ffe22fa68a..5ea799006a 100644 --- a/mobile/assets/i18n/ko-KR.json +++ b/mobile/assets/i18n/ko-KR.json @@ -113,7 +113,6 @@ "login_form_email_hint": "youremail@email.com", "login_form_endpoint_hint": "https://your-server-ip:port/api", "login_form_endpoint_url": "서버 엔드포인트 URL", - "login_form_err_http": "엔드포인트는 http:// 또는 https://로 시작해야 합니다", "login_form_err_invalid_email": "잘못된 이메일 형식입니다", "login_form_err_leading_whitespace": "이메일 앞에 공백문자가 포함되어 있습니다", "login_form_err_trailing_whitespace": "이메일 뒤에 공백문자가 포함되어 있습니다", diff --git a/mobile/assets/i18n/nl-NL.json b/mobile/assets/i18n/nl-NL.json index d27232b533..bc14346ae1 100644 --- a/mobile/assets/i18n/nl-NL.json +++ b/mobile/assets/i18n/nl-NL.json @@ -79,7 +79,6 @@ "login_form_email_hint": "jouwemail@email.com", "login_form_endpoint_hint": "http://jouw-server-ip:port/api", "login_form_endpoint_url": "Server URL", - "login_form_err_http": "Voer http:// of https:// in", "login_form_err_invalid_email": "Ongeldige Email", "login_form_err_leading_whitespace": "Spatie aan het begin", "login_form_err_trailing_whitespace": "Spatie aan het eind", diff --git a/mobile/assets/i18n/pl-PL.json b/mobile/assets/i18n/pl-PL.json index 374cf16025..c83c4737e2 100644 --- a/mobile/assets/i18n/pl-PL.json +++ b/mobile/assets/i18n/pl-PL.json @@ -116,7 +116,6 @@ "login_form_email_hint": "twojmail@email.com", "login_form_endpoint_hint": "http://ip-twojego-serwera:port/api", "login_form_endpoint_url": "URL Serwera", - "login_form_err_http": "Proszę określić http:// lub https://", "login_form_err_invalid_email": "Niepoprawny Email", "login_form_err_leading_whitespace": "Białe znaki", "login_form_err_trailing_whitespace": "Białe znaki po przecinku", diff --git a/mobile/assets/i18n/pt-BR.json b/mobile/assets/i18n/pt-BR.json index 6b3ec1dd65..3a65fcb9fc 100644 --- a/mobile/assets/i18n/pt-BR.json +++ b/mobile/assets/i18n/pt-BR.json @@ -76,7 +76,6 @@ "login_form_email_hint": "youremail@email.com", "login_form_endpoint_hint": "http://your-server-ip:port/api", "login_form_endpoint_url": "Server Endpoint URL", - "login_form_err_http": "Please specify http:// or https://", "login_form_err_invalid_email": "E-mail inválido", "login_form_err_leading_whitespace": "Leading whitespace", "login_form_err_trailing_whitespace": "Trailing whitespace", diff --git a/mobile/assets/i18n/pt-PT.json b/mobile/assets/i18n/pt-PT.json index 59035f220a..f291d53329 100644 --- a/mobile/assets/i18n/pt-PT.json +++ b/mobile/assets/i18n/pt-PT.json @@ -116,7 +116,6 @@ "login_form_email_hint": "seuemail@email.com", "login_form_endpoint_hint": "http://ip-do-seu-servidor:porta/api", "login_form_endpoint_url": "URL do endpoint do servidor", - "login_form_err_http": "Por favor especifique http:// ou https://", "login_form_err_invalid_email": "Email Inválido", "login_form_err_leading_whitespace": "Espaço em branco no início", "login_form_err_trailing_whitespace": "Espaço em branco no fim", diff --git a/mobile/assets/i18n/ru-RU.json b/mobile/assets/i18n/ru-RU.json index ec261f3ea4..54d7af62ee 100644 --- a/mobile/assets/i18n/ru-RU.json +++ b/mobile/assets/i18n/ru-RU.json @@ -116,7 +116,6 @@ "login_form_email_hint": "youremail@email.com", "login_form_endpoint_hint": "http://your-server-ip:port/api", "login_form_endpoint_url": "Server Endpoint URL", - "login_form_err_http": "Please specify http:// or https://", "login_form_err_invalid_email": "Invalid Email", "login_form_err_leading_whitespace": "Leading whitespace", "login_form_err_trailing_whitespace": "Trailing whitespace", diff --git a/mobile/assets/i18n/sk-SK.json b/mobile/assets/i18n/sk-SK.json index 31b65ce22e..afb251ea22 100644 --- a/mobile/assets/i18n/sk-SK.json +++ b/mobile/assets/i18n/sk-SK.json @@ -116,7 +116,6 @@ "login_form_email_hint": "tvojmail@email.com", "login_form_endpoint_hint": "http://ip-tvojho-servera:port/api", "login_form_endpoint_url": "URL adresa servera", - "login_form_err_http": "Prosím, uveďte http:// alebo https://", "login_form_err_invalid_email": "Neplatný e-mail", "login_form_err_leading_whitespace": "Úvodná medzera", "login_form_err_trailing_whitespace": "Koncové medzera", diff --git a/mobile/assets/i18n/zh-CN.json b/mobile/assets/i18n/zh-CN.json index f306085875..4cd546d06f 100644 --- a/mobile/assets/i18n/zh-CN.json +++ b/mobile/assets/i18n/zh-CN.json @@ -116,7 +116,6 @@ "login_form_email_hint": "youremail@email.com", "login_form_endpoint_hint": "http://your-server-ip:port/api", "login_form_endpoint_url": "服务器地址", - "login_form_err_http": "请检查http://或https://", "login_form_err_invalid_email": "请输入正确的邮箱", "login_form_err_leading_whitespace": "前面空格", "login_form_err_trailing_whitespace": "后面空格", diff --git a/mobile/lib/modules/login/ui/login_form.dart b/mobile/lib/modules/login/ui/login_form.dart index 4819187b63..69202ac7e2 100644 --- a/mobile/lib/modules/login/ui/login_form.dart +++ b/mobile/lib/modules/login/ui/login_form.dart @@ -217,11 +217,8 @@ class ServerEndpointInput extends StatelessWidget { }) : super(key: key); String? _validateInput(String? url) { - if (url?.startsWith(RegExp(r'https?://')) == true) { - return null; - } else { - return 'login_form_err_http'.tr(); - } + // Can we do any other validation here instead? + return null; } @override diff --git a/mobile/lib/shared/services/api.service.dart b/mobile/lib/shared/services/api.service.dart index f93f1159ab..3454201431 100644 --- a/mobile/lib/shared/services/api.service.dart +++ b/mobile/lib/shared/services/api.service.dart @@ -26,19 +26,33 @@ class ApiService { deviceInfoApi = DeviceInfoApi(_apiClient); } + /// Takes a server URL and attempts to resolve the API endpoint. + /// + /// Input: [schema://]host[:port][/path] + /// schema - optional (default: https) + /// host - required + /// port - optional (default: based on schema) + /// path - optional (default: /.well-known/immich) resolveEndpoint(String serverUrl) async { - // Sanitize URL to only include origin+path - final url = Uri.parse(serverUrl); - final baseUrl = "${url.origin}${url.path}"; + // Add schema if none is set + final urlWithSchema = serverUrl.startsWith(RegExp(r"https?://")) + ? serverUrl + : "https://$serverUrl"; - // Remove trailing slash, if exists - final endpoint = baseUrl[baseUrl.length - 1] == "/" - ? baseUrl.substring(0, baseUrl.length - 1) - : baseUrl; + final url = Uri.parse(urlWithSchema); + final origin = url.origin; - // Check for .well-known definition, otherwise assume endpoint is full API address - final apiEndpoint = await getWellKnownEndpoint(endpoint) ?? endpoint; - return apiEndpoint; + // Trim trailing slash(es) from path + final path = url.path.replaceFirst(RegExp(r"/+$"), ""); + + if (path.isEmpty) { + // No path provided, lets check for /.well-known/immich + final wellKnownEndpoint = await getWellKnownEndpoint(origin); + if (wellKnownEndpoint) return wellKnownEndpoint; + } + + // Otherwise, assume the URL provided is the api endpoint + return "$origin$path"; } getWellKnownEndpoint(String baseUrl) async { @@ -61,7 +75,7 @@ class ApiService { return endpoint; } } catch (e) { - debugPrint("Could not locate .well-known at $baseUrl: $e"); + debugPrint("Could not locate /.well-known/immich at $baseUrl"); } return null;