55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
import 'dart:io';
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:food_hub_app/config/app_config.dart';
|
|
|
|
import 'package:minio/io.dart';
|
|
import 'package:minio/minio.dart';
|
|
|
|
class MinIOHelper {
|
|
static final MinIOHelper _instance = MinIOHelper._internal();
|
|
|
|
factory MinIOHelper() => _instance;
|
|
|
|
MinIOHelper._internal() {
|
|
_minio = Minio(
|
|
endPoint: AppConfig.rustfsIp,
|
|
port: 9100,
|
|
accessKey: "tHSFfcDW8qpCzKa2Xg6Y",
|
|
secretKey: "oq79EeYJ4jdczRp2IHUMCnbKtSw58NgDlG3sOkvX",
|
|
useSSL: false,
|
|
);
|
|
}
|
|
|
|
late Minio _minio;
|
|
|
|
Future<String> uploadFile({
|
|
required PlatformFile file,
|
|
Function(double)? onProgress,
|
|
}) async {
|
|
try {
|
|
String hashName = await _generateMD5HashName(file.path!);
|
|
String fileName = '$hashName${_getFileExtension(file.name)}';
|
|
|
|
await _minio.fPutObject(AppConfig.bucketName, fileName, file.path!);
|
|
return fileName;
|
|
} catch (e) {
|
|
throw Exception('文件上传失败: $e');
|
|
}
|
|
}
|
|
|
|
String _getFileExtension(String fileName) {
|
|
if (fileName.contains('.')) {
|
|
return '.${fileName.split('.').last.toLowerCase()}';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
Future<String> _generateMD5HashName(String filePath) async {
|
|
final file = File(filePath);
|
|
final bytes = await file.readAsBytes();
|
|
final hash = md5.convert(bytes);
|
|
return hash.toString();
|
|
}
|
|
}
|