-
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for file metadata, info
and exists
#694
Open
jan-tennert
wants to merge
14
commits into
master
Choose a base branch
from
storage-metadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e3dfbb4
Add support for file metadata, `info` and `exists`
jan-tennert a4c07b8
Merge branch 'master' into storage-metadata
jan-tennert 1d272d8
Finish up `info` method and rename `BucketItem` to `FileObject`
jan-tennert d928abe
Add some missing docs and tests
jan-tennert 1f48846
fix docs
jan-tennert 88ceb2f
suppress warning
jan-tennert 4ca681d
suppress warning for signed urls
jan-tennert b442961
Move upsert parameter to new FileOptionBuilder
jan-tennert 98556c9
remove comma
jan-tennert a97dce6
remove println
jan-tennert f1d516d
Merge branch 'master' into storage-metadata
jan-tennert 44687c8
Fix tests
jan-tennert 91b8b9b
Remove invalid builder
jan-tennert f716672
Merge branch 'master' into storage-metadata
jan-tennert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package io.github.jan.supabase.storage | ||
|
||
import io.github.jan.supabase.exceptions.RestException | ||
import io.github.jan.supabase.putJsonObject | ||
import io.github.jan.supabase.safeBody | ||
import io.github.jan.supabase.storage.BucketApi.Companion.UPSERT_HEADER | ||
|
@@ -15,6 +16,7 @@ import io.ktor.client.statement.bodyAsChannel | |
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpHeaders | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.Url | ||
import io.ktor.http.content.OutgoingContent | ||
import io.ktor.http.defaultForFilePath | ||
|
@@ -29,6 +31,8 @@ import kotlinx.serialization.json.jsonPrimitive | |
import kotlinx.serialization.json.put | ||
import kotlinx.serialization.json.putJsonArray | ||
import kotlinx.serialization.json.putJsonObject | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
import kotlin.time.Duration | ||
|
||
internal class BucketApiImpl(override val bucketId: String, val storage: StorageImpl, resumableCache: ResumableCache) : BucketApi { | ||
|
@@ -37,18 +41,22 @@ internal class BucketApiImpl(override val bucketId: String, val storage: Storage | |
|
||
override val resumable = ResumableClientImpl(this, resumableCache) | ||
|
||
override suspend fun update(path: String, data: UploadData, upsert: Boolean): FileUploadResponse = | ||
override suspend fun update( | ||
path: String, | ||
data: UploadData, | ||
options: FileOptionBuilder.() -> Unit | ||
): FileUploadResponse = | ||
uploadOrUpdate( | ||
HttpMethod.Put, bucketId, path, data, upsert | ||
HttpMethod.Put, bucketId, path, data, options | ||
) | ||
|
||
override suspend fun uploadToSignedUrl( | ||
path: String, | ||
token: String, | ||
data: UploadData, | ||
upsert: Boolean | ||
options: FileOptionBuilder.() -> Unit | ||
): FileUploadResponse { | ||
return uploadToSignedUrl(path, token, data, upsert) {} | ||
return uploadToSignedUrl(path, token, data, options) {} | ||
} | ||
|
||
override suspend fun createSignedUploadUrl(path: String): UploadSignedUrl { | ||
|
@@ -64,9 +72,13 @@ internal class BucketApiImpl(override val bucketId: String, val storage: Storage | |
) | ||
} | ||
|
||
override suspend fun upload(path: String, data: UploadData, upsert: Boolean): FileUploadResponse = | ||
override suspend fun upload( | ||
path: String, | ||
data: UploadData, | ||
options: FileOptionBuilder.() -> Unit | ||
): FileUploadResponse = | ||
uploadOrUpdate( | ||
HttpMethod.Post, bucketId, path, data, upsert | ||
HttpMethod.Post, bucketId, path, data, options | ||
) | ||
|
||
override suspend fun delete(paths: Collection<String>) { | ||
|
@@ -203,32 +215,44 @@ internal class BucketApiImpl(override val bucketId: String, val storage: Storage | |
override suspend fun list( | ||
prefix: String, | ||
filter: BucketListFilter.() -> Unit | ||
): List<BucketItem> { | ||
): List<FileObject> { | ||
return storage.api.postJson("object/list/$bucketId", buildJsonObject { | ||
put("prefix", prefix) | ||
putJsonObject(BucketListFilter().apply(filter).build()) | ||
}).safeBody() | ||
} | ||
|
||
override suspend fun info(path: String): FileObjectV2 { | ||
val response = storage.api.get("object/info/$bucketId/$path") | ||
return response.safeBody<FileObjectV2>().copy(serializer = storage.serializer) | ||
} | ||
|
||
override suspend fun exists(path: String): Boolean { | ||
try { | ||
storage.api.request("object/$bucketId/$path") { | ||
method = HttpMethod.Head | ||
} | ||
return true | ||
} catch (e: RestException) { | ||
if (e.statusCode in listOf(HttpStatusCode.NotFound.value, HttpStatusCode.BadRequest.value)) return false | ||
throw e | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
@Suppress("LongParameterList") //TODO: maybe refactor | ||
internal suspend fun uploadOrUpdate( | ||
method: HttpMethod, | ||
bucket: String, | ||
path: String, | ||
data: UploadData, | ||
upsert: Boolean, | ||
options: FileOptionBuilder.() -> Unit, | ||
extra: HttpRequestBuilder.() -> Unit = {} | ||
): FileUploadResponse { | ||
val optionBuilder = FileOptionBuilder(storage.serializer).apply(options) | ||
val response = storage.api.request("object/$bucket/$path") { | ||
this.method = method | ||
setBody(object : OutgoingContent.ReadChannelContent() { | ||
override val contentType: ContentType = ContentType.defaultForFilePath(path) | ||
override val contentLength: Long = data.size | ||
override fun readFrom(): ByteReadChannel = data.stream | ||
}) | ||
header(HttpHeaders.ContentType, ContentType.defaultForFilePath(path)) | ||
header(UPSERT_HEADER, upsert.toString()) | ||
extra() | ||
defaultUploadRequest(path, data, optionBuilder, extra) | ||
}.body<JsonObject>() | ||
val key = response["Key"]?.jsonPrimitive?.content | ||
?: error("Expected a key in a upload response") | ||
|
@@ -237,30 +261,47 @@ internal class BucketApiImpl(override val bucketId: String, val storage: Storage | |
return FileUploadResponse(id, path, key) | ||
} | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
@Suppress("LongParameterList") //TODO: maybe refactor | ||
internal suspend fun uploadToSignedUrl( | ||
path: String, | ||
token: String, | ||
data: UploadData, | ||
upsert: Boolean, | ||
options: FileOptionBuilder.() -> Unit, | ||
extra: HttpRequestBuilder.() -> Unit = {} | ||
): FileUploadResponse { | ||
val optionBuilder = FileOptionBuilder(storage.serializer).apply(options) | ||
val response = storage.api.put("object/upload/sign/$bucketId/$path") { | ||
parameter("token", token) | ||
setBody(object : OutgoingContent.ReadChannelContent() { | ||
override val contentType: ContentType = ContentType.defaultForFilePath(path) | ||
override val contentLength: Long = data.size | ||
override fun readFrom(): ByteReadChannel = data.stream | ||
}) | ||
header(HttpHeaders.ContentType, ContentType.defaultForFilePath(path)) | ||
header("x-upsert", upsert.toString()) | ||
extra() | ||
defaultUploadRequest(path, data, optionBuilder, extra) | ||
}.body<JsonObject>() | ||
val key = response["Key"]?.jsonPrimitive?.content | ||
?: error("Expected a key in a upload response") | ||
val id = response["Id"]?.jsonPrimitive?.content ?: error("Expected an id in a upload response") | ||
return FileUploadResponse(id, path, key) | ||
} | ||
|
||
@Suppress("LongParameterList") //TODO: maybe refactor | ||
@OptIn(ExperimentalEncodingApi::class) | ||
private fun HttpRequestBuilder.defaultUploadRequest( | ||
path: String, | ||
data: UploadData, | ||
optionBuilder: FileOptionBuilder, | ||
extra: HttpRequestBuilder.() -> Unit | ||
) { | ||
setBody(object : OutgoingContent.ReadChannelContent() { | ||
override val contentType: ContentType = optionBuilder.contentType ?: ContentType.defaultForFilePath(path) | ||
override val contentLength: Long = data.size | ||
override fun readFrom(): ByteReadChannel = data.stream | ||
}) | ||
header(HttpHeaders.ContentType, optionBuilder.contentType ?: ContentType.defaultForFilePath(path)) | ||
header(UPSERT_HEADER, optionBuilder.upsert.toString()) | ||
optionBuilder.userMetadata?.let { | ||
header("x-metadata", Base64.Default.encode(it.toString().encodeToByteArray())) | ||
} | ||
extra() | ||
} | ||
|
||
override suspend fun changePublicStatusTo(public: Boolean) = storage.updateBucket(bucketId) { | ||
[email protected] = public | ||
} | ||
|
28 changes: 0 additions & 28 deletions
28
Storage/src/commonMain/kotlin/io/github/jan/supabase/storage/BucketItem.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, lets wait a bit for adding the
info
method as there is a bug in the JS lib, this should beobject/info/public
, but there is also aobject/info/authenticated
.We're figuring out internally on how we're naming methods.
So lets just wait a bit before merging this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I commented on the JS PR regarding this. I couldn't make this work on the hosted Supabase instance, but the self-hosted Docker one works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@grdsdev Any news regarding this?