uploadFile method

Future<String> uploadFile(
  1. XFile file,
  2. String filePath,
  3. ImageType type
)

Uploads an image file to a specific path.

The file is the image file that will be uploaded. The filePath is the path where the file will be uploaded. The type is the type of image that is being uploaded.

Returns the download URL of the uploaded file. Because of the image resizing extension configured in Firebase Storage, the URL will be formatted to include the size of the image.

Implementation

Future<String> uploadFile(XFile file, String filePath, ImageType type) async {
  final fileExtension = filePath.split('.').last;
  final ref = instance.ref();
  final url = kIsWeb
      ? await ref
          .child(filePath)
          .putData(
            await file.readAsBytes(),
            SettableMetadata(contentType: "image/$fileExtension"),
          )
          .then((value) => value.ref.getDownloadURL())
      : await ref
          .child(filePath)
          .putFile(
            File(file.path),
            SettableMetadata(contentType: "image/$fileExtension"),
          )
          .then((value) => value.ref.getDownloadURL());

  return switch (type) {
    ImageType.banner => _sizePlacer(url, "_1200x675"),
    ImageType.profileOriginal => _sizePlacer(url, "_400x400"),
    ImageType.profileThumbnail => _sizePlacer(url, "_200x200"),
    ImageType.postImage => _sizePlacer(url, "_1200x1200"),
  };
}