You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

889 lines
30 KiB

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import '../../core/logger.dart';
import '../../core/exceptions/immich_exception.dart';
import '../local/local_storage_service.dart';
import '../local/models/item.dart';
import '../media/media_service_interface.dart';
import 'models/immich_asset.dart';
import 'models/upload_response.dart';
/// Service for interacting with Immich API.
///
/// This service provides:
/// - Upload images to Immich
/// - Fetch image lists from Immich
/// - Store image metadata locally after uploads
///
/// The service is modular and UI-independent, designed for offline-first behavior.
class ImmichService implements MediaServiceInterface {
/// HTTP client for API requests.
final Dio _dio;
/// Local storage service for caching metadata.
final LocalStorageService _localStorage;
/// Immich API base URL.
final String _baseUrl;
/// Immich API key for authentication.
final String _apiKey;
/// Cache directory for storing fetched images.
Directory? _imageCacheDirectory;
/// Creates an [ImmichService] instance.
///
/// [baseUrl] - Immich server base URL (e.g., 'https://immich.example.com').
/// [apiKey] - Immich API key for authentication.
/// [localStorage] - Local storage service for caching metadata.
/// [dio] - Optional Dio instance for dependency injection (useful for testing).
ImmichService({
required String baseUrl,
required String apiKey,
required LocalStorageService localStorage,
Dio? dio,
}) : _baseUrl = baseUrl,
_apiKey = apiKey,
_localStorage = localStorage,
_dio = dio ?? Dio() {
_dio.options.baseUrl = baseUrl;
_dio.options.headers['x-api-key'] = apiKey;
// Don't set Content-Type globally - it should be set per request
// For JSON requests, it will be set automatically
// For multipart uploads, Dio will set it with the correct boundary
}
/// Uploads an image file to Immich.
///
/// [imageFile] - The image file to upload.
/// [albumId] - Optional album ID to add the image to.
///
/// Returns [UploadResponse] containing the uploaded asset ID.
///
/// Throws [ImmichException] if upload fails.
/// Automatically stores metadata in local storage upon successful upload.
Future<UploadResponse> uploadImageToImmich(
File imageFile, {
String? albumId,
}) async {
try {
if (!await imageFile.exists()) {
throw ImmichException('Image file does not exist: ${imageFile.path}');
}
// Get file metadata
final fileName = imageFile.path.split('/').last;
final fileStat = await imageFile.stat();
final fileCreatedAt = fileStat.changed;
final fileModifiedAt = fileStat.modified;
// Determine MIME type from file extension
final extension = fileName.split('.').last.toLowerCase();
switch (extension) {
case 'png':
break;
case 'jpg':
case 'jpeg':
break;
case 'gif':
break;
case 'webp':
break;
case 'heic':
case 'heif':
break;
}
// Generate device IDs (required by Immich API)
// Using a consistent device ID based on the app
const deviceId = 'flutter-app-boilerplate';
final deviceAssetId =
'device-asset-${fileCreatedAt.millisecondsSinceEpoch}';
// Format dates in ISO 8601 format (UTC)
final fileCreatedAtIso = fileCreatedAt.toUtc().toIso8601String();
final fileModifiedAtIso = fileModifiedAt.toUtc().toIso8601String();
// Prepare metadata according to Immich API format
// Format: [{"key":"mobile-app","value":{"caption":"...","tags":[]}}]
final metadata = [
{
'key': 'mobile-app',
'value': {
'caption': fileName,
'tags': <String>[],
}
}
];
final metadataJson = jsonEncode(metadata);
// Prepare form data for multipart upload
// Based on working curl command:
// curl -X POST "https://photos.satoshinakamoto.win/api/assets" \
// -H "x-api-key: ..." \
// -H "Content-Type: multipart/form-data" \
// -F "assetData=@file.png;type=image/png" \
// -F "deviceAssetId=device-asset-001" \
// -F "deviceId=device-123" \
// -F "fileCreatedAt=2025-11-06T12:34:56Z" \
// -F "fileModifiedAt=2025-11-06T12:34:56Z" \
// -F 'metadata=[{"key":"mobile-app","value":{"caption":"Test image","tags":[]}}]'
final formData = FormData.fromMap({
'assetData': await MultipartFile.fromFile(
imageFile.path,
filename: fileName,
),
'deviceAssetId': deviceAssetId,
'deviceId': deviceId,
'fileCreatedAt': fileCreatedAtIso,
'fileModifiedAt': fileModifiedAtIso,
'metadata': metadataJson,
if (albumId != null) 'albumId': albumId,
});
// Upload to Immich
// According to Immich API documentation: POST /api/assets
// Note: Don't set Content-Type header manually for multipart/form-data
// Dio will set it automatically with the correct boundary
// Determine the correct endpoint path
// If baseUrl already ends with /api, don't add it again
String endpointPath;
if (_baseUrl.endsWith('/api')) {
endpointPath = '/assets';
} else if (_baseUrl.endsWith('/api/')) {
endpointPath = 'assets';
} else {
endpointPath = '/api/assets';
}
final uploadUrl = '$_baseUrl$endpointPath';
Logger.debug('=== Immich Upload Request ===');
Logger.debug('URL: $uploadUrl');
Logger.debug('Base URL: $_baseUrl');
Logger.debug('File: $fileName, Size: ${await imageFile.length()} bytes');
Logger.debug('Device ID: $deviceId');
Logger.debug('Device Asset ID: $deviceAssetId');
Logger.debug('File Created At: $fileCreatedAtIso');
Logger.debug('File Modified At: $fileModifiedAtIso');
Logger.debug('Metadata: $metadataJson');
final response = await _dio.post(
endpointPath,
data: formData,
options: Options(
headers: {
'x-api-key': _apiKey,
// Don't set Content-Type - Dio handles it automatically for FormData
},
),
);
Logger.debug('=== Immich Upload Response ===');
Logger.debug('Status Code: ${response.statusCode}');
Logger.debug('Response Data: ${response.data}');
Logger.debug('Response Headers: ${response.headers}');
if (response.statusCode != 200 && response.statusCode != 201) {
final errorMessage = response.data is Map
? (response.data as Map)['message']?.toString() ??
response.statusMessage
: response.statusMessage;
Logger.error(
'Upload failed with status ${response.statusCode}: $errorMessage');
throw ImmichException(
'Upload failed: $errorMessage',
response.statusCode,
);
}
// Log the response data structure
if (response.data is Map) {
Logger.debug('Response is Map with keys: ${(response.data as Map).keys}');
Logger.debug('Full response map: ${response.data}');
} else if (response.data is List) {
Logger.debug(
'Response is List with ${(response.data as List).length} items');
Logger.debug('First item: ${(response.data as List).first}');
} else {
Logger.debug('Response type: ${response.data.runtimeType}');
Logger.debug('Response value: ${response.data}');
}
// Handle response - it might be a single object or an array
Map<String, dynamic> responseData;
if (response.data is List && (response.data as List).isNotEmpty) {
// If response is an array, take the first item
responseData = (response.data as List).first as Map<String, dynamic>;
Logger.debug('Using first item from array response');
} else if (response.data is Map) {
responseData = response.data as Map<String, dynamic>;
} else {
throw ImmichException(
'Unexpected response format: ${response.data.runtimeType}',
response.statusCode,
);
}
final uploadResponse = UploadResponse.fromJson(responseData);
Logger.debug('Parsed Upload Response:');
Logger.debug(' ID: ${uploadResponse.id}');
Logger.debug(' Duplicate: ${uploadResponse.duplicate}');
// Fetch full asset details to store complete metadata
Logger.debug('Fetching full asset details for ID: ${uploadResponse.id}');
try {
final asset = await _getAssetById(uploadResponse.id);
Logger.debug('Fetched asset: ${asset.id}, ${asset.fileName}');
// Store metadata in local storage
Logger.debug('Storing asset metadata in local storage');
await _storeAssetMetadata(asset);
Logger.debug('Asset metadata stored successfully');
} catch (e) {
// Log error but don't fail the upload - asset was uploaded successfully
Logger.warning('Failed to fetch/store asset metadata: $e');
Logger.warning('Upload was successful, but metadata caching failed');
}
return uploadResponse;
} on DioException catch (e) {
throw ImmichException(
'Upload failed: ${e.message ?? 'Unknown error'}',
e.response?.statusCode,
);
} catch (e) {
throw ImmichException('Upload failed: $e');
}
}
/// Uploads an image file (implements MediaServiceInterface).
@override
Future<Map<String, dynamic>> uploadImage(File imageFile) async {
final response = await uploadImageToImmich(imageFile);
return {
'id': response.id,
'url': getImageUrl(response.id),
};
}
@override
Future<Map<String, dynamic>> uploadVideo(File videoFile) async {
// Immich supports video uploads - use the same upload endpoint
// The API will detect the file type from the Content-Type header
try {
if (!await videoFile.exists()) {
throw ImmichException('Video file does not exist: ${videoFile.path}');
}
final fileName = videoFile.path.split('/').last;
final fileStat = await videoFile.stat();
final fileCreatedAt = fileStat.changed;
final fileModifiedAt = fileStat.modified;
// Create multipart form data
final formData = FormData.fromMap({
'assetData': await MultipartFile.fromFile(
videoFile.path,
filename: fileName,
contentType: DioMediaType.parse('video/mp4'),
),
'deviceAssetId': fileName,
'deviceId': 'flutter-app',
'fileCreatedAt': fileCreatedAt.toIso8601String(),
'fileModifiedAt': fileModifiedAt.toIso8601String(),
'isFavorite': 'false',
});
final response = await _dio.post(
'/asset/upload',
data: formData,
);
if (response.statusCode != 200 && response.statusCode != 201) {
throw ImmichException(
'Upload failed: ${response.statusMessage}',
response.statusCode,
);
}
final uploadResponse = UploadResponse.fromJson(response.data);
Logger.info('Video uploaded to Immich: ${uploadResponse.id}');
return {
'id': uploadResponse.id,
'url': getImageUrl(uploadResponse.id), // Immich uses same URL pattern for videos
};
} catch (e) {
Logger.error('Immich video upload error: $e');
if (e is ImmichException) {
rethrow;
}
throw ImmichException('Video upload failed: $e');
}
}
/// Fetches a list of assets from Immich.
///
/// Based on official Immich API documentation: https://api.immich.app/endpoints/search/searchAssets
/// Uses POST /api/search/metadata endpoint with search parameters.
///
/// [limit] - Maximum number of assets to fetch (default: 100).
/// [skip] - Number of assets to skip (for pagination).
///
/// Returns a list of [ImmichAsset] instances.
///
/// Throws [ImmichException] if fetch fails.
/// Automatically stores fetched metadata in local storage.
Future<List<ImmichAsset>> fetchAssets({
int limit = 100,
int skip = 0,
}) async {
try {
// Official endpoint: POST /api/search/metadata
// Response structure: {"assets": {"items": [...], "total": N, "count": N, "nextPage": ...}}
final response = await _dio.post(
'/api/search/metadata',
data: {
'limit': limit,
'skip': skip,
// Empty search to get all assets
},
);
if (response.statusCode != 200 && response.statusCode != 201) {
throw ImmichException(
'Failed to fetch assets: ${response.statusMessage}',
response.statusCode,
);
}
// Parse response structure: {"assets": {"items": [...], "total": N, "count": N}}
final responseData = response.data as Map<String, dynamic>;
if (!responseData.containsKey('assets')) {
throw ImmichException(
'Unexpected response format: missing "assets" field');
}
final assetsData = responseData['assets'] as Map<String, dynamic>;
if (!assetsData.containsKey('items')) {
throw ImmichException(
'Unexpected response format: missing "items" field in assets');
}
final assetsJson = assetsData['items'] as List<dynamic>;
final List<ImmichAsset> assets = assetsJson
.map((json) => ImmichAsset.fromJson(json as Map<String, dynamic>))
.toList();
// Store all fetched assets in local storage
for (final asset in assets) {
await _storeAssetMetadata(asset);
}
return assets;
} on DioException catch (e) {
final statusCode = e.response?.statusCode;
final errorData = e.response?.data;
String errorMessage;
if (errorData is Map) {
errorMessage = errorData['message']?.toString() ?? errorData.toString();
} else {
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
}
throw ImmichException(
'Failed to fetch assets: $errorMessage',
statusCode,
);
} catch (e) {
if (e is ImmichException) {
rethrow;
}
throw ImmichException('Failed to fetch assets: $e');
}
}
/// Fetches a single asset by ID.
///
/// Based on official Immich API documentation: https://api.immich.app/endpoints/assets
/// Endpoint: GET /api/assets/{id}
///
/// [assetId] - The unique identifier (UUID) of the asset.
///
/// Returns [ImmichAsset] if found.
///
/// Throws [ImmichException] if fetch fails.
Future<ImmichAsset> _getAssetById(String assetId) async {
try {
// Official endpoint: GET /api/assets/{id}
final response = await _dio.get('/api/assets/$assetId');
if (response.statusCode != 200) {
throw ImmichException(
'Failed to fetch asset: ${response.statusMessage}',
response.statusCode,
);
}
return ImmichAsset.fromJson(response.data as Map<String, dynamic>);
} on DioException catch (e) {
final statusCode = e.response?.statusCode;
final errorData = e.response?.data;
String errorMessage;
if (errorData is Map) {
errorMessage = errorData['message']?.toString() ?? errorData.toString();
} else {
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
}
throw ImmichException(
'Failed to fetch asset: $errorMessage',
statusCode,
);
} catch (e) {
throw ImmichException('Failed to fetch asset: $e');
}
}
/// Stores asset metadata in local storage.
///
/// [asset] - The asset to store.
Future<void> _storeAssetMetadata(ImmichAsset asset) async {
try {
final item = Item(
id: 'immich_${asset.id}',
data: {
'type': 'immich_asset',
'asset': asset.toJson(),
},
);
await _localStorage.insertItem(item);
} catch (e) {
// Log error but don't fail the upload/fetch operation
// Local storage is a cache, not critical for the operation
}
}
/// Gets locally cached asset metadata.
///
/// [assetId] - The unique identifier of the asset.
///
/// Returns [ImmichAsset] if found in local storage, null otherwise.
Future<ImmichAsset?> getCachedAsset(String assetId) async {
try {
final item = await _localStorage.getItem('immich_$assetId');
if (item == null) return null;
final assetData = item.data['asset'] as Map<String, dynamic>;
return ImmichAsset.fromLocalJson(assetData);
} catch (e) {
return null;
}
}
/// Gets all locally cached assets.
///
/// Returns a list of [ImmichAsset] instances from local storage.
Future<List<ImmichAsset>> getCachedAssets() async {
try {
final items = await _localStorage.getAllItems();
final List<ImmichAsset> assets = [];
for (final item in items) {
if (item.data['type'] == 'immich_asset') {
final assetData = item.data['asset'] as Map<String, dynamic>;
assets.add(ImmichAsset.fromLocalJson(assetData));
}
}
return assets;
} catch (e) {
return [];
}
}
/// Gets the thumbnail URL for an asset.
///
/// Uses GET /api/assets/{id}/thumbnail endpoint.
///
/// [assetId] - The unique identifier of the asset.
///
/// Returns the full URL to the thumbnail image.
String getThumbnailUrl(String assetId) {
return '$_baseUrl/api/assets/$assetId/thumbnail';
}
/// Gets the full image URL for an asset.
///
/// Uses GET /api/assets/{id}/original endpoint.
///
/// [assetId] - The unique identifier of the asset.
///
/// Returns the full URL to the original image file.
String getImageUrl(String assetId) {
return '$_baseUrl/api/assets/$assetId/original';
}
/// Creates a shared link for an asset (public URL).
///
/// [assetId] - The unique identifier of the asset.
///
/// Returns the public shared link URL, or null if creation fails.
///
/// Note: This creates a shared link that can be accessed without authentication.
/// Immich shared links are typically for albums, but we can create one with a single asset.
Future<String?> createSharedLinkForAsset(String assetId) async {
try {
// First, try to create an album with the asset, then create a shared link
// Or create a shared link directly if the API supports it
// POST /api/shared-links
// According to Immich API, shared links can be created with assetIds
final response = await _dio.post(
'/api/shared-links',
data: {
'type': 'ALBUM',
'assetIds': [assetId],
'description': 'Profile picture',
},
);
if (response.statusCode == 200 || response.statusCode == 201) {
final data = response.data as Map<String, dynamic>;
final shareId = data['id'] as String?;
if (shareId != null) {
// Return the public shared link URL
// Format: {baseUrl}/share/{shareId}
// Note: The actual format might be different - this is the typical Immich shared link format
final baseUrlWithoutApi = _baseUrl.replaceAll('/api', '').replaceAll(RegExp(r'/$'), '');
return '$baseUrlWithoutApi/share/$shareId';
}
}
return null;
} catch (e) {
Logger.warning('Failed to create shared link for asset: $e');
// If shared link creation fails, we'll fall back to authenticated URL
return null;
}
}
/// Gets or creates a public shared link for an asset.
///
/// This method attempts to create a shared link if one doesn't exist.
/// The shared link provides a public URL that doesn't require authentication.
///
/// [assetId] - The unique identifier of the asset.
///
/// Returns the public shared link URL, or the authenticated URL as fallback.
Future<String> getPublicUrlForAsset(String assetId) async {
try {
// Try to create a shared link
final sharedLink = await createSharedLinkForAsset(assetId);
if (sharedLink != null) {
return sharedLink;
}
} catch (e) {
Logger.warning('Failed to get public URL, using authenticated URL: $e');
}
// Fallback to authenticated URL if shared link creation fails
return getImageUrl(assetId);
}
/// Gets the base URL for Immich API.
String get baseUrl => _baseUrl;
/// Initializes the image cache directory.
/// Should be called after LocalStorageService is initialized.
Future<void> _ensureImageCacheDirectory() async {
if (_imageCacheDirectory != null) return;
try {
// Use the same cache directory as LocalStorageService if available
// Otherwise create a separate Immich image cache directory
final appDir = await getApplicationDocumentsDirectory();
_imageCacheDirectory = Directory(path.join(appDir.path, 'immich_image_cache'));
if (!await _imageCacheDirectory!.exists()) {
await _imageCacheDirectory!.create(recursive: true);
}
} catch (e) {
Logger.warning('Failed to initialize image cache directory: $e');
// Continue without cache - images will be fetched fresh each time
}
}
/// Gets the cache file path for an image.
String _getCacheFilePath(String assetId, bool isThumbnail) {
if (_imageCacheDirectory == null) return '';
final cacheKey = '${assetId}_${isThumbnail ? 'thumb' : 'full'}';
return path.join(_imageCacheDirectory!.path, cacheKey);
}
/// Fetches image bytes for an asset with local caching.
///
/// Uses GET /api/assets/{id}/thumbnail for thumbnails or GET /api/assets/{id}/original for full images.
/// Images are cached locally for offline access and faster loading.
///
/// [assetId] - The unique identifier of the asset (from metadata response).
/// [isThumbnail] - Whether to fetch thumbnail (true) or original image (false). Default: true.
///
/// Returns the image bytes as Uint8List.
///
/// Throws [ImmichException] if fetch fails.
Future<Uint8List> fetchImageBytes(String assetId,
{bool isThumbnail = true}) async {
// Ensure cache directory is initialized
await _ensureImageCacheDirectory();
// Check cache first
final cacheFilePath = _getCacheFilePath(assetId, isThumbnail);
if (cacheFilePath.isNotEmpty) {
final cacheFile = File(cacheFilePath);
if (await cacheFile.exists()) {
try {
final cachedBytes = await cacheFile.readAsBytes();
Logger.debug('Loaded image from cache: $assetId (${isThumbnail ? 'thumb' : 'full'})');
return Uint8List.fromList(cachedBytes);
} catch (e) {
Logger.warning('Failed to read cached image, fetching fresh: $e');
// Continue to fetch from network if cache read fails
}
}
}
// Cache miss - fetch from Immich
try {
// Use correct endpoint based on thumbnail vs original
final endpoint = isThumbnail
? '/api/assets/$assetId/thumbnail'
: '/api/assets/$assetId/original';
final response = await _dio.get<List<int>>(
endpoint,
options: Options(
responseType: ResponseType.bytes,
),
);
if (response.statusCode != 200) {
throw ImmichException(
'Failed to fetch image: ${response.statusMessage}',
response.statusCode,
);
}
final imageBytes = Uint8List.fromList(response.data ?? []);
// Cache the fetched image
if (cacheFilePath.isNotEmpty && imageBytes.isNotEmpty) {
try {
final cacheFile = File(cacheFilePath);
await cacheFile.writeAsBytes(imageBytes);
Logger.debug('Cached image: $assetId (${isThumbnail ? 'thumb' : 'full'})');
} catch (e) {
Logger.warning('Failed to cache image (non-fatal): $e');
// Don't fail the request if caching fails
}
}
return imageBytes;
} on DioException catch (e) {
final statusCode = e.response?.statusCode;
final errorData = e.response?.data;
String errorMessage;
if (errorData is Map) {
errorMessage = errorData['message']?.toString() ?? errorData.toString();
} else {
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
}
throw ImmichException(
'Failed to fetch image: $errorMessage',
statusCode,
);
} catch (e) {
throw ImmichException('Failed to fetch image: $e');
}
}
/// Gets the headers needed for authenticated image requests.
///
/// Returns a map of headers including the API key.
Map<String, String> getImageHeaders() {
return {
'x-api-key': _apiKey,
};
}
/// Deletes assets from Immich.
///
/// [assetIds] - List of asset UUIDs to delete.
///
/// Throws [ImmichException] if deletion fails.
Future<void> deleteAssets(List<String> assetIds) async {
if (assetIds.isEmpty) {
throw ImmichException('No asset IDs provided for deletion');
}
try {
Logger.debug('=== Immich Delete Assets ===');
Logger.debug('Asset IDs to delete: $assetIds');
Logger.debug('Count: ${assetIds.length}');
// DELETE /api/assets with ids in request body
// According to Immich API: DELETE /api/assets with body: {"ids": ["uuid1", "uuid2", ...]}
final requestBody = {
'ids': assetIds,
};
Logger.debug('Request body: $requestBody');
final response = await _dio.delete(
'/api/assets',
data: requestBody,
options: Options(
headers: {
'x-api-key': _apiKey,
'Content-Type': 'application/json',
},
),
);
Logger.debug('=== Immich Delete Response ===');
Logger.debug('Status Code: ${response.statusCode}');
Logger.debug('Response Data: ${response.data}');
if (response.statusCode != 200 && response.statusCode != 204) {
final errorMessage = response.data is Map
? (response.data as Map)['message']?.toString() ??
response.statusMessage
: response.statusMessage;
throw ImmichException(
'Delete failed: $errorMessage',
response.statusCode,
);
}
// Remove deleted assets from local cache
for (final assetId in assetIds) {
try {
await _localStorage.deleteItem('immich_$assetId');
} catch (e) {
Logger.warning('Failed to remove asset $assetId from cache: $e');
}
}
Logger.info('Successfully deleted ${assetIds.length} asset(s)');
} on DioException catch (e) {
final statusCode = e.response?.statusCode;
final errorData = e.response?.data;
String errorMessage;
if (errorData is Map) {
errorMessage = errorData['message']?.toString() ?? errorData.toString();
} else {
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
}
throw ImmichException(
'Delete failed: $errorMessage',
statusCode,
);
} catch (e) {
throw ImmichException('Delete failed: $e');
}
}
/// Tests the connection to Immich server by calling the /api/server/about endpoint.
///
/// Returns server information including version and status.
///
/// Throws [ImmichException] if the request fails.
Future<Map<String, dynamic>> getServerInfo() async {
try {
final response = await _dio.get('/api/server/about');
if (response.statusCode != 200) {
throw ImmichException(
'Failed to get server info: ${response.statusMessage}',
response.statusCode,
);
}
return response.data as Map<String, dynamic>;
} on DioException catch (e) {
final statusCode = e.response?.statusCode;
final errorData = e.response?.data;
String errorMessage;
if (errorData is Map) {
errorMessage = errorData['message']?.toString() ?? errorData.toString();
} else {
errorMessage = errorData?.toString() ?? e.message ?? 'Unknown error';
}
throw ImmichException(
'Failed to get server info: $errorMessage',
statusCode,
);
} catch (e) {
throw ImmichException('Failed to get server info: $e');
}
}
/// Clears the cache for a specific image URL or asset ID.
@override
Future<void> clearImageCache(String imageUrlOrAssetId) async {
// Extract asset ID from URL if it's a full URL
String assetId = imageUrlOrAssetId;
if (imageUrlOrAssetId.startsWith('http://') || imageUrlOrAssetId.startsWith('https://')) {
// Extract asset ID from Immich URL like: https://immich.example.com/api/assets/{id}/thumbnail
final uri = Uri.parse(imageUrlOrAssetId);
final pathSegments = uri.pathSegments;
if (pathSegments.length >= 3 && pathSegments[0] == 'api' && pathSegments[1] == 'assets') {
assetId = pathSegments[2];
} else {
// If we can't extract asset ID, try to use the URL as-is
assetId = imageUrlOrAssetId;
}
}
// Clear both thumbnail and full image cache
final thumbCachePath = _getCacheFilePath(assetId, true);
final fullCachePath = _getCacheFilePath(assetId, false);
try {
if (thumbCachePath.isNotEmpty) {
final thumbFile = File(thumbCachePath);
if (await thumbFile.exists()) {
await thumbFile.delete();
Logger.debug('Cleared thumbnail cache: $assetId');
}
}
if (fullCachePath.isNotEmpty) {
final fullFile = File(fullCachePath);
if (await fullFile.exists()) {
await fullFile.delete();
Logger.debug('Cleared full image cache: $assetId');
}
}
} catch (e) {
Logger.warning('Failed to clear image cache for $assetId: $e');
// Don't throw - cache clearing is best effort
}
}
}

Powered by TurnKey Linux.