Skip to content

Commit

Permalink
feat: Added download fileName
Browse files Browse the repository at this point in the history
  • Loading branch information
LotuxPunk committed Jul 14, 2024
1 parent f0807fa commit fb144e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ Downloads a file from the server, usually used to embed the file in an `<img>`,

###### 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`

Expand Down
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions src/main/kotlin/be/vandeas/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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())
}
}
}
}
}
}
Expand Down

0 comments on commit fb144e6

Please sign in to comment.