Skip to content

Commit

Permalink
Fixed Storage for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Jun 25, 2024
1 parent 9639a5d commit a85efaa
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ actual class DocumentChange(val js: JsDocumentChange) {
actual val oldIndex: Int
get() = js.oldIndex
actual val type: ChangeType
get() = ChangeType.values().first { it.jsString == js.type }
get() = ChangeType.entries.first { it.jsString == js.type }
}

actual data class NativeDocumentSnapshot(val js: JsDocumentSnapshot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class FirebaseStorageTest {
}

@Test
fun testStorageNotNull() {
fun testStorageNotNull() = runTest {
assertNotNull(storage)
}

Expand All @@ -79,7 +79,7 @@ class FirebaseStorageTest {

assertNotNull(metadataResult)
assertNotNull(metadataResult.contentType)
assertEquals(metadataResult.contentType, metadata.contentType)
assertEquals(metadata.contentType, metadataResult.contentType)
}

@Test
Expand All @@ -95,7 +95,7 @@ class FirebaseStorageTest {
val metadataResult = ref.getMetadata()

assertNotNull(metadataResult)
assertEquals(metadataResult.customMetadata["key"], metadata.customMetadata["key"])
assertEquals( metadata.customMetadata["key"], metadataResult.customMetadata["key"])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@
package dev.gitlive.firebase.storage.externals

import dev.gitlive.firebase.externals.FirebaseApp
import kotlin.js.Json
import kotlin.js.Promise
import kotlin.js.collections.JsMap

external fun getStorage(app: FirebaseApp? = definedExternally, url: String): FirebaseStorage
external fun getStorage(app: FirebaseApp? = definedExternally, bucketUrl: String): FirebaseStorage

external fun getStorage(app: FirebaseApp? = definedExternally): FirebaseStorage

external fun ref(storage: FirebaseStorage, url: String? = definedExternally): StorageReference
external fun ref(ref: StorageReference, url: String? = definedExternally): StorageReference
external fun ref(ref: StorageReference, path: String? = definedExternally): StorageReference

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

external fun getMetadata(ref: StorageReference): Promise<StorageMetadata>
external fun getMetadata(ref: StorageReference): Promise<FullMetadata>
external fun updateMetadata(ref: StorageReference, metadata: SettableMetadata): Promise<FullMetadata>

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

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

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

external fun listAll(ref: StorageReference): Promise<ListResult>;
external fun list(ref: StorageReference, options: ListOptions?): Promise<ListResult>
external fun listAll(ref: StorageReference): Promise<ListResult>

external fun connectStorageEmulator(
storage: FirebaseStorage,
Expand All @@ -46,45 +49,63 @@ external interface StorageReference {
val storage: FirebaseStorage
}

external open class ListResult {
external interface ListOptions {
val maxResults: Double?
val pageToken: String?
}

external interface ListResult {
val items: Array<StorageReference>
val nextPageToken: String
val prefixes: Array<StorageReference>
}

external interface StorageError

external interface UploadTaskSnapshot {
val bytesTransferred: Number
val ref: StorageReference
val state: String
val task: UploadTask
val totalBytes: Number
external interface SettableMetadata {
val cacheControl: String?
val contentDisposition: String?
val contentEncoding: String?
val contentLanguage: String?
val contentType: String?
val customMetadata: Json?
}

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?
external interface UploadMetadata : SettableMetadata {
val md5Hash: String?
val metageneration: String?
val name: String?
val size: Number?
val timeCreated: String?
val updated: String?
}

external interface FullMetadata : UploadMetadata {
val bucket: String
val downloadTokens: Array<String>?
val fullPath: String
val generation: String
val metageneration: String
val name: String
val ref: StorageReference?
val size: Double
val timeCreated: String
val updated: String
}

external class UploadTask : Promise<UploadTaskSnapshot> {
external interface UploadResult {
val metadata: FullMetadata
val ref: StorageReference
}

external interface UploadTask {
fun cancel(): Boolean;
fun on(event: String, next: (snapshot: UploadTaskSnapshot) -> Unit, error: (a: StorageError) -> Unit, complete: () -> Unit): () -> Unit
fun pause(): Boolean;
fun resume(): Boolean;
fun then(onFulfilled: ((UploadTaskSnapshot) -> Unit)?, onRejected: ((StorageError) -> Unit)?): Promise<Unit>
val snapshot: UploadTaskSnapshot
}

external interface UploadTaskSnapshot {
val bytesTransferred: Double
val ref: StorageReference
val state: String
val task: UploadTask
val totalBytes: Double
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.emitAll
import kotlin.js.Json
import kotlin.js.json

actual val Firebase.storage
get() = FirebaseStorage(getStorage())
Expand Down Expand Up @@ -134,7 +136,7 @@ internal fun errorToException(error: dynamic) = (error?.code ?: error?.message ?
}


internal fun StorageMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadata {
internal fun UploadMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadata {
val sdkMetadata = this
return storageMetadata {
md5Hash = sdkMetadata.md5Hash
Expand All @@ -143,19 +145,21 @@ internal fun StorageMetadata.toFirebaseStorageMetadata(): FirebaseStorageMetadat
contentEncoding = sdkMetadata.contentEncoding
contentLanguage = sdkMetadata.contentLanguage
contentType = sdkMetadata.contentType
sdkMetadata.customMetadata?.entries?.forEach {
setCustomMetadata(it.key, it.value)
}
customMetadata = sdkMetadata.customMetadata?.let { metadata ->
val objectKeys = js("Object.keys")
objectKeys(metadata).unsafeCast<Array<String>>().associateWith { key ->
metadata[key]?.toString().orEmpty()
}
}.orEmpty().toMutableMap()
}
}

internal 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
}
internal fun FirebaseStorageMetadata.toStorageMetadata(): Json = json(
"cacheControl" to cacheControl,
"contentDisposition" to contentDisposition,
"contentEncoding" to contentEncoding,
"contentLanguage" to contentLanguage,
"contentType" to contentType,
"customMetadata" to json(*customMetadata.toList().toTypedArray()),
"md5Hash" to md5Hash
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dev.gitlive.firebase.storage

actual fun createTestData(): Data {
TODO("Not yet implemented")
}
import org.khronos.webgl.Uint8Array

actual fun createTestData(): Data = Data(Uint8Array("test".encodeToByteArray().toTypedArray()))

0 comments on commit a85efaa

Please sign in to comment.