rpaste/modules/paste/rpaste.go
2021-12-27 21:27:10 -07:00

62 lines
1.2 KiB
Go

// support for rpaste (bpaste)
package paste
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
var (
rpasteTimeMap = map[string]string{
"1hour": "1hour",
"1day": "1day",
"1week": "1week",
}
)
func Rpaste(life string, lexer string, fileContent string) error {
payload := map[string]interface{}{
"expiry": life,
"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 {
fmt.Println("Could not contact rpaste endpoint")
os.Exit(1)
}
if resp.StatusCode == 200 {
var response map[string]interface{}
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 {
fmt.Printf("ERROR: %d\n", resp.StatusCode)
}
return nil
}