fileup method

Future<String> fileup(
  1. Uint8List file, {
  2. String? filename,
  3. String? bucket,
  4. String? ext,
  5. StreamUploadProgress? onprogress,
  6. Uri? endpoint,
})

Implementation

Future<String> fileup(
  Uint8List file, {
  String? filename,
  String? bucket,
  String? ext,
  StreamUploadProgress? onprogress,
  Uri? endpoint,
}) async {
  final suffix = switch (ext) {
    null || '' => '',
    _ when ext.startsWith('.') => ext,
    _ => '.$ext',
  };
  final uri = endpoint ?? this.endpoint ?? Uri.base.resolve('mpc/w');
  final request = MultipartRequest('POST', uri);
  if (bucket?.isNotEmpty ?? false) {
    request.headers['mesh-bucket'] = bucket!;
  }
  request.files.add(
    MultipartFile(
      'f',
      _track(file, onprogress),
      file.lengthInBytes,
      filename:
          filename ?? 'mesh_${DateTime.now().millisecondsSinceEpoch}$suffix',
    ),
  );
  final response = await _client.send(request);
  final body = await response.stream.bytesToString();
  if (response.statusCode >= 300) {
    throw Exception('HTTP ${response.statusCode}: $body');
  }
  return body.isEmpty ? body : body.substring(1);
}