Skip to content

Commit

Permalink
server: export new karaokes as they get uploaded
Browse files Browse the repository at this point in the history
  • Loading branch information
odrling committed Nov 17, 2024
1 parent 20fa66f commit afcc586
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 13 deletions.
2 changes: 1 addition & 1 deletion server/karaberus.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func addRoutes(api huma.API) {
Security: kara_ro_security,
}, DownloadHead)
huma.Get(api, "/api/kara/{id}/download/{filetype}", DownloadFile, setSecurity(kara_ro_basic_security))
huma.Get(api, "/api/kara/{id}/mugen/export", MugenExport, setSecurity(kara_admin_security))
huma.Get(api, "/api/kara/{id}/mugen/export", MugenExportKara, setSecurity(kara_admin_security))

huma.Get(api, "/api/font", GetAllFonts, setSecurity(kara_ro_security))
huma.Post(api, "/api/font", UploadFont, setSecurity(kara_security))
Expand Down
4 changes: 4 additions & 0 deletions server/karaenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type KaraberusMugenGitlabConfig struct {
IssueLabels []string `envkey:"LABELS" separator:"," default:"To Add"`
}

func (conf KaraberusMugenGitlabConfig) IsSetup() bool {
return conf.ProjectID != "" && conf.ClientID != "" && conf.ClientSecret != ""
}

type BasicAuthConfig struct {
Username string `envkey:"USERNAME"`
Password string `envkey:"PASSWORD"`
Expand Down
52 changes: 52 additions & 0 deletions server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,39 @@ func isAssociationsUpdate(tx *gorm.DB) bool {
return tx.Statement.Context.Value(UpdateAssociations{}) != nil
}

func UploadHookGitlab(tx *gorm.DB, ki *KaraInfoDB) error {
if CONFIG.Mugen.Gitlab.IsSetup() {
// check if kara is an import
mugen_import := &MugenImport{}
err := tx.Where(&MugenImport{KaraID: ki.ID}).First(mugen_import).Error
if err == nil {
// kara was imported
return nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}

// check if kara is already exported
mugen_export := &MugenExport{}
err = tx.Where(&MugenExport{KaraID: ki.ID}).First(&mugen_export).Error
if err == nil {
// already exported
return nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}

err = createGitlabIssue(tx.Statement.Context, tx, *ki, mugen_export)
if err != nil {
return err
}
}

return nil
}

func (ki *KaraInfoDB) AfterUpdate(tx *gorm.DB) error {
// update kara just in case
err := tx.First(&ki).Error
Expand All @@ -366,6 +399,12 @@ func (ki *KaraInfoDB) AfterUpdate(tx *gorm.DB) error {
if ki.CurrentKaraInfoID == nil && CONFIG.Dakara.BaseURL != "" && ki.UploadInfo.VideoUploaded && ki.UploadInfo.SubtitlesUploaded {
SyncDakaraNotify()
}
if !ki.KaraokeCreationTime.IsZero() {
err = UploadHookGitlab(tx, ki)
if err != nil {
return err
}
}
return nil
}

Expand All @@ -379,6 +418,13 @@ 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()
}

// create historic entry with the current value
orig_kara_info.ID = 0
orig_kara_info.CurrentKaraInfo = ki
Expand Down Expand Up @@ -413,6 +459,12 @@ type MugenImport struct {
Kara KaraInfoDB `gorm:"foreignKey:KaraID;references:ID;constraint:OnDelete:CASCADE"`
}

type MugenExport struct {
KaraID uint `gorm:"primarykey" json:"kid"`
Kara KaraInfoDB `gorm:"foreignKey:KaraID;references:ID;constraint:OnDelete:CASCADE" json:"kara"`
GitlabIssue uint `json:"gitlab_issue"`
}

func (k KaraInfoDB) getAudioTags() ([]AudioTag, error) {
audio_tags := make([]AudioTag, len(k.AudioTags))

Expand Down
40 changes: 33 additions & 7 deletions server/mugen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/Japan7/karaberus/server/clients/mugen"
"github.com/danielgtaylor/huma/v2"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"golang.org/x/sync/semaphore"
Expand Down Expand Up @@ -532,7 +533,7 @@ type GitlabIssuesInput struct {
}

type GitlabIssuesResponse struct {
ID int `json:"id"`
ID uint `json:"id"`
}

func karaDescriptionPart(name string, value string) string {
Expand Down Expand Up @@ -632,7 +633,7 @@ func karaDescription(k KaraInfoDB) (string, error) {
return strings.Join(parts, "\n\n"), nil
}

func createGitlabIssue(ctx context.Context, db *gorm.DB, kara KaraInfoDB, issue_resp *GitlabIssuesResponse) error {
func createGitlabIssue(ctx context.Context, db *gorm.DB, kara KaraInfoDB, mugen_export *MugenExport) error {
token := &OAuthToken{}
err := getGitlabToken(db, token)
if err != nil {
Expand Down Expand Up @@ -681,8 +682,17 @@ func createGitlabIssue(ctx context.Context, db *gorm.DB, kara KaraInfoDB, issue_
return fmt.Errorf("gitlab responded with status code %d", resp.StatusCode)
}

issue_resp := &GitlabIssuesResponse{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(issue_resp)
if err != nil {
return err
}

mugen_export.KaraID = kara.ID
mugen_export.GitlabIssue = issue_resp.ID

err = db.Create(mugen_export).Error
return err
}

Expand All @@ -691,21 +701,37 @@ type MugenExportInput struct {
}

type MugenExportOutput struct {
Body struct {
Issue GitlabIssuesResponse `json:"issue"`
}
Body MugenExport
}

func MugenExport(ctx context.Context, input *MugenExportInput) (*MugenExportOutput, error) {
func MugenExportKara(ctx context.Context, input *MugenExportInput) (*MugenExportOutput, error) {
db := GetDB(ctx)
kara, err := GetKaraByID(db, input.ID)
if err != nil {
return nil, DBErrToHumaErr(err)
}

// check if kara is an import
mugen_import := &MugenImport{}
err = db.Where(&MugenImport{KaraID: kara.ID}).First(mugen_import).Error
if err == nil {
return nil, huma.Error409Conflict(fmt.Sprintf("kara %d is an import", kara.ID))
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}

// check if kara is already exported
out := &MugenExportOutput{}
err = createGitlabIssue(ctx, db, kara, &out.Body.Issue)
err = db.Where(&MugenExport{KaraID: kara.ID}).First(&out.Body).Error
if err == nil {
return nil, huma.Error409Conflict(fmt.Sprintf("kara %d is already exported", kara.ID))
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}

err = createGitlabIssue(ctx, db, kara, &out.Body)
if err != nil {
return nil, err
}
Expand Down
5 changes: 0 additions & 5 deletions server/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ func updateKaraokeAfterUpload(tx *gorm.DB, kara *KaraInfoDB, filetype string, fi
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.KaraokeCreationTime.IsZero() || kara.KaraokeCreationTime.Unix() == 0 {
kara.KaraokeCreationTime = currentTime
}
return nil
}
return errors.New("Unknown file type " + filetype)
Expand Down

0 comments on commit afcc586

Please sign in to comment.