Skip to content

Commit

Permalink
Merge branch 'main' into enable-modules-with-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sharjeelyunus authored Oct 12, 2024
2 parents c922bff + be16aed commit 281df3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 14 additions & 1 deletion modules/ensemble/lib/action/upload_files_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,22 @@ List<File>? _getRawFiles(dynamic rawFiles, DataContext dataContext) {
});
return processedFile;
}

if (files.values.length > 0 && files.values.first is List) {
List<File> 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];
}
Expand Down
20 changes: 16 additions & 4 deletions modules/ensemble/lib/util/upload_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ 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));
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: filename, contentType: mediaType);
} 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}');
Expand All @@ -83,13 +89,19 @@ 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);
}
return null;
}

static generateFileName(MediaType mediaType) {
final timestamp = DateTime.now().millisecondsSinceEpoch;
return '${mediaType.type}_$timestamp.${mediaType.subtype}';
}
}

class MultipartRequest extends http.MultipartRequest {
Expand Down

0 comments on commit 281df3d

Please sign in to comment.