Implement file sharing functionality with public share links and associated API endpoints
This commit is contained in:
168
b0esche_cloud/lib/pages/public_file_viewer.dart
Normal file
168
b0esche_cloud/lib/pages/public_file_viewer.dart
Normal file
@@ -0,0 +1,168 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:js_interop';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'dart:typed_data';
|
||||
import '../theme/app_theme.dart';
|
||||
import '../services/api_client.dart';
|
||||
import '../injection.dart';
|
||||
|
||||
class PublicFileViewer extends StatefulWidget {
|
||||
final String token;
|
||||
|
||||
const PublicFileViewer({super.key, required this.token});
|
||||
|
||||
@override
|
||||
State<PublicFileViewer> createState() => _PublicFileViewerState();
|
||||
}
|
||||
|
||||
class _PublicFileViewerState extends State<PublicFileViewer> {
|
||||
bool _isLoading = true;
|
||||
String? _error;
|
||||
Map<String, dynamic>? _fileData;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadFileData();
|
||||
}
|
||||
|
||||
Future<void> _loadFileData() async {
|
||||
try {
|
||||
final apiClient = getIt<ApiClient>();
|
||||
final response = await apiClient.getRaw('/public/share/${widget.token}');
|
||||
|
||||
setState(() {
|
||||
_fileData = response;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = 'This link is invalid or has expired.';
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _downloadFile() {
|
||||
if (_fileData != null && _fileData!['downloadUrl'] != null) {
|
||||
// Use http package to download
|
||||
http.get(Uri.parse(_fileData!['downloadUrl'])).then((response) {
|
||||
if (response.statusCode == 200) {
|
||||
// Trigger download in web
|
||||
final uint8List = Uint8List.fromList(response.bodyBytes);
|
||||
final jsUint8Array = JSUint8Array(
|
||||
uint8List.buffer.toJS,
|
||||
uint8List.offsetInBytes,
|
||||
uint8List.length,
|
||||
);
|
||||
final jsArray = JSArray<JSAny>.withLength(1);
|
||||
jsArray[0] = jsUint8Array;
|
||||
final blob = web.Blob(jsArray);
|
||||
final url = web.URL.createObjectURL(blob);
|
||||
final anchor = web.HTMLAnchorElement()
|
||||
..href = url
|
||||
..download = _fileData!['fileName'] ?? 'download';
|
||||
anchor.click();
|
||||
web.URL.revokeObjectURL(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppTheme.primaryBackground,
|
||||
appBar: AppBar(
|
||||
backgroundColor: AppTheme.primaryBackground,
|
||||
elevation: 0,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close, color: AppTheme.primaryText),
|
||||
onPressed: () => context.go('/'),
|
||||
),
|
||||
title: Text(
|
||||
_fileData?['fileName'] ?? 'Shared File',
|
||||
style: TextStyle(color: AppTheme.primaryText),
|
||||
),
|
||||
actions: [
|
||||
if (_fileData != null)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.download, color: AppTheme.primaryText),
|
||||
onPressed: _downloadFile,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
child: _isLoading
|
||||
? const CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(AppTheme.accentColor),
|
||||
)
|
||||
: _error != null
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.link_off, size: 64, color: Colors.red[400]),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_error!,
|
||||
style: TextStyle(
|
||||
color: AppTheme.primaryText,
|
||||
fontSize: 18,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: _fileData != null
|
||||
? Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.insert_drive_file,
|
||||
size: 64,
|
||||
color: AppTheme.primaryText,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_fileData!['fileName'] ?? 'Unknown file',
|
||||
style: TextStyle(
|
||||
color: AppTheme.primaryText,
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Size: ${(_fileData!['fileSize'] ?? 0) ~/ 1024} KB',
|
||||
style: TextStyle(color: AppTheme.secondaryText),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _downloadFile,
|
||||
icon: const Icon(Icons.download),
|
||||
label: const Text('Download File'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppTheme.accentColor,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: const SizedBox(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user