suspend fun StorageReference.UploadFlow( context: Context? = null, path: String?,…
- User
suspend fun StorageReference.UploadFlow( context: Context? = null, path: String?, metaData: String = "image/jpeg", ): Flow = channelFlow { if (path.isNullOrEmpty()) { Log.d("StorageMaster", "1") send(StorageUploadEvents.StorageUploadFailed("Path is empty or null")) return@channelFlow } val meta = storageMetadata { contentType = metaData } val uri = Uri.parse(path) val inputStream = try { when { uri.scheme.equals( "content", ignoreCase = true ) -> context?.contentResolver?.openInputStream(uri)
uri.scheme.equals( "file", ignoreCase = true ) || uri.scheme == null -> FileInputStream(uri.path) else -> { send(StorageUploadEvents.StorageUploadFailed("Invalid Uri")) return@channelFlow } } } catch (e: FileNotFoundException) { e.printStackTrace() send(StorageUploadEvents.StorageUploadFailed("File not found: ${e.message}")) return@channelFlow } inputStream ?: run { send(StorageUploadEvents.StorageUploadFailed("Could not open file input stream")) return@channelFlow } val file = File(context?.cacheDir, uri.lastPathSegment ?: "temp_file") file.outputStream().use { inputStream.copyTo(it) } val compressedFile = coroutineScope { context ?: return@coroutineScope file val isImage = metaData.contains("image") if (!isImage) return@coroutineScope file Compressor.compress(context, file) { this.quality(50) } } //if the file does not exist, return a failure event if (compressedFile.exists().not()) { Log.d("StorageMaster", "3") send(StorageUploadEvents.StorageUploadFailed("File not found")) return@channelFlow } // Start the upload task val inputs = FileInputStream(compressedFile) val uploadTask = putStream(inputs, meta) // Use the helper function to listen to upload events uploadTask.UploadAsyncFlow(this@UploadFlow).collect { event -> Log.d("StorageMaster", "4") send(event) // Re-emit the events from the helper function } Log.d("StorageMaster", "5") }when I pass the Uri that I got from: val audioPickerLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.GetContent() ) { uri: Uri? -> uri ?: return@rememberLauncherForActivityResult //if file size is greater than 10mb return if (getFileSize(uri) > 10 * 1024 * 1024) { Toast.makeText(this, "File size is too large", Toast.LENGTH_SHORT).show() return@rememberLauncherForActivityResult } //if file length is not between 3 seconds and 3 minutes return if (getFileLength(uri) < 3000 || getFileLength(uri) > 180000) { Toast.makeText( this, "File length should be between 3 seconds and 3 minutes", Toast.LENGTH_SHORT ).show() return@rememberLauncherForActivityResult } events.tryEmit(MusicPickerContract.MusicPickEvent.MusicPicked(uri)) // Set the selected audio URI }
- Assistant