-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add eTags and LastModified headers (#175)
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
- Loading branch information
1 parent
391b4de
commit f5cf508
Showing
9 changed files
with
115 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package server.plugins | ||
|
||
import io.ktor.http.HttpHeaders | ||
import io.ktor.http.content.EntityTagVersion | ||
import io.ktor.server.http.content.LastModifiedVersion | ||
import io.ktor.server.plugins.conditionalheaders.ConditionalHeadersConfig | ||
import java.security.MessageDigest | ||
import server.response.FileSource | ||
import server.response.FileUUID | ||
import storage.FileType | ||
import storage.HashUtils | ||
import storage.MessageDigestAlgorithm | ||
|
||
fun ConditionalHeadersConfig.configure() { | ||
version { call, outgoingContent -> | ||
val fileUUID = call.response.headers[HttpHeaders.FileUUID] | ||
val fileSource = call.response.headers[HttpHeaders.FileSource] | ||
val fileType = FileType.entries.find { it.headerValue == fileSource } | ||
if (fileUUID != null && fileType != null) { | ||
val file = fileType.fetcher(fileUUID)?.takeIf { it.exists() } | ||
if (file != null) { | ||
val modificationDate = file.lastModified() | ||
val checkSumSha256 = HashUtils.getCheckSumFromFile( | ||
MessageDigest.getInstance(MessageDigestAlgorithm.SHA_256), | ||
file | ||
) | ||
return@version listOf( | ||
EntityTagVersion(checkSumSha256), | ||
LastModifiedVersion(modificationDate) | ||
) | ||
} | ||
} | ||
|
||
emptyList() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package server.response | ||
|
||
import io.ktor.http.HttpHeaders | ||
import storage.FileType | ||
|
||
/** | ||
* A header included in the responses of file requests, containing the UUID of the file. | ||
*/ | ||
val HttpHeaders.FileUUID: String get() = "X-File-UUID" | ||
|
||
/** | ||
* A header included in the responses of file requests, where the file comes from. Basically one of the following: | ||
* - `Images` ([FileType.IMAGE]) | ||
* - `Tracks` ([FileType.TRACK]) | ||
* @see FileType | ||
*/ | ||
val HttpHeaders.FileSource: String get() = "X-File-Source" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package storage | ||
|
||
import java.io.File | ||
|
||
enum class FileType(val headerValue: String, val fetcher: (uuid: String) -> File?) { | ||
IMAGE("Images", Storage::imageFile), | ||
TRACK("Tracks", Storage::trackFile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters