storageservice.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'dart:typed_data';
  2. import 'package:flyinsonolite/jsonrpc/fisLib/services/index.dart';
  3. import 'package:flyinsonolite/infrastructure/logger.dart';
  4. import 'package:http/http.dart' as http;
  5. import 'package:fis_jsonrpc/rpc.dart';
  6. import 'package:flyinsonolite/jsonrpc/jsonrpcclient.dart';
  7. import 'package:flyinsonolite/infrastructure/storage.dart';
  8. ///存储服务扩展类
  9. extension StorageServiceExt on StorageService {
  10. ///文件上传(UInt8List)
  11. Future<String?> uploadUint8List(Uint8List buffer, String name,
  12. [bool? isRechristen]) async {
  13. try {
  14. var nameInfos = name.split('.');
  15. final auth = await _getAuthAsync(
  16. fileName: nameInfos.last,
  17. isRechristen: isRechristen,
  18. );
  19. Map<String, String> params = {};
  20. params['Authorization'] = auth.authorization!;
  21. params['ContentType'] = auth.contentType!;
  22. final response = await http
  23. .put(
  24. Uri.parse(auth.storageUrl!),
  25. body: buffer,
  26. headers: params,
  27. )
  28. .timeout(
  29. const Duration(seconds: 30),
  30. );
  31. if (response.statusCode == 200) {
  32. return auth.storageUrl;
  33. }
  34. } catch (e) {
  35. await loggerAsyn(FISDeviceLogCategory.Error,
  36. 'StorageServiceExt uploadUint8List ex:$e');
  37. }
  38. return null;
  39. }
  40. ///鉴权 TODO: fileName 为空则接口报错,所以此处设置一个默认值
  41. Future<StorageServiceSettingDTO> _getAuthAsync({
  42. String? fileName,
  43. bool? isRechristen,
  44. List<DataItemDTO>? urlParams,
  45. List<DataItemDTO>? headerParams,
  46. String? requestMethod,
  47. }) async {
  48. try {
  49. final result =
  50. await jsonRpcProxy.storage.getAuthorizationAsync(FileServiceRequest(
  51. token: Storage.user.token,
  52. fileName: fileName ?? "dat",
  53. isRechristen: isRechristen ?? true,
  54. urlParams: urlParams,
  55. headerParams: headerParams,
  56. requestMethod: requestMethod,
  57. ));
  58. return result;
  59. } catch (e) {
  60. return StorageServiceSettingDTO();
  61. }
  62. }
  63. }