From 871c7bff7f1cb0f3e44fa78dc64db7e89412a09b Mon Sep 17 00:00:00 2001 From: cotrin1208 Date: Mon, 25 Mar 2024 15:02:34 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Storage=E3=83=A2=E3=82=B8=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/jsMain/kotlin/FirebaseStorage.kt | 86 +++++++++++------- .../src/jsMain/kotlin/StorageError.kt | 19 ++++ .../src/jsMain/kotlin/StorageErrorCode.kt | 89 +++++++++++++++++++ .../src/jsMain/kotlin/StorageObserver.kt | 13 +++ 4 files changed, 173 insertions(+), 34 deletions(-) create mode 100644 firebase-storage/src/jsMain/kotlin/StorageError.kt create mode 100644 firebase-storage/src/jsMain/kotlin/StorageErrorCode.kt create mode 100644 firebase-storage/src/jsMain/kotlin/StorageObserver.kt diff --git a/firebase-storage/src/jsMain/kotlin/FirebaseStorage.kt b/firebase-storage/src/jsMain/kotlin/FirebaseStorage.kt index 005b8ea..6cdf3f7 100644 --- a/firebase-storage/src/jsMain/kotlin/FirebaseStorage.kt +++ b/firebase-storage/src/jsMain/kotlin/FirebaseStorage.kt @@ -5,9 +5,10 @@ package wrapper.firebase.storage import FirebaseApp +import org.khronos.webgl.ArrayBuffer +import org.khronos.webgl.Uint8Array +import org.w3c.files.Blob import org.w3c.files.File -import seskar.js.JsValue -import seskar.js.JsVirtual import kotlin.js.Promise external interface FirebaseStorage { @@ -18,6 +19,13 @@ external interface FirebaseStorage { external fun getStorage(app: FirebaseApp, bucketUrl: String?): FirebaseStorage +external fun connectStorageEmulator( + storage: FirebaseStorage, + host: String, + port: String, + options: dynamic = definedExternally, +) + external interface StorageReference { val bucket: String val fullPath: String @@ -27,53 +35,63 @@ external interface StorageReference { val storage: FirebaseStorage } -@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE") -@JsVirtual -sealed external interface StorageErrorCode { - companion object { - @JsValue("app-deleted") - val APP_DELETED: StorageErrorCode - - @JsValue("bucket-not-found") - val BUCKET_NOT_FOUND: StorageErrorCode - - @JsValue("canceled") - val CANCELED: StorageErrorCode - - @JsValue("cannot-slice-blob") - val CANNOT_SLICE_BLOB: StorageErrorCode - - @JsValue("internal-error") - val INTERNAL_ERROR: StorageErrorCode +external fun ref(storage: FirebaseStorage, url: String?): StorageReference - @JsValue("invalid-argument") - val INVALID_ARGUMENT: StorageErrorCode +external fun deleteObject(ref: StorageReference): Promise - @JsValue("invalid-argument-count") - val INVALID_ARGUMENT_COUNT: StorageErrorCode +external fun getDownloadURL(ref: StorageReference): Promise - } +external interface SettableMetadata { + var cacheControl: String? + var contentDisposition: String? + var contentEncoding: String? + var contentLanguage: String? + var contentType: String? + var customMetadata: dynamic } -external class StorageError( +external interface UploadMetadata : SettableMetadata { + var md5Hash: String? +} -) { - val customData: dynamic - val serverResponse: String? - val status: Number +external interface FullMetadata : UploadMetadata { + val bucket: String + val downloadTokens: Array + val fullPath: String + val generation: String + val metageneration: String + val name: String + val ref: StorageReference? + val size: Number + val timeCreated: String + val updated: String } -external fun ref(storage: FirebaseStorage, url: String?): StorageReference +external fun updateMetadata(ref: StorageReference, metadata: SettableMetadata): Promise -external fun deleteObject(ref: StorageReference): Promise +external fun uploadBytes( + ref: StorageReference, + data: Blob, + metadata: UploadMetadata? = definedExternally, +) -external fun getDownloadURL(ref: StorageReference): Promise +external fun uploadBytes( + ref: StorageReference, + data: Uint8Array, + metadata: UploadMetadata? = definedExternally, +) + +external fun uploadBytes( + ref: StorageReference, + data: ArrayBuffer, + metadata: UploadMetadata? = definedExternally, +) external interface UploadTask { val snapshot: dynamic fun cancel(): Boolean - fun catch(onRejected: (StorageError) -> dynamic): Promise + fun catch(onRejected: (StorageError) -> Any): Promise } external fun uploadBytes(ref: StorageReference, data: File) diff --git a/firebase-storage/src/jsMain/kotlin/StorageError.kt b/firebase-storage/src/jsMain/kotlin/StorageError.kt new file mode 100644 index 0000000..0ef3f2d --- /dev/null +++ b/firebase-storage/src/jsMain/kotlin/StorageError.kt @@ -0,0 +1,19 @@ +@file:JsModule("firebase/storage") +@file:JsNonModule +@file:Suppress("unused") + +package wrapper.firebase.storage + +external class StorageError( + code: StorageErrorCode, + message: String, + status: Number?, +) { + val customData: StorageErrorCustomData + val serverResponse: String? + val status: Number +} + +external interface StorageErrorCustomData { + val serverResponse: String? +} diff --git a/firebase-storage/src/jsMain/kotlin/StorageErrorCode.kt b/firebase-storage/src/jsMain/kotlin/StorageErrorCode.kt new file mode 100644 index 0000000..9e8efb1 --- /dev/null +++ b/firebase-storage/src/jsMain/kotlin/StorageErrorCode.kt @@ -0,0 +1,89 @@ +@file:JsModule("firebase/storage") +@file:JsNonModule +@file:Suppress("unused") + +package wrapper.firebase.storage + +import seskar.js.JsValue +import seskar.js.JsVirtual + +@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE") +@JsVirtual +sealed external interface StorageErrorCode { + companion object { + @JsValue("app-deleted") + val APP_DELETED: StorageErrorCode + + @JsValue("bucket-not-found") + val BUCKET_NOT_FOUND: StorageErrorCode + + @JsValue("canceled") + val CANCELED: StorageErrorCode + + @JsValue("cannot-slice-blob") + val CANNOT_SLICE_BLOB: StorageErrorCode + + @JsValue("internal-error") + val INTERNAL_ERROR: StorageErrorCode + + @JsValue("invalid-argument") + val INVALID_ARGUMENT: StorageErrorCode + + @JsValue("invalid-argument-count") + val INVALID_ARGUMENT_COUNT: StorageErrorCode + + @JsValue("invalid-checksum") + val INVALID_CHECKSUM: StorageErrorCode + + @JsValue("invalid-default-bucket") + val INVALID_DEFAULT_BUCKET: StorageErrorCode + + @JsValue("invalid-event-name") + val INVALID_EVENT_NAME: StorageErrorCode + + @JsValue("invalid-format") + val INVALID_FORMAT: StorageErrorCode + + @JsValue("invalid-root-operation") + val INVALID_ROOT_OPERATION: StorageErrorCode + + @JsValue("invalid-url") + val INVALID_URL: StorageErrorCode + + @JsValue("no-default-bucket") + val NO_DEFAULT_BUCKET: StorageErrorCode + + @JsValue("no-download-url") + val NO_DOWNLOAD_URL: StorageErrorCode + + @JsValue("object-not-found") + val OBJECT_NOT_FOUND:StorageErrorCode + + @JsValue("project-not-found") + val PROJECT_NOT_FOUND: StorageErrorCode + + @JsValue("quota-exceeded") + val QUOTA_EXCEEDED: StorageErrorCode + + @JsValue("retry-limit-exceeded") + val RETRY_LIMIT_EXCEEDED: StorageErrorCode + + @JsValue("server-file-wrong-size") + val SERVER_FILE_WRONG_SIZE: StorageErrorCode + + @JsValue("unauthenticated") + val UNAUTHENTICATED: StorageErrorCode + + @JsValue("unauthorized") + val UNAUTHORIZED: StorageErrorCode + + @JsValue("unauthorized-app") + val UNAUTHORIZED_APP: StorageErrorCode + + @JsValue("unknown") + val UNKNOWN: StorageErrorCode + + @JsValue("unsupported-environment") + val UNSUPPORTED_ENVIRONMENT: StorageErrorCode + } +} diff --git a/firebase-storage/src/jsMain/kotlin/StorageObserver.kt b/firebase-storage/src/jsMain/kotlin/StorageObserver.kt new file mode 100644 index 0000000..bfabec4 --- /dev/null +++ b/firebase-storage/src/jsMain/kotlin/StorageObserver.kt @@ -0,0 +1,13 @@ +@file:Suppress("unused") + +package wrapper.firebase.storage + +import CompleteFn +import ErrorFn +import NextFn + +interface StorageObserver { + val complete: CompleteFn? + val error: ErrorFn? + val next: NextFn? +}