1
1
package server.plugins
2
2
3
+ import ServerDatabase
4
+ import database.entity.Area
5
+ import database.entity.Path
6
+ import database.entity.Sector
7
+ import database.entity.Zone
3
8
import io.ktor.http.HttpHeaders
4
9
import io.ktor.http.content.EntityTagVersion
5
10
import io.ktor.server.http.content.LastModifiedVersion
6
11
import io.ktor.server.plugins.conditionalheaders.ConditionalHeadersConfig
7
12
import java.security.MessageDigest
8
13
import server.response.FileSource
9
14
import server.response.FileUUID
15
+ import server.response.ResourceId
16
+ import server.response.ResourceType
10
17
import storage.FileType
11
18
import storage.HashUtils
12
19
import storage.MessageDigestAlgorithm
13
20
21
+ @OptIn(ExperimentalStdlibApi ::class )
14
22
fun ConditionalHeadersConfig.configure () {
15
23
version { call, outgoingContent ->
16
- val fileUUID = call.response.headers[HttpHeaders .FileUUID ]
17
- val fileSource = call.response.headers[HttpHeaders .FileSource ]
24
+ val headers = call.response.headers
25
+
26
+ val fileUUID = headers[HttpHeaders .FileUUID ]
27
+ val fileSource = headers[HttpHeaders .FileSource ]
18
28
val fileType = FileType .entries.find { it.headerValue == fileSource }
19
29
if (fileUUID != null && fileType != null ) {
20
30
val file = fileType.fetcher(fileUUID)?.takeIf { it.exists() }
@@ -31,6 +41,28 @@ fun ConditionalHeadersConfig.configure() {
31
41
}
32
42
}
33
43
44
+ val resourceType = headers[HttpHeaders .ResourceType ]
45
+ val resourceId = headers[HttpHeaders .ResourceId ]?.toIntOrNull()
46
+ if (resourceType != null && resourceId != null ) {
47
+ val resource = ServerDatabase {
48
+ when (resourceType) {
49
+ " Area" -> Area [resourceId]
50
+ " Zone" -> Zone [resourceId]
51
+ " Sector" -> Sector [resourceId]
52
+ " Path" -> Path [resourceId]
53
+ else -> null
54
+ }
55
+ }
56
+ if (resource != null ) {
57
+ return @version listOfNotNull(
58
+ EntityTagVersion (
59
+ ServerDatabase { resource.hashCode().toHexString() }
60
+ ),
61
+ LastModifiedVersion (resource.timestamp.toEpochMilli())
62
+ )
63
+ }
64
+ }
65
+
34
66
emptyList()
35
67
}
36
68
}
0 commit comments