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