Skip to content

Commit

Permalink
fix(endpoints:list): do not give away content type (since it dum) and…
Browse files Browse the repository at this point in the history
… normalize endpoint param
  • Loading branch information
auguwu committed May 8, 2022
1 parent 9049ff1 commit 205980c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
26 changes: 0 additions & 26 deletions src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<org.noelware.remi.core.Object> =
if (force || listCache.isEmpty()) {
val c = trailer.listAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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(
Expand All @@ -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())
Expand Down

0 comments on commit 205980c

Please sign in to comment.