From fb144e6d16bb74efaef288387f155e5d00ba16ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vandendaelen?= Date: Sun, 14 Jul 2024 19:29:57 +0200 Subject: [PATCH] feat: Added download fileName --- README.md | 11 ++++++----- build.gradle.kts | 8 ++++---- gradle.properties | 4 ++-- src/main/kotlin/be/vandeas/plugins/Routing.kt | 10 ++++++++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c3d88b7..1523d2b 100644 --- a/README.md +++ b/README.md @@ -120,11 +120,12 @@ Downloads a file from the server, usually used to embed the file in an ``, ###### Query Parameters -| Name | Type | Description | -|------------|----------|-----------------------------------------------| -| `fileName` | `string` | The name of the file that will be downloaded. | -| `path` | `string` | The path of the file that will be downloaded. | -| `token` | `string` | The one-time-token. | +| Name | Type | Description | +|------------|----------|----------------------------------------------------------------------------------------------------------------------------| +| `fileName` | `string` | The name of the file that will be downloaded. | +| `path` | `string` | The path of the file that will be downloaded. | +| `token` | `string` | The one-time-token. | +| `download` | `string` | Download filename (via [Content-Disposition](https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Disposition) ) | ##### DELETE `/v1/file` diff --git a/build.gradle.kts b/build.gradle.kts index 0028f80..b8c7af6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,9 +5,9 @@ val logback_version: String by project val koin_ktor: String by project plugins { - kotlin("jvm") version "1.9.23" - id("io.ktor.plugin") version "2.3.10" - id("org.jetbrains.kotlin.plugin.serialization") version "1.9.23" + kotlin("jvm") version "1.9.24" + id("io.ktor.plugin") version "2.3.12" + id("org.jetbrains.kotlin.plugin.serialization") version "1.9.24" } group = "be.vandeas" @@ -38,7 +38,7 @@ dependencies { implementation("io.ktor:ktor-server-host-common-jvm") implementation("io.ktor:ktor-server-cio-jvm") implementation("ch.qos.logback:logback-classic:$logback_version") - implementation("io.ktor:ktor-server-config-yaml:2.3.10") + implementation("io.ktor:ktor-server-config-yaml:2.3.12") // Ktor features plugins implementation("io.ktor:ktor-server-partial-content:$ktor_version") diff --git a/gradle.properties b/gradle.properties index ce12a53..42328ef 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ -ktor_version=2.3.10 -kotlin_version=1.9.23 +ktor_version=2.3.12 +kotlin_version=1.9.24 logback_version=1.4.14 koin_ktor=3.5.6 kotlin.code.style=official diff --git a/src/main/kotlin/be/vandeas/plugins/Routing.kt b/src/main/kotlin/be/vandeas/plugins/Routing.kt index 2f28bbd..bb12671 100644 --- a/src/main/kotlin/be/vandeas/plugins/Routing.kt +++ b/src/main/kotlin/be/vandeas/plugins/Routing.kt @@ -12,7 +12,6 @@ import io.ktor.server.plugins.partialcontent.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import io.ktor.util.* import org.koin.ktor.ext.inject fun Application.configureRouting() { @@ -55,6 +54,7 @@ fun Application.configureRouting() { get("/embed") { val path = call.request.queryParameters["path"] ?: "" val fileName = call.request.queryParameters["fileName"] ?: "" + val downloadFileName = call.request.queryParameters["download"] ?: "" val authorization = call.request.queryParameters["token"] ?: call.request.authorization() ?: throw IllegalArgumentException("Authorization header is required") if (path.isBlank() || fileName.isBlank()) { @@ -71,7 +71,13 @@ fun Application.configureRouting() { ContentDisposition.Attachment.withParameter(ContentDisposition.Parameters.FileName, fileName) .toString() ) - call.respondFile(result.file) + call.respondFile(result.file) { + headers { + if (downloadFileName.isNotBlank()) { + append(HttpHeaders.ContentDisposition, ContentDisposition.Attachment.withParameter(ContentDisposition.Parameters.FileName, downloadFileName).toString()) + } + } + } } } }