rpaste/modules/paste/rpaste.go

71 lines
1.5 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-29 07:40:36 +00:00
baseURL = "https://rpa.st"
rpasteURL = "https://rpa.st/api/v1/paste"
2021-12-24 09:01:22 +00:00
)
2021-12-28 04:27:10 +00:00
func Rpaste(life string, lexer string, fileContent string) error {
2021-12-29 07:40:36 +00:00
// Payload to be pushed into json
2021-12-28 04:27:10 +00:00
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)
}
2021-12-29 07:40:36 +00:00
// Push in the paste
2021-12-28 04:27:10 +00:00
fmt.Println("Uploading...")
2021-12-29 07:40:36 +00:00
resp, err := http.Post(rpasteURL, "application/json", bytes.NewBuffer(payloadJson))
2021-12-28 04:27:10 +00:00
if err != nil {
2021-12-29 07:40:36 +00:00
fmt.Printf("Could not contact rpaste endpoint (%s)\n", rpasteURL)
2021-12-28 04:27:10 +00:00
os.Exit(1)
}
2021-12-29 07:40:36 +00:00
// Response is in json
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"])
2021-12-29 07:40:36 +00:00
fmt.Printf("Raw URL: %s/raw/%s\n", baseURL, slug)
2021-12-28 04:27:10 +00:00
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
}