Skip to content

Commit

Permalink
fix: downloading issues, remove ?download param
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed May 8, 2022
1 parent d896a14 commit 9d0e927
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ plugins {
apply(plugin = "kotlinx-atomicfu")

val JAVA_VERSION = JavaVersion.VERSION_17
val VERSION = Version(1, 2, 0, 0, ReleaseType.None)
val VERSION = Version(1, 2, 1, 0, ReleaseType.None)
val COMMIT_HASH by lazy {
val cmd = "git rev-parse --short HEAD".split("\\s".toRegex())
val proc = ProcessBuilder(cmd)
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/dev/floofy/hazel/Hazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.sentry.Sentry
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
Expand Down Expand Up @@ -199,7 +200,12 @@ class Hazel {
}
}

install(Routing)
routing {
runBlocking {
createCdnEndpoints()
}
}

install(NoelKtorRoutingPlugin) {
endpointLoader = KoinEndpointLoader
}
Expand Down
25 changes: 8 additions & 17 deletions src/main/kotlin/dev/floofy/hazel/routing/RoutingExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ suspend fun Routing.createCdnEndpoints() {

log.debug("Configuring CDN endpoints...")
val contents = storage.listAll()
log.info("Found ${contents.size} items to create as routes!")
log.debug("Found ${contents.size} items to create as routes!")

for (content in contents) {
val name = if (storage.trailer is FilesystemStorageTrailer) {
Expand All @@ -61,7 +61,7 @@ suspend fun Routing.createCdnEndpoints() {
}

log.debug("Found route $name to register!")
get(name) {
get("/$name") {
callOnRoute(content, call, storage, name)
}
}
Expand Down Expand Up @@ -182,7 +182,7 @@ private suspend fun callOnRoute(
storage: StorageWrapper,
name: String
) {
val stream = storage.open(if (storage.trailer is FilesystemStorageTrailer) "./${name.substring(1)}" else name)
val stream = storage.open(if (storage.trailer is FilesystemStorageTrailer) "./$name" else name)
if (stream == null) {
call.respond(
HttpStatusCode.NotFound,
Expand Down Expand Up @@ -220,28 +220,19 @@ private suspend fun callOnRoute(

val contentType = ContentType.parse(rawContentType)
val shouldDownload = when {
call.request.queryParameters["download"] != null -> true
contentType.match(ContentType.Application.GZip) -> true
contentType.match(ContentType.Application.OctetStream) -> true
contentType.match(ContentType.Application.Zip) -> true
else -> false
}

if (shouldDownload) {
if (call.response.isCommitted) return

call.response.header(
HttpHeaders.ContentDisposition,
"attachment; filename=\"${name.substring(1).split("/").last()}\""
)

// Close the stream so we don't memory leak
withContext(Dispatchers.IO) {
stream.close()
if (!call.response.isCommitted) {
call.response.header(
HttpHeaders.ContentDisposition,
"attachment; filename=\"${name.split("/").last()}\""
)
}

call.respond(HttpStatusCode.NoContent)
return
}

// Check if it is an image, if so, let's do some image manipulation!
Expand Down

0 comments on commit 9d0e927

Please sign in to comment.