2023-10-05 19:33:07 +02:00
|
|
|
// The whole catbox upload functionality has been copied from
|
|
|
|
// here so that I could use it with litterbox:
|
|
|
|
// https://github.com/wabarc/go-catbox/blob/main/catbox.go <3
|
|
|
|
//
|
|
|
|
// Copyright 2021 Wayback Archiver. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2023-10-05 21:21:26 +02:00
|
|
|
package main
|
2023-10-05 19:33:07 +02:00
|
|
|
|
|
|
|
import (
|
2023-12-13 22:40:48 +01:00
|
|
|
"bufio"
|
2023-10-05 20:21:23 +02:00
|
|
|
"encoding/json"
|
2023-10-05 19:33:07 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2023-10-10 17:51:12 +02:00
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v4"
|
2023-12-13 22:46:06 +01:00
|
|
|
"github.com/google/uuid"
|
2023-10-05 19:33:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CATBOX_ENDPOINT = "https://litterbox.catbox.moe/resources/internals/api.php"
|
2023-10-05 20:21:23 +02:00
|
|
|
GOFILE_ENDPOINT = "https://store1.gofile.io/uploadFile"
|
|
|
|
KAPPA_ENDPOINT = "https://kappa.lol/api/upload"
|
2023-11-12 12:39:05 +01:00
|
|
|
YAF_ENDPOINT = "https://i.yaf.li/upload"
|
2023-10-05 19:33:07 +02:00
|
|
|
)
|
|
|
|
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) NewUpload(destination, fileName, target, identifier string, msg twitch.PrivateMessage) {
|
2023-10-05 19:33:07 +02:00
|
|
|
|
|
|
|
switch destination {
|
|
|
|
case "catbox":
|
2023-10-10 17:51:12 +02:00
|
|
|
go app.CatboxUpload(target, fileName, identifier, msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
case "yaf":
|
2023-10-10 17:51:12 +02:00
|
|
|
go app.YafUpload(target, fileName, identifier, msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
case "kappa":
|
2023-10-10 17:51:12 +02:00
|
|
|
go app.KappaUpload(target, fileName, identifier, msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
case "gofile":
|
2023-10-10 17:51:12 +02:00
|
|
|
go app.GofileUpload(target, fileName, identifier, msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// CatboxUpload uplodas the given file to catbox.moe.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) CatboxUpload(target, fileName, identifier string, msg twitch.PrivateMessage) {
|
2023-10-05 19:33:07 +02:00
|
|
|
defer os.Remove(fileName)
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, "Uploading to catbox.moe... dankCircle", msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
m := multipart.NewWriter(w)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer m.Close()
|
|
|
|
|
2023-10-10 18:03:13 +02:00
|
|
|
err := m.WriteField("reqtype", "fileupload")
|
|
|
|
if err != nil {
|
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = m.WriteField("time", "24h")
|
|
|
|
if err != nil {
|
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 19:33:07 +02:00
|
|
|
part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name()))
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.Copy(part, file); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, _ := http.NewRequest(http.MethodPost, CATBOX_ENDPOINT, r)
|
|
|
|
req.Header.Add("Content-Type", m.FormDataContentType())
|
|
|
|
|
2023-10-05 21:21:26 +02:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2023-10-05 19:33:07 +02:00
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2023-12-13 22:40:48 +01:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2023-10-05 19:33:07 +02:00
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := string(body)
|
2023-10-05 23:11:11 +02:00
|
|
|
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
|
2023-12-03 15:58:33 +01:00
|
|
|
//app.Send(target, fmt.Sprintf("Removing file: %s", fileName), msg)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, reply, msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// GetGofileServer queries to gofile api for the server name to upload to
|
2024-01-16 16:42:33 +01:00
|
|
|
func (app *application) GetGofileServer() string {
|
|
|
|
type gofileData struct {
|
|
|
|
Server string `json:"server"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gofileResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Data gofileData
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := http.Get("https://api.gofile.io/getServer")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
responseData, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
var responseObject gofileResponse
|
|
|
|
if err = json.Unmarshal(responseData, &responseObject); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadServer := fmt.Sprintf("https://%s.gofile.io/uploadFile", responseObject.Data.Server)
|
|
|
|
return uploadServer
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// GofileUpload uplodas the given file to gofile.io.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) GofileUpload(target, path, identifier string, msg twitch.PrivateMessage) {
|
2023-10-05 19:33:07 +02:00
|
|
|
defer os.Remove(path)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, "Uploading to gofile.io... dankCircle", msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
form := multipart.NewWriter(pw)
|
|
|
|
|
|
|
|
type gofileData struct {
|
|
|
|
DownloadPage string `json:"downloadPage"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
ParentFolder string `json:"parentFolder"`
|
|
|
|
FileId string `json:"fileId"`
|
|
|
|
FileName string `json:"fileName"`
|
|
|
|
Md5 string `json:"md5"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gofileResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Data gofileData
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer pw.Close()
|
|
|
|
|
|
|
|
file, err := os.Open(path) // path to image file
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := form.CreateFormFile("file", path)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
form.Close()
|
|
|
|
}()
|
|
|
|
|
2024-01-16 16:42:33 +01:00
|
|
|
gofileServer := app.GetGofileServer()
|
|
|
|
req, err := http.NewRequest(http.MethodPost, gofileServer, pr)
|
2023-10-05 20:21:23 +02:00
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", form.FormDataContentType())
|
|
|
|
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while sending HTTP request:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-12-03 15:58:33 +01:00
|
|
|
//app.Send(target, "Uploaded PogChamp", msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while reading response:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse := new(gofileResponse)
|
|
|
|
if err := json.Unmarshal(body, jsonResponse); err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while unmarshalling JSON response:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply = jsonResponse.Data.DownloadPage
|
|
|
|
|
2023-10-05 23:11:11 +02:00
|
|
|
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
|
2023-12-03 15:58:33 +01:00
|
|
|
//app.Send(target, fmt.Sprintf("Removing file: %s", path), msg)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, reply, msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
}
|
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// KappaUpload uplodas the given file to kappa.lol.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) KappaUpload(target, path, identifier string, msg twitch.PrivateMessage) {
|
2023-10-05 20:21:23 +02:00
|
|
|
defer os.Remove(path)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, "Uploading to kappa.lol... dankCircle", msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
form := multipart.NewWriter(pw)
|
|
|
|
|
|
|
|
type kappaResponse struct {
|
|
|
|
Link string `json:"link"`
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer pw.Close()
|
|
|
|
|
|
|
|
err := form.WriteField("name", "xd")
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(path) // path to image file
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := form.CreateFormFile("file", path)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
form.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, KAPPA_ENDPOINT, pr)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", form.FormDataContentType())
|
|
|
|
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while sending HTTP request:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-12-03 15:58:33 +01:00
|
|
|
//app.Send(target, "Uploaded PogChamp", msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while reading response:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse := new(kappaResponse)
|
|
|
|
if err := json.Unmarshal(body, jsonResponse); err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while unmarshalling JSON response:", err)
|
2023-10-05 20:21:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply = jsonResponse.Link
|
|
|
|
|
2023-10-05 23:11:11 +02:00
|
|
|
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
|
2023-12-03 15:58:33 +01:00
|
|
|
//app.Send(target, fmt.Sprintf("Removing file: %s", path), msg)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, reply, msg)
|
2023-10-05 20:21:23 +02:00
|
|
|
}
|
2023-10-05 23:11:11 +02:00
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// YafUpload uplodas the given file to yaf.li.
|
2023-10-10 17:51:12 +02:00
|
|
|
func (app *application) YafUpload(target, path, identifier string, msg twitch.PrivateMessage) {
|
2023-10-05 20:21:23 +02:00
|
|
|
defer os.Remove(path)
|
2023-11-12 12:39:05 +01:00
|
|
|
app.Send(target, "Uploading to yaf.li... dankCircle", msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
form := multipart.NewWriter(pw)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer pw.Close()
|
|
|
|
|
|
|
|
err := form.WriteField("name", "xd")
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(path) // path to image file
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := form.CreateFormFile("file", path)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
form.Close()
|
|
|
|
}()
|
|
|
|
|
2023-10-05 20:21:23 +02:00
|
|
|
req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr)
|
2023-10-05 19:33:07 +02:00
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", form.FormDataContentType())
|
|
|
|
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while sending HTTP request:", err)
|
2023-10-05 19:33:07 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
os.Remove(path)
|
2023-10-05 21:21:26 +02:00
|
|
|
app.Log.Errorln("Error while reading response:", err)
|
2023-10-05 19:33:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply = string(body[:])
|
|
|
|
|
2023-10-05 23:11:11 +02:00
|
|
|
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
|
2023-10-10 17:51:12 +02:00
|
|
|
app.Send(target, reply, msg)
|
2023-10-05 19:33:07 +02:00
|
|
|
}
|
2023-12-13 22:40:48 +01:00
|
|
|
|
2024-02-28 11:53:39 +01:00
|
|
|
// YafUploadString uplodas text to yaf.li.
|
2023-12-13 22:40:48 +01:00
|
|
|
func (app *application) YafUploadString(text string) string {
|
2023-12-13 22:46:06 +01:00
|
|
|
path := uuid.NewString()
|
|
|
|
app.Log.Info(path)
|
2023-12-13 22:40:48 +01:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error("Error creating file:", err)
|
|
|
|
os.Remove(path)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
defer os.Remove(path)
|
|
|
|
// Create a buffered writer to efficiently write to the file
|
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
|
|
|
|
// Write the content (string) to the file
|
|
|
|
_, err = writer.WriteString(text)
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error("Error writing to file:", err)
|
|
|
|
os.Remove(path)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the writer to ensure all buffered operations have been applied to the file
|
|
|
|
err = writer.Flush()
|
|
|
|
if err != nil {
|
|
|
|
app.Log.Error("Error flushing writer:", err)
|
|
|
|
os.Remove(path)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
form := multipart.NewWriter(pw)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer pw.Close()
|
|
|
|
|
|
|
|
err := form.WriteField("name", "xd")
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(path) // path to image file
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := form.CreateFormFile("file", path)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
form.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", form.FormDataContentType())
|
|
|
|
|
|
|
|
httpClient := http.Client{
|
|
|
|
Timeout: 300 * time.Second,
|
|
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
app.Log.Errorln("Error while sending HTTP request:", err)
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(path)
|
|
|
|
app.Log.Errorln("Error while reading response:", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var reply = string(body[:])
|
|
|
|
|
|
|
|
return reply
|
|
|
|
}
|