feat(.well-known): add .well-known/immich to reference API endpoint
This commit is contained in:
@@ -58,14 +58,15 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
|
||||
if (authenticationState.isAuthenticated) {
|
||||
var accessToken = Hive.box(userInfoBox).get(accessTokenKey);
|
||||
var endpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
||||
try {
|
||||
var endpoint = Uri.parse(Hive.box(userInfoBox).get(serverEndpointKey));
|
||||
|
||||
debugPrint("Attempting to connect to websocket");
|
||||
// Configure socket transports must be specified
|
||||
Socket socket = io(
|
||||
endpoint.toString().replaceAll('/api', ''),
|
||||
endpoint.origin,
|
||||
OptionBuilder()
|
||||
.setPath('/api/socket.io')
|
||||
.setPath("${endpoint.path}/socket.io")
|
||||
.setTransports(['websocket'])
|
||||
.enableReconnection()
|
||||
.enableForceNew()
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
class ApiService {
|
||||
late ApiClient _apiClient;
|
||||
@@ -22,6 +26,47 @@ class ApiService {
|
||||
deviceInfoApi = DeviceInfoApi(_apiClient);
|
||||
}
|
||||
|
||||
resolveEndpoint(String serverUrl) async {
|
||||
// Sanitize URL to only include origin+path
|
||||
final url = Uri.parse(serverUrl);
|
||||
final baseUrl = "${url.origin}${url.path}";
|
||||
|
||||
// Remove trailing slash, if exists
|
||||
final endpoint = baseUrl[baseUrl.length - 1] == "/"
|
||||
? baseUrl.substring(0, baseUrl.length - 1)
|
||||
: baseUrl;
|
||||
|
||||
// Check for .well-known definition, otherwise assume endpoint is full API address
|
||||
final apiEndpoint = await getWellKnownEndpoint(endpoint) ?? endpoint;
|
||||
return apiEndpoint;
|
||||
}
|
||||
|
||||
getWellKnownEndpoint(String baseUrl) async {
|
||||
final Client client = Client();
|
||||
|
||||
try {
|
||||
final res = await client.get(
|
||||
Uri.parse("$baseUrl/.well-known/immich"),
|
||||
headers: {"Accept": "application/json"},
|
||||
);
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
final data = jsonDecode(res.body);
|
||||
final endpoint = data['api']['endpoint'] as String;
|
||||
|
||||
if (endpoint.startsWith('/')) {
|
||||
// Full URL is relative to base
|
||||
return "$baseUrl$endpoint";
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Could not locate .well-known at $baseUrl: $e");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
setAccessToken(String accessToken) {
|
||||
_apiClient.addDefaultHeader('Authorization', 'Bearer $accessToken');
|
||||
}
|
||||
|
||||
@@ -22,8 +22,11 @@ class SplashScreenPage extends HookConsumerWidget {
|
||||
void performLoggingIn() async {
|
||||
try {
|
||||
if (loginInfo != null) {
|
||||
// Make sure API service is initialized
|
||||
apiService.setEndpoint(loginInfo.serverUrl);
|
||||
// Resolve API server endpoint from user provided serverUrl
|
||||
final serverEndpoint =
|
||||
await apiService.resolveEndpoint(loginInfo.serverUrl);
|
||||
apiService.setEndpoint(serverEndpoint);
|
||||
Hive.box(userInfoBox).put(serverEndpointKey, serverEndpoint);
|
||||
|
||||
var isSuccess = await ref
|
||||
.read(authenticationProvider.notifier)
|
||||
|
||||
Reference in New Issue
Block a user