99 lines
3.6 KiB
Dart
99 lines
3.6 KiB
Dart
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
import 'package:EasyDartModule/base/webserver/WebServer.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:shelf_multipart/shelf_multipart.dart'; // 导入 shelf_multipart 插件
|
|
|
|
import '../const/HttpStatusCode.dart';
|
|
import '../const/ResponseJsonCode.dart';
|
|
import '../model/ApiResponse.dart';
|
|
|
|
part 'FileUploadController.route.dart';
|
|
@RequestMapping(path: "/file")
|
|
class FileUploadController {
|
|
FileUploadController();
|
|
set callHandler(handler) => _callHandler = handler;
|
|
Map<HttpMethod, List<List>> get routeMap => routes;
|
|
|
|
// 上传文件
|
|
@RequestMapping(path: "/upload", method: HttpMethod.POST)
|
|
Future<Response> uploadFile(Request request) async {
|
|
ApiResponse<String> apiResponse = ApiResponse();
|
|
request.formData();
|
|
|
|
try {
|
|
// 检查请求的 Content-Type 是否为 multipart/form-data
|
|
var contentType = request.headers['content-type'];
|
|
if (contentType == null ||
|
|
!contentType.startsWith('multipart/form-data')) {
|
|
apiResponse.code = ResponseJsonCode.fail;
|
|
apiResponse.message = "请求格式必须是 multipart/form-data";
|
|
return Response(HttpStatusCode.internalServerError,
|
|
body: apiResponse.serialize());
|
|
}
|
|
if (request.formData() case var form?) {
|
|
String description = "上传成功";
|
|
String bucket = "";
|
|
String? fileExtension; // 文件后缀
|
|
|
|
var file;
|
|
await for (final formData in form.formData) {
|
|
if (formData.name == 'bucket') {
|
|
bucket = await formData.part.readString();
|
|
}
|
|
if (formData.name == 'file') {
|
|
final filename = formData.filename;
|
|
if (filename != null && filename.isNotEmpty) {
|
|
fileExtension = filename.split('.').last; // 提取文件后缀
|
|
}
|
|
file = await formData.part.readBytes();
|
|
}
|
|
}
|
|
if (bucket.isEmpty) {
|
|
description = "桶名不能为空";
|
|
}
|
|
if (file == null) {
|
|
description = "文件不能为空";
|
|
}
|
|
final String folderName =
|
|
DateFormat('yyyy-MM-dd').format(DateTime.now());
|
|
String objectName =
|
|
"$folderName/${DateTime.now().millisecondsSinceEpoch}.$fileExtension";
|
|
// 创建存储桶(如果不存在)
|
|
await EasyDartModule.storage.createBucket(bucket);
|
|
|
|
// 上传文件
|
|
var path =
|
|
await EasyDartModule.storage.uploadObject(bucket, objectName, file);
|
|
|
|
// EasyDartModule.storage.getObject(bucket, objectName).then((data) {
|
|
// print("下载文件");
|
|
// File("b.gif").writeAsBytesSync(data);
|
|
// print("下载完毕");
|
|
// });
|
|
return Response.ok(path);
|
|
} else if (request.multipart() case var multipart?) {
|
|
final description = StringBuffer('Regular multipart request\n');
|
|
|
|
await for (final part in multipart.parts) {
|
|
description.writeln('new part');
|
|
|
|
part.headers.forEach(
|
|
(key, value) => description.writeln('Header $key=$value'));
|
|
final content = await part.readString();
|
|
description.writeln('content: $content');
|
|
|
|
description.writeln('end of part');
|
|
}
|
|
return Response.ok(description.toString());
|
|
} else {
|
|
return Response.ok('Not a multipart request');
|
|
}
|
|
} catch (e) {
|
|
apiResponse.code = ResponseJsonCode.fail;
|
|
apiResponse.message = "文件上传失败: $e";
|
|
return Response(HttpStatusCode.internalServerError,
|
|
body: apiResponse.serialize());
|
|
}
|
|
}
|
|
}
|