From a33276759d8cefc6d91031a58915c10f499a7909 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 5 Oct 2023 23:11:11 +0200 Subject: [PATCH] add uploads model, log downloads/uploads to the database --- cmd/nourybot/commands.go | 8 +- cmd/nourybot/download.go | 62 ++++++------- cmd/nourybot/main.go | 1 - cmd/nourybot/upload.go | 23 +++-- internal/data/models.go | 5 ++ internal/data/uploads.go | 88 +++++++++++++++++++ .../000005_create_uploads_table.down.sql | 1 + migrations/000005_create_uploads_table.up.sql | 17 ++++ 8 files changed, 157 insertions(+), 48 deletions(-) create mode 100644 internal/data/uploads.go create mode 100644 migrations/000005_create_uploads_table.down.sql create mode 100644 migrations/000005_create_uploads_table.up.sql diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index b1bdb9d..637f1b7 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -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") diff --git a/cmd/nourybot/download.go b/cmd/nourybot/download.go index 24b6782..bd700af 100644 --- a/cmd/nourybot/download.go +++ b/cmd/nourybot/download.go @@ -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) } diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index 3f4dec7..3324c14 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -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)") diff --git a/cmd/nourybot/upload.go b/cmd/nourybot/upload.go index 93485f6..c7cf43a 100644 --- a/cmd/nourybot/upload.go +++ b/cmd/nourybot/upload.go @@ -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) } diff --git a/internal/data/models.go b/internal/data/models.go index b48f0c7..d59658e 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -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}, } } diff --git a/internal/data/uploads.go b/internal/data/uploads.go new file mode 100644 index 0000000..db71ccd --- /dev/null +++ b/internal/data/uploads.go @@ -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 +} diff --git a/migrations/000005_create_uploads_table.down.sql b/migrations/000005_create_uploads_table.down.sql new file mode 100644 index 0000000..281baa8 --- /dev/null +++ b/migrations/000005_create_uploads_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS uploads; diff --git a/migrations/000005_create_uploads_table.up.sql b/migrations/000005_create_uploads_table.up.sql new file mode 100644 index 0000000..5396edc --- /dev/null +++ b/migrations/000005_create_uploads_table.up.sql @@ -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'); +