From 205980c3e506dc29d994c35b7a73106a533d3b44 Mon Sep 17 00:00:00 2001 From: Noel Date: Sat, 7 May 2022 17:07:38 -0700 Subject: [PATCH] fix(endpoints:list): do not give away content type (since it dum) and normalize endpoint param --- .../dev/floofy/hazel/core/StorageWrapper.kt | 26 ------------------- .../routing/endpoints/ListFilesEndpoint.kt | 17 +++++++----- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt b/src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt index 7dc88bf0..1e7b32c6 100644 --- a/src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt +++ b/src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt @@ -87,32 +87,6 @@ class StorageWrapper(config: StorageConfig) { */ suspend fun open(path: String): InputStream? = trailer.open(path) - /** - * Deletes the file under the [path] and returns a [Boolean] if the - * operation was a success or not. - */ - suspend fun delete(path: String): Boolean = trailer.delete(path) - - /** - * Checks if the file exists under this storage trailer. - * @param path The path to find the file. - */ - suspend fun exists(path: String): Boolean = trailer.exists(path) - - /** - * Uploads file to this storage trailer and returns a [Boolean] result - * if the operation was a success or not. - * - * @param path The path to upload the file to - * @param stream The [InputStream] that represents the raw data. - * @param contentType The content type of the file (useful for S3 and GCS support)! - */ - suspend fun upload( - path: String, - stream: InputStream, - contentType: String = "application/octet-stream" - ): Boolean = trailer.upload(path, stream, contentType) - suspend fun listAll(force: Boolean = true): List = if (force || listCache.isEmpty()) { val c = trailer.listAll() diff --git a/src/main/kotlin/dev/floofy/hazel/routing/endpoints/ListFilesEndpoint.kt b/src/main/kotlin/dev/floofy/hazel/routing/endpoints/ListFilesEndpoint.kt index 3daf433e..fe181991 100644 --- a/src/main/kotlin/dev/floofy/hazel/routing/endpoints/ListFilesEndpoint.kt +++ b/src/main/kotlin/dev/floofy/hazel/routing/endpoints/ListFilesEndpoint.kt @@ -26,6 +26,7 @@ import io.ktor.server.response.* import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import org.apache.commons.lang3.time.StopWatch +import org.noelware.remi.filesystem.FilesystemStorageTrailer import java.util.concurrent.TimeUnit class ListFilesEndpoint(private val trailer: StorageWrapper, private val config: Config): AbstractEndpoint("/list") { @@ -34,10 +35,6 @@ class ListFilesEndpoint(private val trailer: StorageWrapper, private val config: val files = trailer.listAll() stopwatch.stop() - val baseUrl = config.baseUrl.ifEmpty { - "http://${config.server.host}:${config.server.port}" - } - val payload = buildJsonObject { put("success", true) put( @@ -49,9 +46,17 @@ class ListFilesEndpoint(private val trailer: StorageWrapper, private val config: put( file.name, buildJsonObject { - put("endpoint", "$baseUrl/${file.name}") + put( + "endpoint", + if (trailer.trailer is FilesystemStorageTrailer) { + file.name + .replace(System.getProperty("user.dir", ""), "") + .replace(trailer.trailer.config.directory, "") + } else { + file.name + } + ) put("name", file.name) - put("content_type", file.contentType) put("will_prompt_download", file.contentType.startsWith("application/octet-stream")) put("size", file.size.toDouble()) put("created_at", file.createdAt.toString())