Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add getter for metadata
  • Loading branch information
Bas Buijsen committed Jun 3, 2024
commit 17f1ba730aa9f609f9413f3a5cb78b5697748aaf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.gitlive.firebase.storage
import android.net.Uri
import com.google.android.gms.tasks.OnCanceledListener
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.storage.OnPausedListener
import com.google.firebase.storage.OnProgressListener
import com.google.firebase.storage.StorageMetadata
Expand Down Expand Up @@ -58,6 +59,8 @@ actual class StorageReference(val android: com.google.firebase.storage.StorageRe
actual val root: StorageReference get() = StorageReference(android.root)
actual val storage: FirebaseStorage get() = FirebaseStorage(android.storage)

actual suspend fun getMetadata(): FirebaseStorageMetadata? = android.metadata.await().toFirebaseStorageMetadata()

actual fun child(path: String): StorageReference = StorageReference(android.child(path))

actual suspend fun delete() = android.delete().await().run { Unit }
Expand Down Expand Up @@ -117,7 +120,6 @@ actual class File(val uri: Uri)

actual typealias FirebaseStorageException = com.google.firebase.storage.StorageException


fun FirebaseStorageMetadata.toStorageMetadata(): StorageMetadata {
return StorageMetadata.Builder()
.setCacheControl(this.cacheControl)
Expand All @@ -130,4 +132,19 @@ fun FirebaseStorageMetadata.toStorageMetadata(): StorageMetadata {
(key, value) -> setCustomMetadata(key, value)
}
}.build()
}

