getPublicFileUrl method

Future<String?> getPublicFileUrl(
  1. String fileId
)

Implementation

Future<String?> getPublicFileUrl(String fileId) async {
  try {
    String fullPath = fileId;
    String folderPath = fullPath.substring(0, fullPath.lastIndexOf('/'));
    String fileName = fullPath.split('/').last;

    //파일 존재 여부
    final fileObjects = await Supabase.instance.client.storage
        .from(mainBucketId)
        .list(path: folderPath);

    if (fileObjects.isNotEmpty) {
      // ignore: unused_local_variable
      FileObject? fileObject = fileObjects.firstWhereOrNull(
        (file) => file.name == fileName,
      );
    } else {
      //print('file $fullPath does not exists !!');
      return null;
    }

    final publicUrl = Supabase.instance.client.storage
        .from(mainBucketId)
        .getPublicUrl(fullPath);

    //print(
    //    'publicUrl:$publicUrl'); //https://jaeumzhrdayuyqhemhyk.supabase.co/storage/v1/object/public/test_bucket/test_folder/test.jpg
    return publicUrl;
  } catch (e) {
    logger.severe('getPublicFileUrl error:$e');
    return null;
  }
}