1+ package net.onelitefeather.plugindebug
2+
3+ import com.google.gson.Gson
4+ import com.google.gson.JsonArray
5+ import net.lingala.zip4j.ZipFile
6+ import java.net.URI
7+ import java.net.http.HttpClient
8+ import java.net.http.HttpRequest
9+ import java.net.http.HttpResponse
10+ import java.nio.file.Files
11+ import java.nio.file.Path
12+ import java.time.Duration
13+ import kotlin.io.path.fileSize
14+ import kotlin.io.path.readLines
15+ import kotlin.io.path.writeLines
16+ import kotlin.io.path.writeText
17+ import kotlin.jvm.optionals.getOrNull
18+
19+ class DebugBuilder private constructor(private val uploadServer : String ) {
20+ private val fileToUpload: MutableList <DebugFile > = mutableListOf ()
21+ private var maxPartFileSize: Long = 2_097_152
22+ private var tempPrefix: String = " plugin-debug"
23+ private val cleanIpRegex = PRIVACY_REGEX
24+ private var maxZipFileSize: Long = 2_097_152
25+ private var userAgent = " plugin-debug"
26+ private val gson: Gson = Gson ()
27+ .newBuilder()
28+ .setPrettyPrinting()
29+ .registerTypeAdapter(DebugFile ::class .java, DebugFileAdapter ())
30+ .setLenient()
31+ .create()
32+
33+ @Throws(IllegalStateException ::class )
34+ fun enableLatestLogSpigot (): DebugBuilder {
35+ val latestLogFile = Path .of(" logs" , " latest.log" )
36+ if (latestLogFile.fileSize() >= maxPartFileSize)
37+ throw IllegalStateException (" Latest log is to big only {} bytes allowed" .format(maxPartFileSize))
38+ val logFile = Files .createTempFile(tempPrefix, " .log" )
39+ logFile.writeLines(latestLogFile.readLines().map { it.replace(cleanIpRegex, " *" ) })
40+ fileToUpload.add(DebugFile (logFile, FileType .LOG , " Latest Log" ))
41+ return this
42+ }
43+
44+ fun addFile (file : DebugFile ): DebugBuilder {
45+ fileToUpload.add(file)
46+ return this
47+ }
48+
49+ fun addFile (
50+ filePath : Path ,
51+ fileType : FileType ,
52+ uiTabName : String ,
53+ ): DebugBuilder {
54+ fileToUpload.add(DebugFile (filePath, fileType, uiTabName))
55+ return this
56+ }
57+
58+
59+ fun addText (text : String , uiTabName : String ): DebugBuilder {
60+ val textFile = Files .createTempFile(tempPrefix, " .txt" )
61+ textFile.writeText(text)
62+ fileToUpload.add(DebugFile (textFile, FileType .TEXT , uiTabName))
63+ return this
64+ }
65+
66+ fun addJson (json : String , uiTabName : String ): DebugBuilder {
67+ val textFile = Files .createTempFile(tempPrefix, " .json" )
68+ textFile.writeText(json)
69+ fileToUpload.add(DebugFile (textFile, FileType .JSON , uiTabName))
70+ return this
71+ }
72+
73+ fun addYAML (json : String , uiTabName : String ): DebugBuilder {
74+ val textFile = Files .createTempFile(tempPrefix, " .yml" )
75+ textFile.writeText(json)
76+ fileToUpload.add(DebugFile (textFile, FileType .YAML , uiTabName))
77+ return this
78+ }
79+
80+ @Throws(IllegalStateException ::class )
81+ fun upload (): DebugUploadResult {
82+ val tempFolder = Files .createTempDirectory(tempPrefix)
83+ val zipFile = tempFolder.resolve(" debug.zip" )
84+ val zipContainer = ZipFile (zipFile.toFile())
85+ val jsonArray = JsonArray ()
86+ fileToUpload.forEach {
87+ zipContainer.addFile(it.filePath.toFile())
88+ jsonArray.add(gson.toJsonTree(it))
89+ }
90+ val debugJson = tempFolder.resolve(" debug.json" )
91+ debugJson.writeText(gson.toJson(jsonArray))
92+ zipContainer.addFile(debugJson.toFile())
93+ if (zipFile.fileSize() >= maxZipFileSize)
94+ throw IllegalStateException (" Zip file is to big only {} bytes allowed" .format(maxZipFileSize))
95+ val client = HttpClient .newBuilder().build()
96+ val request: HttpRequest = HttpRequest .newBuilder()
97+ .uri(URI .create(" $uploadServer /post" ))
98+ .timeout(Duration .ofMinutes(2 ))
99+ .header(" User-Agent" , userAgent)
100+ .header(" Content-Type" , " application/zip" ).POST (HttpRequest .BodyPublishers .ofFile(zipFile)).build()
101+ val response: HttpResponse <String > = client.sendAsync(request, HttpResponse .BodyHandlers .ofString()).join()
102+ val code = response.headers().firstValue(" Location" ).getOrNull()
103+ ? : throw IllegalStateException (" Key not returned" )
104+ return DebugUploadResult (
105+ code,
106+ uploadServer
107+ )
108+ }
109+
110+ companion object {
111+ @JvmStatic
112+ fun builder (uploadServer : String ): DebugBuilder = DebugBuilder (uploadServer)
113+ }
114+ }
0 commit comments