rpaste/modules/paste/rpaste.go

66 lines
1.4 KiB
Go
Raw Normal View History

2021-12-21 01:06:08 +00:00
// support for rpaste (bpaste)
package paste
2021-12-24 09:01:22 +00:00
2021-12-28 04:27:10 +00:00
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
2021-12-24 09:01:22 +00:00
var (
rpasteTimeMap = map[string]string{
"1hour": "1hour",
"1day": "1day",
"1week": "1week",
}
)
2021-12-28 04:27:10 +00:00
func Rpaste(life string, lexer string, fileContent string) error {
payload := map[string]interface{}{
2021-12-29 00:52:28 +00:00
"expiry": rpasteTimeMap[life],
2021-12-28 04:27:10 +00:00
"files": []map[string]string{
{
"name": "paste",
"lexer": lexer,
"content": PasteData,
},
},
}
payloadJson, err := json.Marshal(payload)
if err != nil {
fmt.Println("Could not marshal json")
os.Exit(1)
}
fmt.Println("Uploading...")
resp, err := http.Post("https://rpa.st/api/v1/paste", "application/json", bytes.NewBuffer(payloadJson))
if err != nil {
2021-12-29 00:52:28 +00:00
fmt.Println("Could not contact rpaste endpoint (https://rpa.st/api/v1)")
2021-12-28 04:27:10 +00:00
os.Exit(1)
}
2021-12-29 00:52:28 +00:00
var response map[string]interface{}
2021-12-28 04:27:10 +00:00
if resp.StatusCode == 200 {
json.NewDecoder(resp.Body).Decode(&response)
url := fmt.Sprintf("%s", response["link"])
ss := strings.Split(url, "/")
slug := ss[len(ss)-1]
fmt.Printf("Paste URL: %s\n", response["link"])
fmt.Printf("Raw URL: https://rpa.st/raw/%s\n", slug)
fmt.Printf("Removal URL: %s\n", response["removal"])
} else {
2021-12-29 00:52:28 +00:00
json.NewDecoder(resp.Body).Decode(&response)
fmt.Printf("ERROR: %d, %s\n", resp.StatusCode, response["message"])
if resp.StatusCode == 500 {
fmt.Println("You cannot upload binary data.")
}
2021-12-28 04:27:10 +00:00
}
return nil
}