2025-02-27 10:17:37 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2025-02-27 13:14:51 -03:00
|
|
|
"bytes"
|
2025-02-27 10:17:37 -03:00
|
|
|
"fmt"
|
2025-02-27 13:14:51 -03:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2025-02-27 14:02:54 -03:00
|
|
|
"net/url"
|
2025-02-27 13:14:51 -03:00
|
|
|
"os"
|
|
|
|
"path"
|
2025-02-27 13:54:44 -03:00
|
|
|
"strings"
|
2025-02-27 10:17:37 -03:00
|
|
|
|
2025-02-27 13:58:54 -03:00
|
|
|
"github.com/atotto/clipboard"
|
|
|
|
)
|
2025-02-27 13:14:51 -03:00
|
|
|
|
2025-02-27 13:58:54 -03:00
|
|
|
var pasteURL = os.Getenv("PASTEBIN_URL")
|
2025-02-27 13:14:51 -03:00
|
|
|
var key = os.Getenv("AUTH_KEY")
|
2025-02-27 14:11:19 -03:00
|
|
|
var authParam = os.Getenv("AUTH_PARAM")
|
2025-02-27 13:14:51 -03:00
|
|
|
|
2025-02-27 10:17:37 -03:00
|
|
|
func main() {
|
2025-02-27 14:11:19 -03:00
|
|
|
if pasteURL == "" {
|
|
|
|
fmt.Println("Please set PASTEBIN_URL environment variable")
|
2025-02-27 13:58:54 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-02-27 13:54:44 -03:00
|
|
|
file := strings.Split(SelectFile(), "file://")[1]
|
2025-02-27 14:02:54 -03:00
|
|
|
output, _ := url.QueryUnescape(file)
|
|
|
|
|
|
|
|
request, err := uploadFile(output)
|
2025-02-27 13:14:51 -03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating request:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
res, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error executing request:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
respBody, _ := io.ReadAll(res.Body)
|
2025-02-27 13:58:54 -03:00
|
|
|
|
|
|
|
if err := clipboard.WriteAll(string(respBody)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2025-02-27 11:09:14 -03:00
|
|
|
}
|
|
|
|
|
2025-02-27 13:14:51 -03:00
|
|
|
func uploadFile(file string) (*http.Request, error) {
|
|
|
|
// open the file
|
|
|
|
body, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open file: %v", err)
|
|
|
|
}
|
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
// prepare multipart form data
|
|
|
|
data := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(data)
|
|
|
|
|
|
|
|
// create the form file part
|
|
|
|
part, err := writer.CreateFormFile("file", path.Base(file))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating form file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy file content to form data
|
|
|
|
_, err = io.Copy(part, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error copying file content: %v", err)
|
|
|
|
}
|
2025-02-27 13:54:44 -03:00
|
|
|
writer.Close()
|
2025-02-27 13:14:51 -03:00
|
|
|
|
|
|
|
// create the HTTP request
|
|
|
|
req, err := http.NewRequest("POST", pasteURL, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set headers
|
2025-02-27 14:11:19 -03:00
|
|
|
req.Header.Set(authParam, key)
|
2025-02-27 13:14:51 -03:00
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
|
|
|
|
return req, nil
|
2025-02-27 10:17:37 -03:00
|
|
|
}
|