[BUG] Fix upload attachment
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:model/model.dart';
|
||||
|
||||
class ComposerAPI {
|
||||
@@ -11,9 +13,15 @@ class ComposerAPI {
|
||||
ComposerAPI(this._dioClient);
|
||||
|
||||
Future<UploadResponse> uploadAttachment(UploadRequest uploadRequest) async {
|
||||
final file = File(uploadRequest.fileInfo.filePath);
|
||||
final headerParam = _dioClient.getHeaders();
|
||||
headerParam[HttpHeaders.contentTypeHeader] = uploadRequest.fileInfo.mimeType;
|
||||
headerParam[HttpHeaders.contentLengthHeader] = file.readAsBytesSync().length;
|
||||
|
||||
final resultJson = await _dioClient.post(
|
||||
Uri.decodeFull(uploadRequest.uploadUrl.toString()),
|
||||
data: File(uploadRequest.fileInfo.filePath).readAsBytesSync());
|
||||
return UploadResponse.fromJson(resultJson);
|
||||
options: Options(headers: headerParam),
|
||||
data: file.openRead());
|
||||
return UploadResponse.fromJson(jsonDecode(resultJson));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:mime/mime.dart';
|
||||
|
||||
class FileInfo with EquatableMixin {
|
||||
final String fileName;
|
||||
@@ -12,6 +13,8 @@ class FileInfo with EquatableMixin {
|
||||
return FileInfo('', '', 0);
|
||||
}
|
||||
|
||||
String get mimeType => lookupMimeType(filePath) ?? 'application/json; charset=UTF-8';
|
||||
|
||||
@override
|
||||
List<Object> get props => [fileName, filePath, fileSize];
|
||||
}
|
||||
@@ -57,6 +57,9 @@ dependencies:
|
||||
# http_parser
|
||||
http_parser: 4.0.0
|
||||
|
||||
# mime
|
||||
mime: 1.0.1
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
Reference in New Issue
Block a user