From fd43251063b49c15258356029588f00ba4e06867 Mon Sep 17 00:00:00 2001 From: Sneh Mehta Date: Fri, 11 Oct 2024 15:36:51 +0530 Subject: [PATCH 1/2] camera blob default value, uploadFiles support multiple file --- .../ensemble/lib/action/upload_files_action.dart | 15 ++++++++++++++- modules/ensemble/lib/util/upload_utils.dart | 12 ++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/modules/ensemble/lib/action/upload_files_action.dart b/modules/ensemble/lib/action/upload_files_action.dart index d80edaf52..298bb60c1 100644 --- a/modules/ensemble/lib/action/upload_files_action.dart +++ b/modules/ensemble/lib/action/upload_files_action.dart @@ -198,9 +198,22 @@ List? _getRawFiles(dynamic rawFiles, DataContext dataContext) { }); return processedFile; } + + if (files.values.length > 0 && files.values.first is List) { + List processedFile = []; + files.forEach((key, value) { + (value as List).forEach((element) { + if (element is Map && element.containsKey('path')) { + element['fieldName'] = key; + processedFile.add(File.fromJson(element)); + } + }); + }); + return processedFile; + } } - if (files is String) { + if (files is String && files.isNotEmpty) { final rawFiles = File.fromString(files); return [rawFiles]; } diff --git a/modules/ensemble/lib/util/upload_utils.dart b/modules/ensemble/lib/util/upload_utils.dart index 0fbdcc6a6..874577566 100644 --- a/modules/ensemble/lib/util/upload_utils.dart +++ b/modules/ensemble/lib/util/upload_utils.dart @@ -60,10 +60,13 @@ class UploadUtils { lookupMimeType(file.path ?? '', headerBytes: file.bytes) ?? 'application/octet-stream'; if (file.bytes != null) { - multipartFile = http.MultipartFile.fromBytes(file.fieldName ?? fieldName, file.bytes!, - filename: file.name, contentType: MediaType.parse(mimeType)); + multipartFile = http.MultipartFile.fromBytes( + file.fieldName ?? fieldName, file.bytes!, + filename: file.name?.isNotEmpty ?? false ? file.name : 'data', + contentType: MediaType.parse(mimeType)); } else if (file.path != null) { - multipartFile = await http.MultipartFile.fromPath(file.fieldName ?? fieldName, file.path!, + multipartFile = await http.MultipartFile.fromPath( + file.fieldName ?? fieldName, file.path!, filename: file.name, contentType: MediaType.parse(mimeType)); } else { debugPrint('Failed to add ${file.name} ${file.ext} ${file.path}'); @@ -83,7 +86,8 @@ class UploadUtils { if (res.statusCode >= 200 && res.statusCode <= 300) { return HttpResponse(res, APIState.success); } else { - throw Exception('uploadFile: Failed to upload files \nserver response:\n${res.body}'); + throw Exception( + 'uploadFile: Failed to upload files \nserver response:\n${res.body}'); } } catch (error) { onError?.call(error); From ebc22b118ec4cf7ef08a8521dda9b3afae919d5c Mon Sep 17 00:00:00 2001 From: Sneh Mehta Date: Fri, 11 Oct 2024 21:20:56 +0530 Subject: [PATCH 2/2] generate file name for blob --- modules/ensemble/lib/util/upload_utils.dart | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/ensemble/lib/util/upload_utils.dart b/modules/ensemble/lib/util/upload_utils.dart index 874577566..7b43ab988 100644 --- a/modules/ensemble/lib/util/upload_utils.dart +++ b/modules/ensemble/lib/util/upload_utils.dart @@ -60,10 +60,13 @@ class UploadUtils { lookupMimeType(file.path ?? '', headerBytes: file.bytes) ?? 'application/octet-stream'; if (file.bytes != null) { + final mediaType = MediaType.parse(mimeType); + final filename = file.name?.isNotEmpty ?? false + ? file.name + : generateFileName(mediaType); multipartFile = http.MultipartFile.fromBytes( file.fieldName ?? fieldName, file.bytes!, - filename: file.name?.isNotEmpty ?? false ? file.name : 'data', - contentType: MediaType.parse(mimeType)); + filename: filename, contentType: mediaType); } else if (file.path != null) { multipartFile = await http.MultipartFile.fromPath( file.fieldName ?? fieldName, file.path!, @@ -94,6 +97,11 @@ class UploadUtils { } return null; } + + static generateFileName(MediaType mediaType) { + final timestamp = DateTime.now().millisecondsSinceEpoch; + return '${mediaType.type}_$timestamp.${mediaType.subtype}'; + } } class MultipartRequest extends http.MultipartRequest {