uploadFile method

Future<String?> uploadFile(
  1. String file,
  2. Uint8List data, {
  3. bool? public,
})

Implementation

Future<String?> uploadFile(
  String file,
  Uint8List data, {
  bool? public,
}) async {
  String? url, publicUrl;
  Map<String, String> headers = {};

  if (public == null || public == false) {
    url = await _getUploadLink(file);
  } else {
    InstanceUploadLinkResponse? resp =
        await _getInstanceUploadLink(file, public);

    url = resp?.url;
    publicUrl = resp?.publicUrl;
  }

  if (url == null) {
    throw Exception("url not found");
  }

  if (public != true) {
    data = await this.encrypt!(data, aesKey.bytes);
  }

  if (public == true) {
    headers["x-amz-acl"] = "public-read";
  }

  Uri uri = Uri.parse(url);
  await html.HttpRequest.request(
    uri.toString(),
    method: "PUT",
    mimeType: "application/octet-stream",
    responseType: "json",
    sendData: data,
    requestHeaders: headers,
  );

  return publicUrl;
}