2023-09-16 17:18:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// uploadPaste uploads a given text to a pastebin site and returns the link
|
|
|
|
//
|
|
|
|
// this whole function was pretty much yoinked from here
|
|
|
|
// https://github.com/zneix/haste-client/blob/master/main.go <3
|
|
|
|
func (app *application) uploadPaste(text string) (string, error) {
|
2023-09-16 19:39:10 +02:00
|
|
|
const hasteURL = "https://haste.noury.cc"
|
|
|
|
const apiRoute = "/documents"
|
2023-09-16 17:18:14 +02:00
|
|
|
var httpClient = &http.Client{Timeout: 10 * time.Second}
|
|
|
|
|
|
|
|
type pasteResponse struct {
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", hasteURL+apiRoute, bytes.NewBufferString(text))
|
|
|
|
if err != nil {
|
2023-09-16 19:39:10 +02:00
|
|
|
app.Log.Errorln("Could not upload paste:", err)
|
2023-09-16 17:18:14 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-16 19:39:10 +02:00
|
|
|
req.Header.Set("User-Agent", "nourybot")
|
2023-09-16 17:18:14 +02:00
|
|
|
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-09-16 19:39:10 +02:00
|
|
|
app.Log.Errorln("Error while sending HTTP request:", err)
|
2023-09-16 17:18:14 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusMultipleChoices {
|
|
|
|
app.Log.Errorln("Failed to upload data, server responded with", resp.StatusCode)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Errorln("Error while reading response:", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse := new(pasteResponse)
|
|
|
|
if err := json.Unmarshal(body, jsonResponse); err != nil {
|
2023-09-16 19:39:10 +02:00
|
|
|
app.Log.Errorln("Error while unmarshalling JSON response:", err)
|
2023-09-16 17:18:14 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
finalURL := hasteURL + "/" + jsonResponse.Key
|
|
|
|
|
|
|
|
return finalURL, nil
|
|
|
|
}
|