implement gofile upload

This commit is contained in:
lyx0 2024-01-10 21:05:06 +01:00
parent 7e46dd6f8a
commit 07c59b5811
3 changed files with 185 additions and 13 deletions

View file

@ -28,6 +28,9 @@ func (app *application) ParseCommand(evt *event.Event) {
case "xd":
app.SendText(evt, "xd !")
return
case "gofile":
app.NewDownload("gofile", evt, cmdParams[1])
return
case "yaf":
app.NewDownload("yaf", evt, cmdParams[1])
return

View file

@ -14,7 +14,6 @@ import (
const (
//CATBOX_ENDPOINT = "https://litterbox.catbox.moe/resources/internals/api.php"
//GOFILE_ENDPOINT = "https://store1.gofile.io/uploadFile"
//KAPPA_ENDPOINT = "https://kappa.lol/api/upload"
YAF_ENDPOINT = "https://i.yaf.li/upload"
)
@ -30,8 +29,8 @@ func (app *application) NewDownload(destination string, evt *event.Event, link s
// app.CatboxDownload(target, link, identifier, msg)
//case "kappa":
// app.KappaDownload(target, link, identifier, msg)
//case "gofile":
// app.GofileDownload(target, link, identifier, msg)
case "gofile":
app.GofileDownload(evt, link)
}
}
@ -75,3 +74,52 @@ func (app *application) YafDownload(evt *event.Event, link, uuid string) {
app.NewUpload("yaf", evt, fileName)
}
func (app *application) GofileDownload(evt *event.Event, link string) {
goutubedl.Path = "yt-dlp"
var rExt string
app.SendText(evt, "Downloading...")
result, err := goutubedl.New(context.Background(), link, goutubedl.Options{})
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
// For some reason youtube links return webm as result.Info.Ext but
// are in reality mp4.
if strings.HasPrefix(link, "https://www.youtube.com/") {
rExt = "mp4"
} else {
rExt = result.Info.Ext
}
safeFilename := fmt.Sprintf("download_%s", result.Info.Title)
downloadResult, err := result.Download(context.Background(), "best")
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", safeFilename, rExt)
f, err := os.Create(fileName)
//app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
defer f.Close()
if _, err = io.Copy(f, downloadResult); err != nil {
app.Log.Error().Err(err).Msg("Failed to download")
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
downloadResult.Close()
f.Close()
app.SendText(evt, fileName)
go app.NewUpload("gofile", evt, fileName)
}

View file

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
@ -19,8 +20,8 @@ func (app *application) NewUpload(destination string, evt *event.Event, fileName
// go app.CatboxUpload(target, fileName, identifier, msg)
//case "kappa":
// go app.KappaUpload(target, fileName, identifier, msg)
//case "gofile":
// go app.GofileUpload(target, fileName, identifier, msg)
case "gofile":
go app.GofileUpload(evt, fileName)
}
}
@ -100,3 +101,123 @@ func (app *application) YafUpload(evt *event.Event, fileName string) {
app.SendText(evt, reply)
}
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
}
func (app *application) GofileUpload(evt *event.Event, fileName string) {
defer os.Remove(fileName)
app.SendText(evt, "Uploading to gofile.io...")
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(fileName) // path to image file
if err != nil {
app.Log.Error().Err(err).Msg("Failed to upload")
os.Remove(fileName)
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
w, err := form.CreateFormFile("file", fileName)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to upload")
os.Remove(fileName)
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
_, err = io.Copy(w, file)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to upload")
os.Remove(fileName)
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
form.Close()
}()
gofileServer := app.GetGofileServer()
app.SendText(evt, gofileServer)
req, err := http.NewRequest(http.MethodPost, gofileServer, pr)
if err != nil {
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(fileName)
return
}
req.Header.Set("Content-Type", form.FormDataContentType())
httpClient := http.Client{
Timeout: 300 * time.Second,
}
resp, err := httpClient.Do(req)
if err != nil {
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(fileName)
app.Log.Error().Err(err).Msgf("Error while sending HTTP request: %s", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
app.Log.Error().Err(err).Msg("Failed to upload")
os.Remove(fileName)
app.SendText(evt, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
jsonResponse := new(gofileResponse)
if err := json.Unmarshal(body, jsonResponse); err != nil {
app.Log.Error().Err(err).Msg("Failed to upload")
os.Remove(fileName)
app.SendText(evt, fmt.Sprintf("Error unmarshalling json response: %q", err))
return
}
var reply = jsonResponse.Data.DownloadPage
app.SendText(evt, reply)
}