2023-07-28 03:05:27 +00:00
|
|
|
import 'dart:async';
|
2023-01-19 15:45:37 +00:00
|
|
|
import 'dart:convert';
|
2023-07-28 03:05:27 +00:00
|
|
|
import 'dart:io';
|
2023-01-19 15:45:37 +00:00
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
2023-01-19 15:45:37 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-01 02:36:40 +00:00
|
|
|
import 'package:immich_mobile/entities/store.entity.dart';
|
2023-01-19 15:45:37 +00:00
|
|
|
import 'package:immich_mobile/utils/url_helper.dart';
|
2024-04-23 21:09:10 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 12:23:48 +00:00
|
|
|
import 'package:openapi/api.dart';
|
2023-01-19 15:45:37 +00:00
|
|
|
import 'package:http/http.dart';
|
2022-07-13 12:23:48 +00:00
|
|
|
|
2024-06-26 19:31:55 +00:00
|
|
|
class ApiService implements Authentication {
|
2022-07-13 12:23:48 +00:00
|
|
|
late ApiClient _apiClient;
|
|
|
|
|
2024-05-29 22:26:57 +00:00
|
|
|
late UsersApi usersApi;
|
2022-07-13 12:23:48 +00:00
|
|
|
late AuthenticationApi authenticationApi;
|
2022-11-20 17:43:10 +00:00
|
|
|
late OAuthApi oAuthApi;
|
2024-05-29 22:26:57 +00:00
|
|
|
late AlbumsApi albumsApi;
|
|
|
|
late AssetsApi assetsApi;
|
2023-03-23 15:08:14 +00:00
|
|
|
late SearchApi searchApi;
|
2024-08-20 12:50:14 +00:00
|
|
|
late ServerApi serverInfoApi;
|
2024-05-29 15:51:01 +00:00
|
|
|
late MapApi mapApi;
|
2024-05-29 22:26:57 +00:00
|
|
|
late PartnersApi partnersApi;
|
|
|
|
late PeopleApi peopleApi;
|
2023-09-10 12:51:18 +00:00
|
|
|
late AuditApi auditApi;
|
2024-05-29 22:26:57 +00:00
|
|
|
late SharedLinksApi sharedLinksApi;
|
2024-05-14 15:35:37 +00:00
|
|
|
late SyncApi syncApi;
|
2023-11-09 16:10:56 +00:00
|
|
|
late SystemConfigApi systemConfigApi;
|
2024-05-29 22:26:57 +00:00
|
|
|
late ActivitiesApi activitiesApi;
|
2024-02-08 21:57:54 +00:00
|
|
|
late DownloadApi downloadApi;
|
|
|
|
late TrashApi trashApi;
|
2024-08-19 17:37:15 +00:00
|
|
|
late StacksApi stacksApi;
|
2022-07-13 12:23:48 +00:00
|
|
|
|
2023-01-19 15:45:37 +00:00
|
|
|
ApiService() {
|
2023-03-23 01:36:44 +00:00
|
|
|
final endpoint = Store.tryGet(StoreKey.serverEndpoint);
|
|
|
|
if (endpoint != null && endpoint.isNotEmpty) {
|
|
|
|
setEndpoint(endpoint);
|
2023-01-19 15:45:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-04 20:35:13 +00:00
|
|
|
String? _accessToken;
|
2024-04-23 21:09:10 +00:00
|
|
|
final _log = Logger("ApiService");
|
2023-01-19 15:45:37 +00:00
|
|
|
|
2022-07-13 12:23:48 +00:00
|
|
|
setEndpoint(String endpoint) {
|
2024-06-26 19:31:55 +00:00
|
|
|
_apiClient = ApiClient(basePath: endpoint, authentication: this);
|
2024-02-04 20:35:13 +00:00
|
|
|
if (_accessToken != null) {
|
|
|
|
setAccessToken(_accessToken!);
|
2023-03-03 22:38:30 +00:00
|
|
|
}
|
2024-05-29 22:26:57 +00:00
|
|
|
usersApi = UsersApi(_apiClient);
|
2022-07-13 12:23:48 +00:00
|
|
|
authenticationApi = AuthenticationApi(_apiClient);
|
2022-11-20 17:43:10 +00:00
|
|
|
oAuthApi = OAuthApi(_apiClient);
|
2024-05-29 22:26:57 +00:00
|
|
|
albumsApi = AlbumsApi(_apiClient);
|
|
|
|
assetsApi = AssetsApi(_apiClient);
|
2024-08-20 12:50:14 +00:00
|
|
|
serverInfoApi = ServerApi(_apiClient);
|
2023-03-23 15:08:14 +00:00
|
|
|
searchApi = SearchApi(_apiClient);
|
2024-05-29 15:51:01 +00:00
|
|
|
mapApi = MapApi(_apiClient);
|
2024-05-29 22:26:57 +00:00
|
|
|
partnersApi = PartnersApi(_apiClient);
|
|
|
|
peopleApi = PeopleApi(_apiClient);
|
2023-09-10 12:51:18 +00:00
|
|
|
auditApi = AuditApi(_apiClient);
|
2024-05-29 22:26:57 +00:00
|
|
|
sharedLinksApi = SharedLinksApi(_apiClient);
|
2024-05-14 15:35:37 +00:00
|
|
|
syncApi = SyncApi(_apiClient);
|
2023-11-09 16:10:56 +00:00
|
|
|
systemConfigApi = SystemConfigApi(_apiClient);
|
2024-05-29 22:26:57 +00:00
|
|
|
activitiesApi = ActivitiesApi(_apiClient);
|
2024-02-08 21:57:54 +00:00
|
|
|
downloadApi = DownloadApi(_apiClient);
|
|
|
|
trashApi = TrashApi(_apiClient);
|
2024-08-19 17:37:15 +00:00
|
|
|
stacksApi = StacksApi(_apiClient);
|
2022-07-13 12:23:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:45:37 +00:00
|
|
|
Future<String> resolveAndSetEndpoint(String serverUrl) async {
|
2024-12-05 15:11:48 +00:00
|
|
|
final endpoint = await resolveEndpoint(serverUrl);
|
2023-01-19 15:45:37 +00:00
|
|
|
setEndpoint(endpoint);
|
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
// Save in local database for next startup
|
2023-03-23 01:36:44 +00:00
|
|
|
Store.put(StoreKey.serverEndpoint, endpoint);
|
2023-01-19 15:45:37 +00:00
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2024-12-05 15:11:48 +00:00
|
|
|
Future<String> resolveEndpoint(String serverUrl) async {
|
2023-01-19 15:45:37 +00:00
|
|
|
final url = sanitizeUrl(serverUrl);
|
|
|
|
|
2023-07-28 03:05:27 +00:00
|
|
|
if (!await _isEndpointAvailable(serverUrl)) {
|
|
|
|
throw ApiException(503, "Server is not reachable");
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:45:37 +00:00
|
|
|
// Check for /.well-known/immich
|
|
|
|
final wellKnownEndpoint = await _getWellKnownEndpoint(url);
|
|
|
|
if (wellKnownEndpoint.isNotEmpty) return wellKnownEndpoint;
|
|
|
|
|
|
|
|
// Otherwise, assume the URL provided is the api endpoint
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2023-07-28 03:05:27 +00:00
|
|
|
Future<bool> _isEndpointAvailable(String serverUrl) async {
|
|
|
|
if (!serverUrl.endsWith('/api')) {
|
|
|
|
serverUrl += '/api';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-10-07 01:45:23 +00:00
|
|
|
await setEndpoint(serverUrl);
|
2024-12-02 15:33:44 +00:00
|
|
|
await serverInfoApi.pingServer().timeout(const Duration(seconds: 5));
|
2023-07-28 03:05:27 +00:00
|
|
|
} on TimeoutException catch (_) {
|
|
|
|
return false;
|
|
|
|
} on SocketException catch (_) {
|
|
|
|
return false;
|
2024-04-29 14:17:49 +00:00
|
|
|
} catch (error, stackTrace) {
|
|
|
|
_log.severe(
|
|
|
|
"Error while checking server availability",
|
|
|
|
error,
|
|
|
|
stackTrace,
|
|
|
|
);
|
|
|
|
return false;
|
2023-07-28 03:05:27 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-19 15:45:37 +00:00
|
|
|
Future<String> _getWellKnownEndpoint(String baseUrl) async {
|
|
|
|
final Client client = Client();
|
|
|
|
|
|
|
|
try {
|
2024-06-26 19:31:55 +00:00
|
|
|
var headers = {"Accept": "application/json"};
|
|
|
|
headers.addAll(getRequestHeaders());
|
|
|
|
|
2023-01-19 15:45:37 +00:00
|
|
|
final res = await client.get(
|
|
|
|
Uri.parse("$baseUrl/.well-known/immich"),
|
2024-06-26 19:31:55 +00:00
|
|
|
headers: headers,
|
2023-01-19 15:45:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
final data = jsonDecode(res.body);
|
|
|
|
final endpoint = data['api']['endpoint'].toString();
|
|
|
|
|
|
|
|
if (endpoint.startsWith('/')) {
|
|
|
|
// Full URL is relative to base
|
|
|
|
return "$baseUrl$endpoint";
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Could not locate /.well-known/immich at $baseUrl");
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
void setAccessToken(String accessToken) {
|
2024-02-04 20:35:13 +00:00
|
|
|
_accessToken = accessToken;
|
2024-06-26 19:31:55 +00:00
|
|
|
Store.put(StoreKey.accessToken, accessToken);
|
|
|
|
}
|
|
|
|
|
2024-11-26 18:43:44 +00:00
|
|
|
Future<void> setDeviceInfoHeader() async {
|
|
|
|
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
|
|
|
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
final iosInfo = await deviceInfoPlugin.iosInfo;
|
|
|
|
authenticationApi.apiClient
|
|
|
|
.addDefaultHeader('deviceModel', iosInfo.utsname.machine);
|
|
|
|
authenticationApi.apiClient.addDefaultHeader('deviceType', 'iOS');
|
|
|
|
} else {
|
|
|
|
final androidInfo = await deviceInfoPlugin.androidInfo;
|
|
|
|
authenticationApi.apiClient
|
|
|
|
.addDefaultHeader('deviceModel', androidInfo.model);
|
|
|
|
authenticationApi.apiClient.addDefaultHeader('deviceType', 'Android');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 19:31:55 +00:00
|
|
|
static Map<String, String> getRequestHeaders() {
|
|
|
|
var accessToken = Store.get(StoreKey.accessToken, "");
|
|
|
|
var customHeadersStr = Store.get(StoreKey.customHeaders, "");
|
|
|
|
var header = <String, String>{};
|
|
|
|
if (accessToken.isNotEmpty) {
|
|
|
|
header['x-immich-user-token'] = accessToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (customHeadersStr.isEmpty) {
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
var customHeaders = jsonDecode(customHeadersStr) as Map;
|
|
|
|
customHeaders.forEach((key, value) {
|
|
|
|
header[key] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> applyToParams(
|
|
|
|
List<QueryParam> queryParams,
|
|
|
|
Map<String, String> headerParams,
|
|
|
|
) {
|
|
|
|
return Future<void>(() {
|
|
|
|
var headers = ApiService.getRequestHeaders();
|
|
|
|
headerParams.addAll(headers);
|
|
|
|
});
|
2022-07-13 12:23:48 +00:00
|
|
|
}
|
2023-03-03 22:38:30 +00:00
|
|
|
|
|
|
|
ApiClient get apiClient => _apiClient;
|
2022-07-13 12:23:48 +00:00
|
|
|
}
|