Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending Hastebin through HTTP request #223

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/kotlin/com/vk/admstorm/actions/CreateHasteAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/com/vk/admstorm/executors/BaseRemoteExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 52 additions & 5 deletions src/main/kotlin/com/vk/admstorm/services/HastebinService.kt
Original file line number Diff line number Diff line change
@@ -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 {
Danil42Russia marked this conversation as resolved.
Show resolved Hide resolved
companion object {
private val LOG = logger<HastebinService>()

fun getInstance(project: Project) = project.service<HastebinService>()
}

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")
Danil42Russia marked this conversation as resolved.
Show resolved Hide resolved

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())
Danil42Russia marked this conversation as resolved.
Show resolved Hide resolved
if(jsonResponse !is JsonObject){
LOG.error("Error parsing the request (body: ${response.body()})")
return null
}

val value = jsonResponse["key"]?.jsonPrimitive?.content
Danil42Russia marked this conversation as resolved.
Show resolved Hide resolved
if (value == null) {
LOG.error("JsonResponse returned null value")
return null
}

return "$hastLink/${value}"
}
}
Loading