1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'dart:typed_data';
- import 'package:flyinsonolite/jsonrpc/fisLib/services/index.dart';
- import 'package:flyinsonolite/infrastructure/logger.dart';
- import 'package:http/http.dart' as http;
- import 'package:fis_jsonrpc/rpc.dart';
- import 'package:flyinsonolite/jsonrpc/jsonrpcclient.dart';
- import 'package:flyinsonolite/infrastructure/storage.dart';
- ///存储服务扩展类
- extension StorageServiceExt on StorageService {
- ///文件上传(UInt8List)
- Future<String?> uploadUint8List(Uint8List buffer, String name,
- [bool? isRechristen]) async {
- try {
- var nameInfos = name.split('.');
- final auth = await _getAuthAsync(
- fileName: nameInfos.last,
- isRechristen: isRechristen,
- );
- Map<String, String> params = {};
- params['Authorization'] = auth.authorization!;
- params['ContentType'] = auth.contentType!;
- final response = await http
- .put(
- Uri.parse(auth.storageUrl!),
- body: buffer,
- headers: params,
- )
- .timeout(
- const Duration(seconds: 30),
- );
- if (response.statusCode == 200) {
- return auth.storageUrl;
- }
- } catch (e) {
- await loggerAsyn(FISDeviceLogCategory.Error,
- 'StorageServiceExt uploadUint8List ex:$e');
- }
- return null;
- }
- ///鉴权 TODO: fileName 为空则接口报错,所以此处设置一个默认值
- Future<StorageServiceSettingDTO> _getAuthAsync({
- String? fileName,
- bool? isRechristen,
- List<DataItemDTO>? urlParams,
- List<DataItemDTO>? headerParams,
- String? requestMethod,
- }) async {
- try {
- final result =
- await jsonRpcProxy.storage.getAuthorizationAsync(FileServiceRequest(
- token: Storage.user.token,
- fileName: fileName ?? "dat",
- isRechristen: isRechristen ?? true,
- urlParams: urlParams,
- headerParams: headerParams,
- requestMethod: requestMethod,
- ));
- return result;
- } catch (e) {
- return StorageServiceSettingDTO();
- }
- }
- }
|