-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
UncivServerFileStorage.kt
52 lines (45 loc) · 1.63 KB
/
UncivServerFileStorage.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.unciv.logic.multiplayer.storage
import com.badlogic.gdx.Net
import java.io.FileNotFoundException
import java.lang.Exception
class UncivServerFileStorage(val serverUrl: String, val timeout: Int = 30000) : FileStorage {
override fun saveFileData(fileName: String, data: String, overwrite: Boolean) {
SimpleHttp.sendRequest(Net.HttpMethods.PUT, fileUrl(fileName), data, timeout) {
success, result, _ ->
if (!success) {
println(result)
throw Exception(result)
}
}
}
override fun loadFileData(fileName: String): String {
var fileData = ""
SimpleHttp.sendGetRequest(fileUrl(fileName), timeout = timeout){
success, result, code ->
if (!success) {
println(result)
when (code) {
404 -> throw FileNotFoundException(result)
else -> throw Exception(result)
}
}
else fileData = result
}
return fileData
}
override fun getFileMetaData(fileName: String): FileMetaData {
TODO("Not yet implemented")
}
override fun deleteFile(fileName: String) {
SimpleHttp.sendRequest(Net.HttpMethods.DELETE, fileUrl(fileName), "", timeout) {
success, result, code ->
if (!success) {
when (code) {
404 -> throw FileNotFoundException(result)
else -> throw Exception(result)
}
}
}
}
private fun fileUrl(fileName: String) = "$serverUrl/files/$fileName"
}