Skip to content

Commit ea6996d

Browse files
authored
sending Hastebin through HTTP request (#223)
1 parent 3dfe734 commit ea6996d

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ class CreateHasteAction : AdmActionBase() {
2020

2121
executeOnPooledThread {
2222
val link = HastebinService.getInstance(e.project!!).createHaste(copyText)
23-
MyUtils.copyToClipboard(link)
23+
24+
val text = if (link != null) {
25+
MyUtils.copyToClipboard(link)
26+
"Link to hastebin copied to clipboard"
27+
} else {
28+
"Hastebin service unavailable. Try again later"
29+
}
30+
2431
AdmNotification()
25-
.withTitle("Link to hastebin copied to clipboard")
32+
.withTitle(text)
2633
.show()
2734
}
2835
}

src/main/kotlin/com/vk/admstorm/actions/SendLogsToHastebinAction.kt

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class SendLogsToHastebinAction : AdmActionBase() {
1212
val project = e.project ?: return
1313
val hasteLink = HastebinService.getInstance(project).createHaste(readIdeaLogFile())
1414

15+
if (hasteLink == null) {
16+
AdmNotification()
17+
.withTitle("Hastebin service unavailable. Try again later")
18+
.show(project)
19+
return
20+
}
21+
1522
AdmNotification("Thanks for logs!")
1623
.withTitle("Logs successfully sent to Hastebin")
1724
.withActions(AdmNotification.Action("Copy hastebin link") { _, notification ->

src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ abstract class BaseRemoteExecutor(protected val project: Project, toolName: Stri
9191
executeOnPooledThread {
9292
val output = outputListener.output.stdout + outputListener.output.stderr
9393
val link = HastebinService.getInstance(e.project!!).createHaste(output)
94-
copyToClipboard(link)
94+
95+
val text = if (link != null) {
96+
copyToClipboard(link)
97+
"Link to hastebin copied to clipboard"
98+
} else {
99+
"Hastebin service unavailable. Try again later"
100+
}
95101

96102
AdmNotification()
97-
.withTitle("Link to hastebin copied to clipboard")
103+
.withTitle(text)
98104
.show()
99105
}
100106
}

src/main/kotlin/com/vk/admstorm/playground/KphpPlaygroundWindow.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import com.vk.admstorm.configuration.kphp.KphpScriptRunner
4343
import com.vk.admstorm.configuration.kphp.KphpUtils.scriptBinaryPath
4444
import com.vk.admstorm.console.Console
4545
import com.vk.admstorm.notifications.AdmErrorNotification
46+
import com.vk.admstorm.notifications.AdmNotification
4647
import com.vk.admstorm.psi.PhpRecursiveElementVisitor
4748
import com.vk.admstorm.services.HastebinService
4849
import com.vk.admstorm.transfer.TransferService
@@ -128,8 +129,14 @@ require_once 'vendor/autoload.php';
128129

129130
executeOnPooledThread {
130131
val link = HastebinService.getInstance(myProject).createHaste(content)
131-
MyUtils.copyToClipboard(link)
132+
if (link == null) {
133+
AdmNotification()
134+
.withTitle("Hastebin service unavailable. Try again later")
135+
.show()
136+
return@executeOnPooledThread
137+
}
132138

139+
MyUtils.copyToClipboard(link)
133140
invokeLater {
134141
myShareLabel.text = "Link copied"
135142
myShareLabel.isVisible = true
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
11
package com.vk.admstorm.services
22

3+
import com.intellij.execution.process.ProcessIOExecutorService
34
import com.intellij.openapi.components.Service
45
import com.intellij.openapi.components.service
6+
import com.intellij.openapi.diagnostic.logger
57
import com.intellij.openapi.project.Project
6-
import com.vk.admstorm.CommandRunner
78
import com.vk.admstorm.env.Env
9+
import com.vk.admstorm.env.getByKey
10+
import kotlinx.serialization.json.Json
11+
import kotlinx.serialization.json.JsonObject
12+
import kotlinx.serialization.json.jsonPrimitive
13+
import java.net.HttpURLConnection
14+
import java.net.URI
15+
import java.net.http.HttpClient
16+
import java.net.http.HttpRequest
17+
import java.net.http.HttpResponse
18+
import java.time.Duration
819

920
@Service(Service.Level.PROJECT)
10-
class HastebinService(private val project: Project) {
21+
class HastebinService {
1122
companion object {
23+
private val LOG = logger<HastebinService>()
24+
1225
fun getInstance(project: Project) = project.service<HastebinService>()
1326
}
1427

15-
fun createHaste(data: String): String {
16-
val output = data.replace("\"", "\\\"").replace("$", "\\$")
17-
return CommandRunner.runRemotely(project, "echo \"$output\" | ${Env.data.pasteBinCommand}").stdout
28+
private fun createHttpClient(): HttpClient {
29+
return HttpClient.newBuilder()
30+
.connectTimeout(Duration.ofSeconds(10))
31+
.executor(ProcessIOExecutorService.INSTANCE)
32+
.build()
33+
}
34+
35+
fun createHaste(data: String): String? {
36+
val hastLink = Env.data.services.getByKey("hastebin")?.url ?: return null
37+
LOG.info("Getting hastLink, hastLink is $hastLink")
38+
39+
val httpClient = createHttpClient()
40+
val request = HttpRequest.newBuilder()
41+
.uri(URI.create("$hastLink/documents"))
42+
.POST(HttpRequest.BodyPublishers.ofString(data))
43+
.timeout(Duration.ofSeconds(10))
44+
.build()
45+
46+
val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString())
47+
if (response.statusCode() != HttpURLConnection.HTTP_OK ) {
48+
LOG.error("Error sending the request (status code: ${response.statusCode()}, body: ${response.body()})")
49+
return null
50+
}
51+
52+
val jsonResponse = Json.parseToJsonElement(response.body())
53+
if(jsonResponse !is JsonObject){
54+
LOG.error("Error parsing the request (body: ${response.body()})")
55+
return null
56+
}
57+
58+
val value = jsonResponse["key"]?.jsonPrimitive?.content
59+
if (value == null) {
60+
LOG.error("JsonResponse returned null value")
61+
return null
62+
}
63+
64+
return "$hastLink/${value}"
1865
}
1966
}

0 commit comments

Comments
 (0)