59 lines
1.5 KiB
Dart
59 lines
1.5 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:minio/io.dart';
|
|
import 'package:minio/minio.dart';
|
|
|
|
import 'file_utils.dart';
|
|
|
|
class MinIOHelper {
|
|
static final MinIOHelper _instance = MinIOHelper._internal();
|
|
|
|
factory MinIOHelper() => _instance;
|
|
|
|
final String ip = '14.103.235.151';
|
|
final String fileUrl = 'http://14.103.235.151:9100';
|
|
|
|
MinIOHelper._internal() {
|
|
_minio = Minio(
|
|
endPoint: ip,
|
|
port: 9100,
|
|
accessKey: "tHSFfcDW8qpCzKa2Xg6Y",
|
|
secretKey: "oq79EeYJ4jdczRp2IHUMCnbKtSw58NgDlG3sOkvX",
|
|
useSSL: false,
|
|
);
|
|
}
|
|
|
|
late Minio _minio;
|
|
|
|
Future<String> uploadFile({
|
|
required PlatformFile file,
|
|
required String bucketName,
|
|
Function(double)? onProgress,
|
|
}) async {
|
|
try {
|
|
if (isImageFile(file.name)) {
|
|
// 压缩图片
|
|
final compressedFile = await compressImage(File(file.path!));
|
|
|
|
String hashName = await generateMD5HashName(compressedFile.path);
|
|
String fileName = '$hashName${getFileExtension(file.name)}';
|
|
|
|
await _minio.fPutObject(bucketName, fileName, compressedFile.path);
|
|
await compressedFile.delete();
|
|
|
|
return fileName;
|
|
} else {
|
|
String hashName = await generateMD5HashName(file.path!);
|
|
String fileName = '$hashName${getFileExtension(file.name)}';
|
|
|
|
await _minio.fPutObject(bucketName, fileName, file.path!);
|
|
|
|
return fileName;
|
|
}
|
|
} catch (e) {
|
|
throw Exception('文件上传失败: $e');
|
|
}
|
|
}
|
|
}
|