Skip to content

Commit

Permalink
server: set new kara context outside of db hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
odrling committed Nov 27, 2024
1 parent fddf48e commit e63f016
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
30 changes: 10 additions & 20 deletions server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
27 changes: 24 additions & 3 deletions server/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions server/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"os"
"strconv"
"strings"
"time"

"github.com/danielgtaylor/huma/v2"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e63f016

Please sign in to comment.