uploadFiles method
Uploads files to the backend matching the TypeScript API.
This method closely mirrors the TypeScript uploadFiles API, providing
a more flexible interface for file uploads.
files is a list of FileData objects to upload.
metadataList is an optional list of metadata for each file. If provided,
must match the order and length of files.
bucketId specifies the target bucket for the upload.
Returns a list of FileMetadata for the successfully uploaded files.
Throws an ApiException if the upload fails.
Implementation
Future<List<FileMetadata>> uploadFiles({
required List<FileData> files,
String? bucketId,
List<UploadFileMetadata>? metadataList,
UploadProgressCallback? onUploadProgress,
}) async {
if (metadataList != null && metadataList.length != files.length) {
throw ArgumentError(
'metadataList length (${metadataList.length}) must match files length (${files.length})',
);
}
final fields = <String, String>{};
final multipartFiles = <http.MultipartFile>[];
// Add bucket-id if present
if (bucketId != null) {
fields['bucket-id'] = bucketId;
}
// Add metadata[] if present
if (metadataList != null) {
for (final metadata in metadataList) {
multipartFiles.add(
http.MultipartFile.fromBytes(
'metadata[]',
utf8.encode(jsonEncode(metadata.toJson())),
filename: '',
contentType: MediaType('application', 'json'),
),
);
}
}
// Add file[] if present
for (final file in files) {
final contentType = MediaType.parse(
file.contentType ?? applicationOctetStreamType,
);
multipartFiles.add(
http.MultipartFile.fromBytes(
'file[]',
file.bytes,
filename: file.filename ?? '',
contentType: contentType,
),
);
}
final response = await _apiClient.postMultipart(
'/files',
files: multipartFiles,
fields: fields,
headers: _session.authenticationHeaders,
responseDeserializer: UploadFilesResponse.fromJson,
onUploadProgress: onUploadProgress,
);
return response.processedFiles;
}