From e63f016c287903f66101503007eaa43331d97911 Mon Sep 17 00:00:00 2001 From: odrling Date: Mon, 18 Nov 2024 11:20:49 +0100 Subject: [PATCH] server: set new kara context outside of db hooks --- server/model.go | 30 ++++++++++-------------------- server/s3.go | 27 ++++++++++++++++++++++++--- server/upload.go | 26 -------------------------- 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/server/model.go b/server/model.go index dd247b3b..e53ee80c 100644 --- a/server/model.go +++ b/server/model.go @@ -427,20 +427,18 @@ func (ki *KaraInfoDB) AfterUpdate(tx *gorm.DB) error { return err } - if ki.CurrentKaraInfoID == nil && CONFIG.Dakara.BaseURL != "" && ki.UploadInfo.VideoUploaded && ki.UploadInfo.SubtitlesUploaded { - SyncDakaraNotify() - } - if isNewKaraUpdate(tx) { - // NewKaraUpdate is set on the history instead of the actual value - // so we reconstruct the current value from the data - ki.ID = *ki.CurrentKaraInfoID - ki.CurrentKaraInfo = nil + if ki.CurrentKaraInfoID == nil { + if CONFIG.Dakara.BaseURL != "" && ki.UploadInfo.VideoUploaded && ki.UploadInfo.SubtitlesUploaded { + SyncDakaraNotify() + } - err = UploadHookGitlab(tx, ki) - if err != nil { - return err + if isNewKaraUpdate(tx) { + err = UploadHookGitlab(tx, ki) + if err != nil { + return err + } + go PostWebhooks(*ki) } - go PostWebhooks(*ki) } return nil } @@ -455,14 +453,6 @@ func (ki *KaraInfoDB) BeforeUpdate(tx *gorm.DB) error { return err } - // check for unix time 0 is for older karaokes, because we also used - // that at some point - if ki.VideoUploaded && ki.SubtitlesUploaded && - ki.KaraokeCreationTime.IsZero() || ki.KaraokeCreationTime.Unix() == 0 { - ki.KaraokeCreationTime = time.Now().UTC() - tx = WithNewKaraUpdate(tx) - } - // create historic entry with the current value orig_kara_info.ID = 0 orig_kara_info.CurrentKaraInfo = ki diff --git a/server/s3.go b/server/s3.go index 529d62f3..ef3afe66 100644 --- a/server/s3.go +++ b/server/s3.go @@ -135,9 +135,30 @@ func SaveFileToS3WithMetadata(ctx context.Context, tx *gorm.DB, fd io.Reader, ka return nil, err } - err = updateKaraokeAfterUpload(tx, kara, type_directory, filesize, crc32) - if err != nil { - return nil, err + currentTime := time.Now().UTC() + switch type_directory { + case "video": + kara.VideoUploaded = true + kara.VideoModTime = currentTime + kara.VideoSize = filesize + kara.VideoCRC32 = crc32 + case "inst": + kara.InstrumentalUploaded = true + kara.InstrumentalModTime = currentTime + kara.InstrumentalSize = filesize + kara.InstrumentalCRC32 = crc32 + case "sub": + kara.SubtitlesUploaded = true + kara.SubtitlesModTime = currentTime + kara.SubtitlesSize = filesize + kara.SubtitlesCRC32 = crc32 + } + // check for unix time 0 is for older karaokes, because we also used + // that at some point + if kara.VideoUploaded && kara.SubtitlesUploaded && + kara.KaraokeCreationTime.IsZero() || kara.KaraokeCreationTime.Unix() == 0 { + kara.KaraokeCreationTime = currentTime + tx = WithNewKaraUpdate(tx) } res, err := CheckKara(ctx, *kara) diff --git a/server/upload.go b/server/upload.go index 55c145ca..4e0a79b3 100644 --- a/server/upload.go +++ b/server/upload.go @@ -15,7 +15,6 @@ import ( "os" "strconv" "strings" - "time" "github.com/danielgtaylor/huma/v2" "github.com/gofiber/fiber/v2" @@ -151,31 +150,6 @@ type UploadOutput struct { } } -func updateKaraokeAfterUpload(tx *gorm.DB, kara *KaraInfoDB, filetype string, filesize int64, crc32 uint32) error { - currentTime := time.Now().UTC() - switch filetype { - case "video": - kara.VideoUploaded = true - kara.VideoModTime = currentTime - kara.VideoSize = filesize - kara.VideoCRC32 = crc32 - return nil - case "inst": - kara.InstrumentalUploaded = true - kara.InstrumentalModTime = currentTime - kara.InstrumentalSize = filesize - kara.InstrumentalCRC32 = crc32 - return nil - case "sub": - kara.SubtitlesUploaded = true - kara.SubtitlesModTime = currentTime - kara.SubtitlesSize = filesize - kara.SubtitlesCRC32 = crc32 - return nil - } - return errors.New("Unknown file type " + filetype) -} - func UploadKaraFile(ctx context.Context, input *UploadInput) (*UploadOutput, error) { db := GetDB(ctx) var err error