-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from supabase-community/storage-metadata
Add support for file metadata, `info` and `exists`
- Loading branch information
1 parent
c84967a
commit 34ce185
Showing
5 changed files
with
230 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 25 additions & 1 deletion
26
Storage/src/commonMain/kotlin/io/github/jan/supabase/storage/UploadOptionBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
package io.github.jan.supabase.storage | ||
|
||
import io.github.jan.supabase.SupabaseSerializer | ||
import io.github.jan.supabase.encodeToJsonElement | ||
import io.github.jan.supabase.network.HttpRequestOverride | ||
import io.ktor.http.ContentType | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonObjectBuilder | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.jsonObject | ||
|
||
/** | ||
* Builder for uploading files with additional options | ||
* @param serializer The serializer to use for encoding the metadata | ||
* @param upsert Whether to update the file if it already exists | ||
* @param userMetadata The user metadata to upload with the file | ||
* @param contentType The content type of the file. If null, the content type will be inferred from the file extension | ||
*/ | ||
class UploadOptionBuilder( | ||
@PublishedApi internal val serializer: SupabaseSerializer, | ||
var upsert: Boolean = false, | ||
var userMetadata: JsonObject? = null, | ||
var contentType: ContentType? = null, | ||
internal val httpRequestOverrides: MutableList<HttpRequestOverride> = mutableListOf() | ||
) { | ||
|
||
/** | ||
* Overrides the HTTP request | ||
* Adds an [HttpRequestOverride] to the upload request | ||
* @param override The override to add | ||
*/ | ||
fun httpOverride(override: HttpRequestOverride) { | ||
httpRequestOverrides.add(override) | ||
} | ||
|
||
/** | ||
* Sets the user metadata to upload with the file | ||
* @param data The data to upload. Must be serializable by the [serializer] | ||
*/ | ||
inline fun <reified T : Any> userMetadata(data: T) { | ||
userMetadata = serializer.encodeToJsonElement(data).jsonObject | ||
} | ||
|
||
/** | ||
* Sets the user metadata to upload with the file | ||
* @param builder The builder for the metadata | ||
*/ | ||
inline fun userMetadata(builder: JsonObjectBuilder.() -> Unit) { | ||
userMetadata = buildJsonObject(builder) | ||
} | ||
|
||
} |
Oops, something went wrong.