add uploads model, log downloads/uploads to the database

This commit is contained in:
lyx0 2023-10-05 23:11:11 +02:00
parent afd6488135
commit a33276759d
8 changed files with 157 additions and 48 deletions

View file

@ -84,16 +84,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
}
case "catbox":
go app.NewDownload("catbox", target, cmdParams[1])
go app.NewDownload("catbox", target, cmdParams[1], message)
case "kappa":
go app.NewDownload("kappa", target, cmdParams[1])
go app.NewDownload("kappa", target, cmdParams[1], message)
case "yaf":
go app.NewDownload("yaf", target, cmdParams[1])
go app.NewDownload("yaf", target, cmdParams[1], message)
case "gofile":
go app.NewDownload("gofile", target, cmdParams[1])
go app.NewDownload("gofile", target, cmdParams[1], message)
case "mail":
app.SendEmail("Test command used!", "This is an email test")

View file

@ -6,25 +6,37 @@ import (
"io"
"os"
"github.com/gempir/go-twitch-irc/v4"
"github.com/google/uuid"
"github.com/wader/goutubedl"
)
func (app *application) NewDownload(destination, target, link string) {
func (app *application) NewDownload(destination, target, link string, msg twitch.PrivateMessage) {
identifier := uuid.NewString()
go app.Models.Uploads.Insert(
msg.User.Name,
msg.User.ID,
msg.Channel,
msg.Message,
destination,
link,
identifier,
)
app.Send(target, "xd")
switch destination {
case "catbox":
app.CatboxDownload(target, link)
app.CatboxDownload(target, link, identifier)
case "yaf":
app.YafDownload(target, link)
app.YafDownload(target, link, identifier)
case "kappa":
app.KappaDownload(target, link)
app.KappaDownload(target, link, identifier)
case "gofile":
app.GofileDownload(target, link)
app.GofileDownload(target, link, identifier)
}
}
func (app *application) YafDownload(target, link string) {
func (app *application) YafDownload(target, link, identifier string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
@ -42,13 +54,7 @@ func (app *application) YafDownload(target, link string) {
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
fileName := fmt.Sprintf("%s.%s", identifier, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
@ -70,11 +76,11 @@ func (app *application) YafDownload(target, link string) {
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("yaf", fileName, target)
go app.NewUpload("yaf", fileName, target, identifier)
}
func (app *application) KappaDownload(target, link string) {
func (app *application) KappaDownload(target, link, identifier string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
@ -92,13 +98,7 @@ func (app *application) KappaDownload(target, link string) {
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName := fmt.Sprintf("%s.%s", uuidFilename, rExt)
fileName := fmt.Sprintf("%s.%s", identifier, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
@ -120,11 +120,11 @@ func (app *application) KappaDownload(target, link string) {
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("kappa", fileName, target)
go app.NewUpload("kappa", fileName, target, identifier)
}
func (app *application) GofileDownload(target, link string) {
func (app *application) GofileDownload(target, link, identifier string) {
goutubedl.Path = "yt-dlp"
app.Send(target, "Downloading... dankCircle")
@ -165,11 +165,11 @@ func (app *application) GofileDownload(target, link string) {
// dl.twitchClient.Say(target, "ResidentSleeper ..")
// time.Sleep(duration)
go app.NewUpload("gofile", fileName, target)
go app.NewUpload("gofile", fileName, target, identifier)
}
func (app *application) CatboxDownload(target, link string) {
func (app *application) CatboxDownload(target, link, identifier string) {
goutubedl.Path = "yt-dlp"
var fileName string
@ -194,13 +194,7 @@ func (app *application) CatboxDownload(target, link string) {
return
}
app.Send(target, "Downloaded.")
uuidFilename, err := uuid.NewUUID()
if err != nil {
app.Log.Errorln(err)
app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
return
}
fileName = fmt.Sprintf("%s.%s", uuidFilename, rExt)
fileName = fmt.Sprintf("%s.%s", identifier, rExt)
f, err := os.Create(fileName)
app.Send(target, fmt.Sprintf("Filename: %s", fileName))
@ -219,5 +213,5 @@ func (app *application) CatboxDownload(target, link string) {
downloadResult.Close()
f.Close()
go app.NewUpload("catbox", fileName, target)
go app.NewUpload("catbox", fileName, target, identifier)
}

View file

@ -44,7 +44,6 @@ type application struct {
}
var envFlag string
var fileUploaderURL = "https://i.yaf.ee/upload"
func init() {
flag.StringVar(&envFlag, "env", "dev", "database connection to use: (dev/prod)")

View file

@ -27,22 +27,22 @@ const (
YAF_ENDPOINT = "https://i.yaf.ee/upload"
)
func (app *application) NewUpload(destination, fileName, target string) {
func (app *application) NewUpload(destination, fileName, target, identifier string) {
switch destination {
case "catbox":
go app.CatboxUpload(target, fileName)
go app.CatboxUpload(target, fileName, identifier)
case "yaf":
go app.YafUpload(target, fileName)
go app.YafUpload(target, fileName, identifier)
case "kappa":
go app.KappaUpload(target, fileName)
go app.KappaUpload(target, fileName, identifier)
case "gofile":
go app.GofileUpload(target, fileName)
go app.GofileUpload(target, fileName, identifier)
}
}
func (app *application) CatboxUpload(target, fileName string) {
func (app *application) CatboxUpload(target, fileName, identifier string) {
defer os.Remove(fileName)
file, err := os.Open(fileName)
if err != nil {
@ -97,11 +97,12 @@ func (app *application) CatboxUpload(target, fileName string) {
}
reply := string(body)
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
app.Send(target, fmt.Sprintf("Removing file: %s", fileName))
app.Send(target, reply)
}
func (app *application) GofileUpload(target, path string) {
func (app *application) GofileUpload(target, path, identifier string) {
defer os.Remove(path)
app.Send(target, "Uploading to gofile.io... dankCircle")
pr, pw := io.Pipe()
@ -187,11 +188,12 @@ func (app *application) GofileUpload(target, path string) {
var reply = jsonResponse.Data.DownloadPage
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
app.Send(target, fmt.Sprintf("Removing file: %s", path))
app.Send(target, reply)
}
func (app *application) KappaUpload(target, path string) {
func (app *application) KappaUpload(target, path, identifier string) {
defer os.Remove(path)
app.Send(target, "Uploading to kappa.lol... dankCircle")
pr, pw := io.Pipe()
@ -274,10 +276,12 @@ func (app *application) KappaUpload(target, path string) {
var reply = jsonResponse.Link
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
app.Send(target, fmt.Sprintf("Removing file: %s", path))
app.Send(target, reply)
}
func (app *application) YafUpload(target, path string) {
func (app *application) YafUpload(target, path, identifier string) {
defer os.Remove(path)
app.Send(target, "Uploading to yaf.ee... dankCircle")
pr, pw := io.Pipe()
@ -348,6 +352,7 @@ func (app *application) YafUpload(target, path string) {
var reply = string(body[:])
go app.Models.Uploads.UpdateUploadURL(identifier, reply)
app.Send(target, fmt.Sprintf("Removing file: %s", path))
app.Send(target, reply)
}

View file

@ -53,6 +53,10 @@ type Models struct {
GetAll() ([]*Timer, error)
Delete(name string) error
}
Uploads interface {
Insert(twitchLogin, twitchID, twitchMessage, twitchChannel, filehoster, downloadURL, identifier string)
UpdateUploadURL(identifier, uploadURL string)
}
}
func NewModels(db *sql.DB) Models {
@ -61,5 +65,6 @@ func NewModels(db *sql.DB) Models {
Users: UserModel{DB: db},
Commands: CommandModel{DB: db},
Timers: TimerModel{DB: db},
Uploads: UploadModel{DB: db},
}
}

88
internal/data/uploads.go Normal file
View file

@ -0,0 +1,88 @@
package data
import (
"database/sql"
"errors"
"time"
)
type Upload struct {
ID int `json:"id"`
AddedAt time.Time `json:"-"`
TwitchLogin string `json:"twitchlogin"`
TwitchID string `json:"twitchid"`
TwitchChannel string `json:"twitchchannel"`
TwitchMessage string `json:"twitchmessage"`
Filehoster string `json:"filehoster"`
DownloadURL string `json:"downloadurl"`
UploadURL string `json:"uploadurl"`
Identifier string `json:"identifier"`
}
type UploadModel struct {
DB *sql.DB
}
// Insert takes in a channel struct and inserts it into the database.
func (u UploadModel) Insert(twitchLogin, twitchID, twitchChannel, twitchMessage, filehoster, downloadURL, identifier string) {
query := `
INSERT INTO uploads(twitchlogin, twitchid, twitchchannel, twitchmessage, filehoster, downloadurl, uploadurl, identifier)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, added_at, identifier;
`
args := []interface{}{
twitchLogin,
twitchID,
twitchChannel,
twitchMessage,
filehoster,
downloadURL,
"undefined",
identifier,
}
// Execute the query returning the number of affected rows.
result, err := u.DB.Exec(query, args...)
if err != nil {
return
}
// Check how many rows were affected.
rowsAffected, err := result.RowsAffected()
if err != nil {
return
}
if rowsAffected == 0 {
return
}
return
}
func (u UploadModel) UpdateUploadURL(identifier, uploadURL string) {
var id string
query := `
UPDATE uploads
SET uploadurl = $2
WHERE identifier = $1
RETURNING id`
args := []interface{}{
identifier,
uploadURL,
}
err := u.DB.QueryRow(query, args...).Scan(id)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return
default:
return
}
}
return
}

View file

@ -0,0 +1 @@
DROP TABLE IF EXISTS uploads;

View file

@ -0,0 +1,17 @@
CREATE TABLE IF NOT EXISTS uploads (
id bigserial PRIMARY KEY,
added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
twitchlogin text NOT NULL,
twitchid text NOT NULL,
twitchmessage text NOT NULL,
twitchchannel text NOT NULL,
filehoster text NOT NULL,
downloadurl text,
uploadurl text,
identifier text
);
INSERT INTO uploads (added_at,twitchlogin,twitchid,twitchchannel,twitchmessage,filehoster,downloadurl,uploadurl,identifier) VALUES
(NOW(),'nourylul','31437432','nourylul','()yaf https://www.youtube.com/watch?v=3rBFkwtaQbU','yaf','https://www.youtube.com/watch?v=3rBFkwtaQbU','https://i.yaf.ee/LEFuX.webm','a4af2284-4e13-46fa-9896-393bb1771a9d'),
(NOW(),'uudelleenkytkeytynyt','465178364','nourylul','()gofile https://www.youtube.com/watch?v=st6yupvNkVo','gofile','https://www.youtube.com/watch?v=st6yupvNkVo','https://gofile.io/d/PD1QNr','4ec952cc-42c0-41cd-9b07-637b4ec3c2b3');