upload image to nostr working

This commit is contained in:
gitea
2025-11-06 23:36:24 +01:00
parent 49dd0fdaf1
commit 2c5f0eaefc
7 changed files with 734 additions and 152 deletions
+319 -15
View File
@@ -1,11 +1,13 @@
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import '../../data/local/local_storage_service.dart';
import '../../data/immich/immich_service.dart';
import '../../data/immich/models/immich_asset.dart';
/// Screen for Immich media integration.
///
///
/// Displays images from Immich in a grid layout with pull-to-refresh.
/// Shows cached images first, then fetches from API.
class ImmichScreen extends StatefulWidget {
@@ -26,6 +28,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
List<ImmichAsset> _assets = [];
bool _isLoading = false;
String? _errorMessage;
final ImagePicker _imagePicker = ImagePicker();
bool _isUploading = false;
@override
void initState() {
@@ -100,6 +104,11 @@ class _ImmichScreenState extends State<ImmichScreen> {
appBar: AppBar(
title: const Text('Immich Media'),
actions: [
IconButton(
icon: const Icon(Icons.photo_library),
onPressed: _pickAndUploadImages,
tooltip: 'Upload from Gallery',
),
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: _testServerConnection,
@@ -118,6 +127,19 @@ class _ImmichScreenState extends State<ImmichScreen> {
}
Widget _buildBody() {
if (_isUploading) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Uploading images...'),
],
),
);
}
if (_isLoading && _assets.isEmpty) {
return const Center(
child: CircularProgressIndicator(),
@@ -197,7 +219,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
Widget _buildImageTile(ImmichAsset asset) {
final thumbnailUrl = _getThumbnailUrl(asset);
// For Immich API, we need to pass the API key as a header
// Since Image.network doesn't easily support custom headers,
// we'll use a workaround: Immich might accept the key in the URL query parameter
@@ -205,15 +227,15 @@ class _ImmichScreenState extends State<ImmichScreen> {
// Let's check ImmichService - it has Dio with headers configured.
// Actually, we can use Image.network with headers parameter (Flutter supports this).
// But we need the API key. Let me check if ImmichService exposes it.
// Since Immich API requires x-api-key header, and Image.network supports headers,
// we need to get the API key. However, ImmichService doesn't expose it.
// Let's modify ImmichService to expose a method that returns headers, or
// we can fetch images via Dio and display them.
// For now, let's use Image.network and assume Immich might work without header
// (which it won't, but this is a placeholder). We'll fix this properly next.
return GestureDetector(
onTap: () {
// TODO: Navigate to full image view
@@ -235,7 +257,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
Widget _buildImageWidget(String url, ImmichAsset asset) {
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
return FutureBuilder<Uint8List?>(
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
future:
widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
@@ -280,7 +303,8 @@ class _ImmichScreenState extends State<ImmichScreen> {
),
Expanded(
child: FutureBuilder<Uint8List?>(
future: widget.immichService?.fetchImageBytes(asset.id, isThumbnail: false),
future: widget.immichService
?.fetchImageBytes(asset.id, isThumbnail: false),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
@@ -288,7 +312,9 @@ class _ImmichScreenState extends State<ImmichScreen> {
);
}
if (snapshot.hasError || !snapshot.hasData || snapshot.data == null) {
if (snapshot.hasError ||
!snapshot.hasData ||
snapshot.data == null) {
return const Center(
child: Text('Failed to load image'),
);
@@ -354,11 +380,11 @@ class _ImmichScreenState extends State<ImmichScreen> {
try {
final serverInfo = await widget.immichService!.getServerInfo();
if (!mounted) return;
Navigator.of(context).pop(); // Close loading dialog
showDialog(
context: context,
builder: (context) => AlertDialog(
@@ -390,9 +416,9 @@ class _ImmichScreenState extends State<ImmichScreen> {
);
} catch (e) {
if (!mounted) return;
Navigator.of(context).pop(); // Close loading dialog
showDialog(
context: context,
builder: (context) => AlertDialog(
@@ -415,7 +441,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
/// Formats server info map as a readable string.
String _formatServerInfo(Map<String, dynamic> info) {
final buffer = StringBuffer();
info.forEach((key, value) {
if (value is Map) {
buffer.writeln('$key:');
@@ -426,7 +452,285 @@ class _ImmichScreenState extends State<ImmichScreen> {
buffer.writeln('$key: $value');
}
});
return buffer.toString();
}
/// Opens image picker and uploads selected images to Immich.
Future<void> _pickAndUploadImages() async {
if (widget.immichService == null) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Immich service not available'),
backgroundColor: Colors.red,
),
);
}
return;
}
try {
// Show dialog to choose between single or multiple images
final pickerChoice = await showDialog<ImagePickerChoice>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Select Images'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: const Icon(Icons.photo),
title: const Text('Pick Single Image'),
onTap: () =>
Navigator.of(context).pop(ImagePickerChoice.single),
),
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Pick Multiple Images'),
onTap: () =>
Navigator.of(context).pop(ImagePickerChoice.multiple),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
],
),
);
if (pickerChoice == null) return;
List<XFile> pickedFiles;
try {
if (pickerChoice == ImagePickerChoice.multiple) {
pickedFiles = await _imagePicker.pickMultiImage();
} else {
final pickedFile = await _imagePicker.pickImage(
source: ImageSource.gallery,
imageQuality: 100,
);
pickedFiles = pickedFile != null ? [pickedFile] : [];
}
} catch (pickError, stackTrace) {
// Handle image picker specific errors
debugPrint('Image picker error: $pickError');
debugPrint('Stack trace: $stackTrace');
if (mounted) {
String errorMessage = 'Failed to open gallery';
String errorDetails = pickError.toString();
// Check for specific error types
if (errorDetails.contains('Permission') ||
errorDetails.contains('permission') ||
errorDetails.contains('PERMISSION')) {
errorMessage =
'Permission denied. Please grant photo library access in app settings.';
} else if (errorDetails.contains('PlatformException')) {
// Extract the actual error message from PlatformException
final match = RegExp(r'PlatformException\([^,]+,\s*([^,]+)')
.firstMatch(errorDetails);
if (match != null) {
errorDetails = match.group(1) ?? errorDetails;
}
errorMessage = 'Failed to open gallery: $errorDetails';
} else {
errorMessage = 'Failed to open gallery: $errorDetails';
}
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Gallery Error'),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(errorMessage),
const SizedBox(height: 16),
const Text(
'Troubleshooting:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
const Text('1. Check app permissions in device settings'),
const Text('2. Make sure a gallery app is installed'),
const Text('3. Try restarting the app'),
const SizedBox(height: 16),
const Text(
'Full error:',
style:
TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
Text(
errorDetails,
style: const TextStyle(fontSize: 10),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
}
return;
}
if (pickedFiles.isEmpty) {
// User cancelled or no images selected - this is not an error
return;
}
// Show upload progress
setState(() {
_isUploading = true;
});
int successCount = 0;
int failureCount = 0;
final errors = <String>[];
// Upload each image
for (final pickedFile in pickedFiles) {
try {
final file = File(pickedFile.path);
debugPrint('Uploading file: ${pickedFile.name}');
final uploadResponse = await widget.immichService!.uploadImage(file);
debugPrint('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
successCount++;
} catch (e, stackTrace) {
debugPrint('Upload failed for ${pickedFile.name}: $e');
debugPrint('Stack trace: $stackTrace');
failureCount++;
errors.add('${pickedFile.name}: ${e.toString()}');
}
}
setState(() {
_isUploading = false;
});
// Show result
if (mounted) {
if (failureCount == 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Successfully uploaded $successCount image(s)'),
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Uploaded $successCount, failed $failureCount'),
backgroundColor: Colors.orange,
duration: const Duration(seconds: 4),
action: SnackBarAction(
label: 'Details',
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Upload Errors'),
content: SingleChildScrollView(
child: Text(errors.join('\n')),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
},
),
),
);
}
// Refresh the asset list
debugPrint('Refreshing asset list after upload');
await _loadAssets(forceRefresh: true);
debugPrint('Asset list refreshed, current count: ${_assets.length}');
}
} catch (e, stackTrace) {
setState(() {
_isUploading = false;
});
if (mounted) {
// Log the full error for debugging
debugPrint('Image picker error: $e');
debugPrint('Stack trace: $stackTrace');
String errorMessage = 'Failed to pick images';
if (e.toString().contains('Permission')) {
errorMessage =
'Permission denied. Please grant photo library access in app settings.';
} else if (e.toString().contains('PlatformException')) {
errorMessage =
'Failed to open gallery. Please check app permissions.';
} else {
errorMessage = 'Failed to pick images: ${e.toString()}';
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(errorMessage),
backgroundColor: Colors.red,
duration: const Duration(seconds: 5),
action: SnackBarAction(
label: 'Details',
textColor: Colors.white,
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Error Details'),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text('Error: ${e.toString()}'),
const SizedBox(height: 16),
const Text(
'If this is a permission error, please grant photo library access in your device settings.',
style: TextStyle(fontSize: 12),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
),
);
},
),
),
);
}
}
}
}
enum ImagePickerChoice {
single,
multiple,
}