fun StorageMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadata {
val sdkMetadata = this
return storageMetadata {
md5Hash = sdkMetadata.md5Hash
cacheControl = sdkMetadata.cacheControl
contentDisposition = sdkMetadata.contentDisposition
contentEncoding = sdkMetadata.contentEncoding
contentLanguage = sdkMetadata.contentLanguage
contentType = sdkMetadata.contentType
sdkMetadata.customMetadataKeys.forEach {
setCustomMetadata(it, sdkMetadata.getCustomMetadata(it))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ expect class StorageReference {
val root: StorageReference
val storage: FirebaseStorage

suspend fun getMetadata(): FirebaseStorageMetadata?

fun child(path: String): StorageReference

suspend fun delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ actual class StorageReference(val ios: FIRStorageReference) {

actual fun child(path: String): StorageReference = StorageReference(ios.child(path))

actual suspend fun getMetadata(): FirebaseStorageMetadata? = ios.awaitResult {
metadataWithCompletion { metadata, error ->
if (error == null) {
it.invoke(metadata?.toFirebaseStorageMetadata(), null)
} else {
it.invoke(null, error)
}
}
}

actual suspend fun delete() = await { ios.deleteWithCompletion(it) }

actual suspend fun getDownloadUrl(): String = ios.awaitResult {
Expand Down Expand Up @@ -161,4 +171,19 @@ fun FirebaseStorageMetadata.toFIRMetadata(): FIRStorageMetadata {
metadata.setContentLanguage(this.contentLanguage)
metadata.setContentType(this.contentType)
return metadata
}

fun FIRStorageMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadata {
val sdkMetadata = this
return storageMetadata {
md5Hash = sdkMetadata.md5Hash()
cacheControl = sdkMetadata.cacheControl()
contentDisposition = sdkMetadata.contentDisposition()
contentEncoding = sdkMetadata.contentEncoding()
contentLanguage = sdkMetadata.contentLanguage()
contentType = sdkMetadata.contentType()
sdkMetadata.customMetadata()?.forEach {
setCustomMetadata(it.key.toString(), it.value.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package dev.gitlive.firebase.storage.externals

import dev.gitlive.firebase.externals.FirebaseApp
import dev.gitlive.firebase.storage.FirebaseStorageMetadata
import kotlin.js.Promise

external fun getStorage(app: FirebaseApp? = definedExternally): FirebaseStorage
Expand All @@ -14,9 +13,11 @@ external fun ref(ref: StorageReference, url: String? = definedExternally): Stora

external fun getDownloadURL(ref: StorageReference): Promise<String>

external fun uploadBytes(ref: StorageReference, file: dynamic, metadata: FirebaseStorageMetadata?): Promise<Unit>
external fun getMetadata(ref: StorageReference): Promise<StorageMetadata>

external fun uploadBytesResumable(ref: StorageReference, data: dynamic, metadata: FirebaseStorageMetadata?): UploadTask
external fun uploadBytes(ref: StorageReference, file: dynamic, metadata: StorageMetadata?): Promise<Unit>

external fun uploadBytesResumable(ref: StorageReference, data: dynamic, metadata: StorageMetadata?): UploadTask

external fun deleteObject(ref: StorageReference): Promise<Unit>;

Expand Down Expand Up @@ -59,6 +60,25 @@ external interface UploadTaskSnapshot {
val totalBytes: Number
}

external class StorageMetadata {
val bucket: String?
var cacheControl: String?
var contentDisposition: String?
var contentEncoding: String?
var contentLanguage: String?
var contentType: String?
var customMetadata: Map<String, String>?
val fullPath: String?
val generation: String?
val md5Hash: String?
val metageneration: String?
val name: String?
val size: Number?
val timeCreated: String?
val updated: String?

}

external class UploadTask : Promise<UploadTaskSnapshot> {
fun cancel(): Boolean;
fun on(event: String, next: (snapshot: UploadTaskSnapshot) -> Unit, error: (a: StorageError) -> Unit, complete: () -> Unit): () -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ actual class StorageReference(val js: dev.gitlive.firebase.storage.externals.Sto
actual val root: StorageReference get() = StorageReference(js.root)
actual val storage: FirebaseStorage get() = FirebaseStorage(js.storage)

actual suspend fun getMetadata(): FirebaseStorageMetadata? = rethrow { getMetadata(js).await().toFirebaseStorageMetadata() }

actual fun child(path: String): StorageReference = StorageReference(ref(js, path))

actual suspend fun delete() = rethrow { deleteObject(js).await() }
Expand All @@ -59,10 +61,10 @@ actual class StorageReference(val js: dev.gitlive.firebase.storage.externals.Sto

actual suspend fun listAll(): ListResult = rethrow { ListResult(listAll(js).await()) }

actual suspend fun putFile(file: File, metadata: FirebaseStorageMetadata?): Unit = rethrow { uploadBytes(js, file, metadata).await() }
actual suspend fun putFile(file: File, metadata: FirebaseStorageMetadata?): Unit = rethrow { uploadBytes(js, file, metadata?.toStorageMetadata()).await() }

actual fun putFileResumable(file: File, metadata: FirebaseStorageMetadata?): ProgressFlow = rethrow {
val uploadTask = uploadBytesResumable(js, file, metadata)
val uploadTask = uploadBytesResumable(js, file, metadata?.toStorageMetadata())

val flow = callbackFlow {
val unsubscribe = uploadTask.on(
Expand Down Expand Up @@ -123,4 +125,31 @@ internal fun errorToException(error: dynamic) = (error?.code ?: error?.message ?
FirebaseStorageException(code, error)
}
}
}
}


fun StorageMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadata {
val sdkMetadata = this
return storageMetadata {
md5Hash = sdkMetadata.md5Hash
cacheControl = sdkMetadata.cacheControl
contentDisposition = sdkMetadata.contentDisposition
contentEncoding = sdkMetadata.contentEncoding
contentLanguage = sdkMetadata.contentLanguage
contentType = sdkMetadata.contentType
sdkMetadata.customMetadata?.entries?.forEach {
setCustomMetadata(it.key, it.value)
}
}
}

fun FirebaseStorageMetadata.toStorageMetadata(): StorageMetadata {
val metadata = StorageMetadata()
metadata.cacheControl = cacheControl
metadata.contentDisposition = contentDisposition
metadata.contentEncoding = contentEncoding
metadata.contentLanguage = contentLanguage
metadata.contentType = contentType
metadata.customMetadata = customMetadata
return metadata
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ actual class StorageReference {
actual val storage: FirebaseStorage
get() = TODO("Not yet implemented")

actual suspend fun getMetadata(): FirebaseStorageMetadata? {
TODO("Not yet implemented")
}

actual fun child(path: String): StorageReference {
TODO("Not yet implemented")
}
Expand Down