Skip to content

Commit 33f75ca

Browse files
add delete() and getDownloadUrl() for StorageReference
1 parent 7a2ca5c commit 33f75ca

File tree

4 files changed

+41
-19
lines changed
  • firebase-storage/src
    • androidMain/kotlin/dev/gitlive/firebase/storage
    • commonMain/kotlin/dev/gitlive/firebase/storage
    • iosMain/kotlin/dev/gitlive/firebase/storage
    • jsMain/kotlin/dev/gitlive/firebase/storage

4 files changed

+41
-19
lines changed

firebase-storage/src/androidMain/kotlin/dev/gitlive/firebase/storage/storage.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package dev.gitlive.firebase.storage
88
import dev.gitlive.firebase.Firebase
99
import dev.gitlive.firebase.FirebaseApp
1010
import dev.gitlive.firebase.FirebaseException
11+
import kotlinx.coroutines.tasks.await
1112

1213
actual val Firebase.storage get() =
1314
FirebaseStorage(com.google.firebase.storage.FirebaseStorage.getInstance())
@@ -44,6 +45,8 @@ actual class StorageReference(val android: com.google.firebase.storage.StorageRe
4445
actual val storage: FirebaseStorage get() = FirebaseStorage(android.storage)
4546

4647
actual fun child(path: String): StorageReference = StorageReference(android.child(path))
48+
actual suspend fun delete() = android.delete().await().run { Unit }
49+
actual suspend fun getDownloadUrl(): String = android.downloadUrl.await().toString()
4750
}
4851

4952
actual open class StorageException(message: String) : FirebaseException(message)

firebase-storage/src/commonMain/kotlin/dev/gitlive/firebase/storage/storage.kt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,10 @@ expect class StorageReference {
2626
val parent: StorageReference?
2727
val root: StorageReference
2828
val storage: FirebaseStorage
29+
2930
fun child(path: String): StorageReference
30-
// fun delete(): Task<Unit>
31-
// fun downloadUrl(): Task<Uri>
32-
// fun getBytes(maxDownloadSizeBytes: Long): Task<ByteArray>
33-
// fun getMetadata(): Task<StorageMetadata>
34-
// fun list(options: ListOptions? = definedExternally): Task<ListResult>
35-
// fun listAll(): Task<ListResult>
36-
// fun putBytes(bytes: ByteArray, metadata: StorageMetadata? = definedExternally): UploadTask
37-
// fun putFile(file: Uri, metadata: StorageMetadata? = definedExternally): UploadTask
38-
// fun putFile(file: Uri, metadata: StorageMetadata? = definedExternally, existingUploadUri: Uri? = definedExternally): UploadTask
39-
// fun putFile(file: Uri, metadata: StorageMetadata? = definedExternally, existingUploadUri: Uri? = definedExternally, existingUploadHeaders: Map<String, String>? = definedExternally): UploadTask
40-
// fun putStream(stream: InputStream, metadata: StorageMetadata? = definedExternally): UploadTask
41-
// fun updateMetadata(metadata: StorageMetadata): Task<StorageMetadata>
42-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally): Flow<ByteReadPacket>
43-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.StreamProcessor): Flow<ByteReadPacket>
44-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.StreamProcessor, cancellationFlow: Flow<Unit>): Flow<ByteReadPacket>
45-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.StreamProcessor, cancellationFlow: Flow<Unit>, executor: Executor): Flow<ByteReadPacket>
46-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.StreamProcessor, cancellationFlow: Flow<Unit>, executor: Executor, bufferSize: Int): Flow<ByteReadPacket>
47-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.StreamProcessor, cancellationFlow: Flow<Unit>, executor: Executor, bufferSize: Int, chunkSize: Int): Flow<ByteReadPacket>
48-
// fun getStream(maxDownloadSizeBytes: Long = definedExternally, progressListener: StreamDownloadTask.Stream
31+
suspend fun delete()
32+
suspend fun getDownloadUrl(): String
4933
}
5034

5135
expect open class StorageException : FirebaseException

firebase-storage/src/iosMain/kotlin/dev/gitlive/firebase/storage/storage.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import cocoapods.FirebaseStorage.FIRStorageReference
99
import dev.gitlive.firebase.Firebase
1010
import dev.gitlive.firebase.FirebaseApp
1111
import dev.gitlive.firebase.FirebaseException
12+
import kotlinx.coroutines.CompletableDeferred
13+
import platform.Foundation.NSError
14+
import platform.Foundation.NSURL
1215

1316

1417
actual val Firebase.storage get() =
@@ -47,6 +50,35 @@ actual class StorageReference(val ios: FIRStorageReference) {
4750
actual val storage: FirebaseStorage get() = FirebaseStorage(ios.storage())
4851

4952
actual fun child(path: String): StorageReference = StorageReference(ios.child(path))
53+
actual suspend fun delete() = await { ios.deleteWithCompletion(it) }
54+
actual suspend fun getDownloadUrl(): String = awaitResult<NSURL?> { ios.downloadURLWithCompletion(it) }?.absoluteString!!
5055
}
5156

5257
actual open class StorageException(message: String) : FirebaseException(message)
58+
59+
suspend inline fun <reified T> awaitResult(function: (callback: (T?, NSError?) -> Unit) -> Unit): T {
60+
val job = CompletableDeferred<T?>()
61+
function { result, error ->
62+
if(error == null) {
63+
job.complete(result)
64+
} else {
65+
job.completeExceptionally(error.toException())
66+
}
67+
}
68+
return job.await() as T
69+
}
70+
71+
suspend inline fun <T> await(function: (callback: (NSError?) -> Unit) -> T): T {
72+
val job = CompletableDeferred<Unit>()
73+
val result = function { error ->
74+
if(error == null) {
75+
job.complete(Unit)
76+
} else {
77+
job.completeExceptionally(error.toException())
78+
}
79+
}
80+
job.await()
81+
return result
82+
}
83+
84+
fun NSError.toException() = StorageException(description!!) // TODO: Improve error handling

firebase-storage/src/jsMain/kotlin/dev/gitlive/firebase/storage/storage.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dev.gitlive.firebase.Firebase
88
import dev.gitlive.firebase.FirebaseApp
99
import dev.gitlive.firebase.FirebaseException
1010
import dev.gitlive.firebase.firebase
11+
import kotlinx.coroutines.await
1112

1213

1314
actual val Firebase.storage get() =
@@ -45,6 +46,8 @@ actual class StorageReference(val js: firebase.storage.Reference) {
4546
actual val storage: FirebaseStorage get() = FirebaseStorage(js.storage)
4647

4748
actual fun child(path: String): StorageReference = StorageReference(js.child(path))
49+
actual suspend fun delete() = rethrow { js.delete().await() }
50+
actual suspend fun getDownloadUrl(): String = rethrow { js.getDownloadURL().await().toString() }
4851
}
4952

5053
inline fun <T, R> T.rethrow(function: T.() -> R): R = dev.gitlive.firebase.storage.rethrow { function() }

0 commit comments

Comments
 (0)