Skip to content

Commit 9d0e927

Browse files
committed
fix: downloading issues, remove ?download param
1 parent d896a14 commit 9d0e927

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ plugins {
5050
apply(plugin = "kotlinx-atomicfu")
5151

5252
val JAVA_VERSION = JavaVersion.VERSION_17
53-
val VERSION = Version(1, 2, 0, 0, ReleaseType.None)
53+
val VERSION = Version(1, 2, 1, 0, ReleaseType.None)
5454
val COMMIT_HASH by lazy {
5555
val cmd = "git rev-parse --short HEAD".split("\\s".toRegex())
5656
val proc = ProcessBuilder(cmd)

src/main/kotlin/dev/floofy/hazel/Hazel.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import io.ktor.server.request.*
4040
import io.ktor.server.response.*
4141
import io.ktor.server.routing.*
4242
import io.sentry.Sentry
43+
import kotlinx.coroutines.runBlocking
4344
import kotlinx.serialization.json.buildJsonArray
4445
import kotlinx.serialization.json.buildJsonObject
4546
import kotlinx.serialization.json.put
@@ -199,7 +200,12 @@ class Hazel {
199200
}
200201
}
201202

202-
install(Routing)
203+
routing {
204+
runBlocking {
205+
createCdnEndpoints()
206+
}
207+
}
208+
203209
install(NoelKtorRoutingPlugin) {
204210
endpointLoader = KoinEndpointLoader
205211
}

src/main/kotlin/dev/floofy/hazel/routing/RoutingExtensions.kt

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ suspend fun Routing.createCdnEndpoints() {
4949

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

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

6363
log.debug("Found route $name to register!")
64-
get(name) {
64+
get("/$name") {
6565
callOnRoute(content, call, storage, name)
6666
}
6767
}
@@ -182,7 +182,7 @@ private suspend fun callOnRoute(
182182
storage: StorageWrapper,
183183
name: String
184184
) {
185-
val stream = storage.open(if (storage.trailer is FilesystemStorageTrailer) "./${name.substring(1)}" else name)
185+
val stream = storage.open(if (storage.trailer is FilesystemStorageTrailer) "./$name" else name)
186186
if (stream == null) {
187187
call.respond(
188188
HttpStatusCode.NotFound,
@@ -220,28 +220,19 @@ private suspend fun callOnRoute(
220220

221221
val contentType = ContentType.parse(rawContentType)
222222
val shouldDownload = when {
223-
call.request.queryParameters["download"] != null -> true
224223
contentType.match(ContentType.Application.GZip) -> true
225224
contentType.match(ContentType.Application.OctetStream) -> true
226225
contentType.match(ContentType.Application.Zip) -> true
227226
else -> false
228227
}
229228

230229
if (shouldDownload) {
231-
if (call.response.isCommitted) return
232-
233-
call.response.header(
234-
HttpHeaders.ContentDisposition,
235-
"attachment; filename=\"${name.substring(1).split("/").last()}\""
236-
)
237-
238-
// Close the stream so we don't memory leak
239-
withContext(Dispatchers.IO) {
240-
stream.close()
230+
if (!call.response.isCommitted) {
231+
call.response.header(
232+
HttpHeaders.ContentDisposition,
233+
"attachment; filename=\"${name.split("/").last()}\""
234+
)
241235
}
242-
243-
call.respond(HttpStatusCode.NoContent)
244-
return
245236
}
246237

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

0 commit comments

Comments
 (0)