|
|
|
|
@ -1,32 +1,14 @@
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import '../../core/logger.dart';
|
|
|
|
|
import '../../core/exceptions/immich_exception.dart';
|
|
|
|
|
import '../local/local_storage_service.dart';
|
|
|
|
|
import '../local/models/item.dart';
|
|
|
|
|
import 'models/immich_asset.dart';
|
|
|
|
|
import 'models/upload_response.dart';
|
|
|
|
|
|
|
|
|
|
/// Exception thrown when Immich API operations fail.
|
|
|
|
|
class ImmichException implements Exception {
|
|
|
|
|
/// Error message.
|
|
|
|
|
final String message;
|
|
|
|
|
|
|
|
|
|
/// HTTP status code if available.
|
|
|
|
|
final int? statusCode;
|
|
|
|
|
|
|
|
|
|
/// Creates an [ImmichException] with the provided message.
|
|
|
|
|
ImmichException(this.message, [this.statusCode]);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
String toString() {
|
|
|
|
|
if (statusCode != null) {
|
|
|
|
|
return 'ImmichException: $message (Status: $statusCode)';
|
|
|
|
|
}
|
|
|
|
|
return 'ImmichException: $message';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Service for interacting with Immich API.
|
|
|
|
|
///
|
|
|
|
|
/// This service provides:
|
|
|
|
|
@ -175,15 +157,15 @@ class ImmichService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final uploadUrl = '$_baseUrl$endpointPath';
|
|
|
|
|
debugPrint('=== Immich Upload Request ===');
|
|
|
|
|
debugPrint('URL: $uploadUrl');
|
|
|
|
|
debugPrint('Base URL: $_baseUrl');
|
|
|
|
|
debugPrint('File: $fileName, Size: ${await imageFile.length()} bytes');
|
|
|
|
|
debugPrint('Device ID: $deviceId');
|
|
|
|
|
debugPrint('Device Asset ID: $deviceAssetId');
|
|
|
|
|
debugPrint('File Created At: $fileCreatedAtIso');
|
|
|
|
|
debugPrint('File Modified At: $fileModifiedAtIso');
|
|
|
|
|
debugPrint('Metadata: $metadataJson');
|
|
|
|
|
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,
|
|
|
|
|
@ -196,17 +178,17 @@ class ImmichService {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
debugPrint('=== Immich Upload Response ===');
|
|
|
|
|
debugPrint('Status Code: ${response.statusCode}');
|
|
|
|
|
debugPrint('Response Data: ${response.data}');
|
|
|
|
|
debugPrint('Response Headers: ${response.headers}');
|
|
|
|
|
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;
|
|
|
|
|
debugPrint(
|
|
|
|
|
Logger.error(
|
|
|
|
|
'Upload failed with status ${response.statusCode}: $errorMessage');
|
|
|
|
|
throw ImmichException(
|
|
|
|
|
'Upload failed: $errorMessage',
|
|
|
|
|
@ -216,15 +198,15 @@ class ImmichService {
|
|
|
|
|
|
|
|
|
|
// Log the response data structure
|
|
|
|
|
if (response.data is Map) {
|
|
|
|
|
debugPrint('Response is Map with keys: ${(response.data as Map).keys}');
|
|
|
|
|
debugPrint('Full response map: ${response.data}');
|
|
|
|
|
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) {
|
|
|
|
|
debugPrint(
|
|
|
|
|
Logger.debug(
|
|
|
|
|
'Response is List with ${(response.data as List).length} items');
|
|
|
|
|
debugPrint('First item: ${(response.data as List).first}');
|
|
|
|
|
Logger.debug('First item: ${(response.data as List).first}');
|
|
|
|
|
} else {
|
|
|
|
|
debugPrint('Response type: ${response.data.runtimeType}');
|
|
|
|
|
debugPrint('Response value: ${response.data}');
|
|
|
|
|
Logger.debug('Response type: ${response.data.runtimeType}');
|
|
|
|
|
Logger.debug('Response value: ${response.data}');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle response - it might be a single object or an array
|
|
|
|
|
@ -232,7 +214,7 @@ class ImmichService {
|
|
|
|
|
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>;
|
|
|
|
|
debugPrint('Using first item from array response');
|
|
|
|
|
Logger.debug('Using first item from array response');
|
|
|
|
|
} else if (response.data is Map) {
|
|
|
|
|
responseData = response.data as Map<String, dynamic>;
|
|
|
|
|
} else {
|
|
|
|
|
@ -243,24 +225,24 @@ class ImmichService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final uploadResponse = UploadResponse.fromJson(responseData);
|
|
|
|
|
debugPrint('Parsed Upload Response:');
|
|
|
|
|
debugPrint(' ID: ${uploadResponse.id}');
|
|
|
|
|
debugPrint(' Duplicate: ${uploadResponse.duplicate}');
|
|
|
|
|
Logger.debug('Parsed Upload Response:');
|
|
|
|
|
Logger.debug(' ID: ${uploadResponse.id}');
|
|
|
|
|
Logger.debug(' Duplicate: ${uploadResponse.duplicate}');
|
|
|
|
|
|
|
|
|
|
// Fetch full asset details to store complete metadata
|
|
|
|
|
debugPrint('Fetching full asset details for ID: ${uploadResponse.id}');
|
|
|
|
|
Logger.debug('Fetching full asset details for ID: ${uploadResponse.id}');
|
|
|
|
|
try {
|
|
|
|
|
final asset = await _getAssetById(uploadResponse.id);
|
|
|
|
|
debugPrint('Fetched asset: ${asset.id}, ${asset.fileName}');
|
|
|
|
|
Logger.debug('Fetched asset: ${asset.id}, ${asset.fileName}');
|
|
|
|
|
|
|
|
|
|
// Store metadata in local storage
|
|
|
|
|
debugPrint('Storing asset metadata in local storage');
|
|
|
|
|
Logger.debug('Storing asset metadata in local storage');
|
|
|
|
|
await _storeAssetMetadata(asset);
|
|
|
|
|
debugPrint('Asset metadata stored successfully');
|
|
|
|
|
Logger.debug('Asset metadata stored successfully');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Log error but don't fail the upload - asset was uploaded successfully
|
|
|
|
|
debugPrint('Warning: Failed to fetch/store asset metadata: $e');
|
|
|
|
|
debugPrint('Upload was successful, but metadata caching failed');
|
|
|
|
|
Logger.warning('Failed to fetch/store asset metadata: $e');
|
|
|
|
|
Logger.warning('Upload was successful, but metadata caching failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uploadResponse;
|
|
|
|
|
@ -558,9 +540,9 @@ class ImmichService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
debugPrint('=== Immich Delete Assets ===');
|
|
|
|
|
debugPrint('Asset IDs to delete: $assetIds');
|
|
|
|
|
debugPrint('Count: ${assetIds.length}');
|
|
|
|
|
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", ...]}
|
|
|
|
|
@ -568,7 +550,7 @@ class ImmichService {
|
|
|
|
|
'ids': assetIds,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debugPrint('Request body: $requestBody');
|
|
|
|
|
Logger.debug('Request body: $requestBody');
|
|
|
|
|
|
|
|
|
|
final response = await _dio.delete(
|
|
|
|
|
'/api/assets',
|
|
|
|
|
@ -581,9 +563,9 @@ class ImmichService {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
debugPrint('=== Immich Delete Response ===');
|
|
|
|
|
debugPrint('Status Code: ${response.statusCode}');
|
|
|
|
|
debugPrint('Response Data: ${response.data}');
|
|
|
|
|
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
|
|
|
|
|
@ -601,11 +583,11 @@ class ImmichService {
|
|
|
|
|
try {
|
|
|
|
|
await _localStorage.deleteItem('immich_$assetId');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
debugPrint('Warning: Failed to remove asset $assetId from cache: $e');
|
|
|
|
|
Logger.warning('Failed to remove asset $assetId from cache: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debugPrint('Successfully deleted ${assetIds.length} asset(s)');
|
|
|
|
|
Logger.info('Successfully deleted ${assetIds.length} asset(s)');
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
final statusCode = e.response?.statusCode;
|
|
|
|
|
final errorData = e.response?.data;
|
|
|
|
|
|