rpaste/modules/paste/fpaste.go

86 lines
2.2 KiB
Go
Raw Normal View History

2021-12-24 09:01:22 +00:00
// support for fpaste
package paste
2021-12-29 00:52:28 +00:00
import (
2021-12-29 07:40:36 +00:00
"bytes"
2021-12-29 00:52:28 +00:00
"fmt"
2021-12-29 07:40:36 +00:00
"io"
"net/http"
"net/url"
"os"
2021-12-29 00:52:28 +00:00
"os/user"
2021-12-29 07:40:36 +00:00
"strings"
2021-12-29 00:52:28 +00:00
)
2021-12-24 09:01:22 +00:00
var (
2021-12-29 07:40:36 +00:00
// paste.centos.org does things in minutes
2021-12-24 09:01:22 +00:00
fpasteTimeMap = map[string]string{
2021-12-29 00:52:28 +00:00
"1hour": "60",
"1day": "1440",
"1week": "10080",
2021-12-24 09:01:22 +00:00
}
2021-12-29 07:40:36 +00:00
rawSlug = "view/raw"
// I'll ask the maintainer of the paste site if I could potentially obtain
// our own API key instead of using the one from the fpaste script
fpasteBaseURL = "https://paste.centos.org"
2021-12-29 00:52:28 +00:00
fpasteAPIKey = "5uZ30dTZE1a5V0WYhNwcMddBRDpk6UzuzMu-APKM38iMHacxdA0n4vCqA34avNyt"
2021-12-29 07:40:36 +00:00
fpasteURL = "https://paste.centos.org/api/create"
fpasteUserAgent = "rpaste/0.2.0"
2021-12-29 00:52:28 +00:00
fpasteTitle = "UNTITLED" // we'll need to make this changeable in the future
fpastePrivate = "1"
fpasteUserName = "user"
2021-12-24 09:01:22 +00:00
)
2021-12-29 00:52:28 +00:00
func Fpaste(life string, lexer string, fileContent string) error {
// Set username as the author
user, err := user.Current()
if err != nil {
fmt.Println("Username could not be found")
} else {
fpasteUserName = user.Name
}
2021-12-29 07:40:36 +00:00
// Stikkit doesn't seem to take in json data. It takes in form data and
// general headers.
form := url.Values{}
form.Add("title", fpasteTitle)
form.Add("expire", fpasteTimeMap[life])
form.Add("private", fpastePrivate)
form.Add("lang", lexer)
form.Add("name", fpasteUserName)
form.Add("text", fileContent)
rurl, err := url.Parse(fpasteURL)
if err != nil {
fmt.Println("Failed to construct URL")
os.Exit(1)
}
// This helps us do a proper urlencode for the API key, instead of just
// stashing it in the var itself.
vs := rurl.Query()
vs.Add("apikey", fpasteAPIKey)
rurl.RawQuery = vs.Encode()
fmt.Println("Uploading...")
resp, err := http.PostForm(rurl.String(), form)
// The response comes back as bytes, so we copy the response buffer into io
// and string it out
buf := &bytes.Buffer{}
io.Copy(buf, resp.Body)
pasteURL := buf.String()
if resp.StatusCode == 200 {
urlish := strings.Replace(pasteURL, "\n", "", -1)
ss := strings.Split(urlish, "/")
slug := ss[len(ss)-1]
fmt.Printf("Paste URL: %s", pasteURL)
fmt.Printf("Raw URL: %s/%s/%s\n", fpasteBaseURL, rawSlug, slug)
} else {
fmt.Printf("%s", http.StatusText(resp.StatusCode))
}
2021-12-29 00:52:28 +00:00
return nil
}