rpaste/modules/paste/rpaste.go

71 lines
1.5 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",
}
baseURL = "https://rpa.st"
rpasteURL = "https://rpa.st/api/v1/paste"
)
func Rpaste(life string, lexer string, fileContent string) error {
// Payload to be pushed into json
payload := map[string]interface{}{
"expiry": rpasteTimeMap[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)
}
// Push in the paste
fmt.Println("Uploading...")
resp, err := http.Post(rpasteURL, "application/json", bytes.NewBuffer(payloadJson))
if err != nil {
fmt.Printf("Could not contact rpaste endpoint (%s)\n", rpasteURL)
os.Exit(1)
}
// Response is in json
var response map[string]interface{}
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: %s/raw/%s\n", baseURL, slug)
fmt.Printf("Removal URL: %s\n", response["removal"])
} else {
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.")
}
}
return nil
}