|
1 | 1 | package com.vk.admstorm.services
|
2 | 2 |
|
| 3 | +import com.intellij.execution.process.ProcessIOExecutorService |
3 | 4 | import com.intellij.openapi.components.Service
|
4 | 5 | import com.intellij.openapi.components.service
|
| 6 | +import com.intellij.openapi.diagnostic.logger |
5 | 7 | import com.intellij.openapi.project.Project
|
6 |
| -import com.vk.admstorm.CommandRunner |
7 | 8 | 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 |
8 | 19 |
|
9 | 20 | @Service(Service.Level.PROJECT)
|
10 |
| -class HastebinService(private val project: Project) { |
| 21 | +class HastebinService { |
11 | 22 | companion object {
|
| 23 | + private val LOG = logger<HastebinService>() |
| 24 | + |
12 | 25 | fun getInstance(project: Project) = project.service<HastebinService>()
|
13 | 26 | }
|
14 | 27 |
|
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}" |
18 | 65 | }
|
19 | 66 | }
|
0 commit comments