diff --git a/src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt b/src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt index 83d1f5d1..33653075 100644 --- a/src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt +++ b/src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt @@ -20,9 +20,16 @@ class CreateHasteAction : AdmActionBase() { executeOnPooledThread { val link = HastebinService.getInstance(e.project!!).createHaste(copyText) - MyUtils.copyToClipboard(link) + + val text = if (link != null) { + MyUtils.copyToClipboard(link) + "Link to hastebin copied to clipboard" + } else { + "Hastebin service unavailable. Try again later" + } + AdmNotification() - .withTitle("Link to hastebin copied to clipboard") + .withTitle(text) .show() } } diff --git a/src/main/kotlin/com/vk/admstorm/actions/SendLogsToHastebinAction.kt b/src/main/kotlin/com/vk/admstorm/actions/SendLogsToHastebinAction.kt index da4e5e0b..dcdf5114 100644 --- a/src/main/kotlin/com/vk/admstorm/actions/SendLogsToHastebinAction.kt +++ b/src/main/kotlin/com/vk/admstorm/actions/SendLogsToHastebinAction.kt @@ -12,6 +12,13 @@ class SendLogsToHastebinAction : AdmActionBase() { val project = e.project ?: return val hasteLink = HastebinService.getInstance(project).createHaste(readIdeaLogFile()) + if (hasteLink == null) { + AdmNotification() + .withTitle("Hastebin service unavailable. Try again later") + .show(project) + return + } + AdmNotification("Thanks for logs!") .withTitle("Logs successfully sent to Hastebin") .withActions(AdmNotification.Action("Copy hastebin link") { _, notification -> diff --git a/src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt b/src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt index e3a070ac..d83df3cf 100644 --- a/src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt +++ b/src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt @@ -91,10 +91,16 @@ abstract class BaseRemoteExecutor(protected val project: Project, toolName: Stri executeOnPooledThread { val output = outputListener.output.stdout + outputListener.output.stderr val link = HastebinService.getInstance(e.project!!).createHaste(output) - copyToClipboard(link) + + val text = if (link != null) { + copyToClipboard(link) + "Link to hastebin copied to clipboard" + } else { + "Hastebin service unavailable. Try again later" + } AdmNotification() - .withTitle("Link to hastebin copied to clipboard") + .withTitle(text) .show() } } diff --git a/src/main/kotlin/com/vk/admstorm/playground/KphpPlaygroundWindow.kt b/src/main/kotlin/com/vk/admstorm/playground/KphpPlaygroundWindow.kt index 5582cb49..b0c7c9fb 100644 --- a/src/main/kotlin/com/vk/admstorm/playground/KphpPlaygroundWindow.kt +++ b/src/main/kotlin/com/vk/admstorm/playground/KphpPlaygroundWindow.kt @@ -43,6 +43,7 @@ import com.vk.admstorm.configuration.kphp.KphpScriptRunner import com.vk.admstorm.configuration.kphp.KphpUtils.scriptBinaryPath import com.vk.admstorm.console.Console import com.vk.admstorm.notifications.AdmErrorNotification +import com.vk.admstorm.notifications.AdmNotification import com.vk.admstorm.psi.PhpRecursiveElementVisitor import com.vk.admstorm.services.HastebinService import com.vk.admstorm.transfer.TransferService @@ -128,8 +129,14 @@ require_once 'vendor/autoload.php'; executeOnPooledThread { val link = HastebinService.getInstance(myProject).createHaste(content) - MyUtils.copyToClipboard(link) + if (link == null) { + AdmNotification() + .withTitle("Hastebin service unavailable. Try again later") + .show() + return@executeOnPooledThread + } + MyUtils.copyToClipboard(link) invokeLater { myShareLabel.text = "Link copied" myShareLabel.isVisible = true diff --git a/src/main/kotlin/com/vk/admstorm/services/HastebinService.kt b/src/main/kotlin/com/vk/admstorm/services/HastebinService.kt index 044f9eb8..6b190783 100644 --- a/src/main/kotlin/com/vk/admstorm/services/HastebinService.kt +++ b/src/main/kotlin/com/vk/admstorm/services/HastebinService.kt @@ -1,19 +1,66 @@ package com.vk.admstorm.services +import com.intellij.execution.process.ProcessIOExecutorService import com.intellij.openapi.components.Service import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.Project -import com.vk.admstorm.CommandRunner import com.vk.admstorm.env.Env +import com.vk.admstorm.env.getByKey +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonPrimitive +import java.net.HttpURLConnection +import java.net.URI +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration @Service(Service.Level.PROJECT) -class HastebinService(private val project: Project) { +class HastebinService { companion object { + private val LOG = logger() + fun getInstance(project: Project) = project.service() } - fun createHaste(data: String): String { - val output = data.replace("\"", "\\\"").replace("$", "\\$") - return CommandRunner.runRemotely(project, "echo \"$output\" | ${Env.data.pasteBinCommand}").stdout + private fun createHttpClient(): HttpClient { + return HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10)) + .executor(ProcessIOExecutorService.INSTANCE) + .build() + } + + fun createHaste(data: String): String? { + val hastLink = Env.data.services.getByKey("hastebin")?.url ?: return null + LOG.info("Getting hastLink, hastLink is $hastLink") + + val httpClient = createHttpClient() + val request = HttpRequest.newBuilder() + .uri(URI.create("$hastLink/documents")) + .POST(HttpRequest.BodyPublishers.ofString(data)) + .timeout(Duration.ofSeconds(10)) + .build() + + val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()) + if (response.statusCode() != HttpURLConnection.HTTP_OK ) { + LOG.error("Error sending the request (status code: ${response.statusCode()}, body: ${response.body()})") + return null + } + + val jsonResponse = Json.parseToJsonElement(response.body()) + if(jsonResponse !is JsonObject){ + LOG.error("Error parsing the request (body: ${response.body()})") + return null + } + + val value = jsonResponse["key"]?.jsonPrimitive?.content + if (value == null) { + LOG.error("JsonResponse returned null value") + return null + } + + return "$hastLink/${value}" } }