Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous #99

Merged
merged 12 commits into from
Jan 30, 2023
2 changes: 2 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:
cache-to: type=gha,scope=${{ env.IMAGE_NAME }},mode=max

build-push-arm64:
# Do not run on PRs
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: golangci-lint
on:
push:
tags:
- v*
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.19.5

- uses: actions/checkout@v3

- name: golangci-lint
uses: golangci/[email protected]
with:
version: latest
args: --timeout 3m
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"go.diagnostic.vulncheck": "Imports",
"go.lintTool": "golangci-lint"
// "go.lintFlags": ["--fast"]
}
7 changes: 5 additions & 2 deletions internal/admin/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package admin

import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/zibbp/ganymede/internal/kv"
"os/exec"
"strconv"
"time"

"github.com/labstack/echo/v4"
"github.com/zibbp/ganymede/internal/kv"
)

type InfoResp struct {
Expand Down Expand Up @@ -81,6 +82,8 @@ func getTwitchDownloaderVersion() (string, error) {
out, err := run.CombinedOutput()
if err != nil {
// TwitchDownloaderCLI throws exit status 1 on --version
// so we ignore the error
return string(out), nil
}
return string(out), nil
}
Expand Down
40 changes: 20 additions & 20 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Service) ArchiveTwitchChannel(cName string) (*ent.Channel, error) {

// Check if channel exists in DB
cCheck := s.ChannelService.CheckChannelExists(tChannel.Login)
if cCheck == true {
if cCheck {
return nil, fmt.Errorf("channel already exists")
}

Expand Down Expand Up @@ -100,12 +100,12 @@ func (s *Service) ArchiveTwitchVod(vID string, quality string, chat bool, render
if err != nil {
return nil, fmt.Errorf("error checking if vod exists: %v", err)
}
if vCheck == true {
if vCheck {
return nil, fmt.Errorf("vod already exists")
}
// Check if channel exists
cCheck := s.ChannelService.CheckChannelExists(tVod.UserLogin)
if cCheck == false {
if !cCheck {
log.Debug().Msgf("channel does not exist: %s while archiving vod. creating now.", tVod.UserLogin)
_, err := s.ArchiveTwitchChannel(tVod.UserLogin)
if err != nil {
Expand Down Expand Up @@ -133,14 +133,14 @@ func (s *Service) ArchiveTwitchVod(vID string, quality string, chat bool, render
fileName, err := GetFileName(vUUID, tVod)
if err != nil {
log.Error().Err(err).Msg("error using template to create file name, falling back to default")
fileName = fmt.Sprintf("%s", tVod.ID)
fileName = tVod.ID
}

// Sets
rootVodPath := fmt.Sprintf("/vods/%s/%s", tVod.UserLogin, folderName)
var chatPath string
var chatVideoPath string
if chat == true {
if chat {
chatPath = fmt.Sprintf("%s/%s-chat.json", rootVodPath, fileName)
chatVideoPath = fmt.Sprintf("%s/%s-chat.mp4", rootVodPath, fileName)
} else {
Expand Down Expand Up @@ -309,7 +309,7 @@ func (s *Service) ArchiveTwitchLive(lwc *ent.Live, ts twitch.Live) (*TwitchVodRe

// Check if channel exists
cCheck := s.ChannelService.CheckChannelExists(ts.UserLogin)
if cCheck == false {
if !cCheck {
log.Debug().Msgf("channel does not exist: %s while archiving live stream. creating now.", ts.UserLogin)
_, err := s.ArchiveTwitchChannel(ts.UserLogin)
if err != nil {
Expand Down Expand Up @@ -344,14 +344,14 @@ func (s *Service) ArchiveTwitchLive(lwc *ent.Live, ts twitch.Live) (*TwitchVodRe
fileName, err := GetFileName(vUUID, tVodDto)
if err != nil {
log.Error().Err(err).Msg("error using template to create file name, falling back to default")
fileName = fmt.Sprintf("%s", tVodDto.ID)
fileName = tVodDto.ID
}

// Sets
rootVodPath := fmt.Sprintf("/vods/%s/%s", ts.UserLogin, folderName)
var chatPath string
var chatVideoPath string
if lwc.ArchiveChat == true {
if lwc.ArchiveChat {
chatPath = fmt.Sprintf("%s/%s-chat.json", rootVodPath, fileName)
chatVideoPath = fmt.Sprintf("%s/%s-chat.mp4", rootVodPath, fileName)
} else {
Expand Down Expand Up @@ -448,13 +448,13 @@ func (s *Service) RestartTask(c echo.Context, qID uuid.UUID, task string, cont b
case "vod_create_folder":
go s.TaskVodCreateFolder(ch, v, q, cont)
case "vod_download_thumbnail":
if q.LiveArchive == true {
if q.LiveArchive {
go s.TaskVodDownloadLiveThumbnail(ch, v, q, cont)
} else {
go s.TaskVodDownloadThumbnail(ch, v, q, cont)
}
case "vod_save_info":
if q.LiveArchive == true {
if q.LiveArchive {
go s.TaskVodSaveLiveInfo(ch, v, q, cont)
} else {
go s.TaskVodSaveInfo(ch, v, q, cont)
Expand All @@ -472,7 +472,7 @@ func (s *Service) RestartTask(c echo.Context, qID uuid.UUID, task string, cont b
case "chat_render":
go s.TaskChatRender(ch, v, q, cont)
case "chat_move":
if q.LiveArchive == true {
if q.LiveArchive {
go s.TaskLiveChatMove(ch, v, q, cont)
} else {
go s.TaskChatMove(ch, v, q, cont)
Expand All @@ -497,8 +497,8 @@ func (s *Service) TaskVodCreateFolder(ch *ent.Channel, v *ent.Vod, q *ent.Queue,
}
q.Update().SetTaskVodCreateFolder(utils.Success).SaveX(context.Background())

if cont == true {
if q.LiveArchive == true {
if cont {
if q.LiveArchive {
go s.TaskVodDownloadLiveThumbnail(ch, v, q, true)
} else {
go s.TaskVodDownloadThumbnail(ch, v, q, true)
Expand Down Expand Up @@ -551,7 +551,7 @@ func (s *Service) TaskVodDownloadLiveThumbnail(ch *ent.Channel, v *ent.Vod, q *e
q.Update().SetTaskVodDownloadThumbnail(utils.Success).SaveX(context.Background())
}

if cont == true {
if cont {
// Refresh thumbnails for live stream after 30 minutes
go s.RefreshLiveThumbnails(ch, v, q)
// Proceed with task
Expand Down Expand Up @@ -604,7 +604,7 @@ func (s *Service) TaskVodDownloadThumbnail(ch *ent.Channel, v *ent.Vod, q *ent.Q

q.Update().SetTaskVodDownloadThumbnail(utils.Success).SaveX(context.Background())

if cont == true {
if cont {
go s.TaskVodSaveInfo(ch, v, q, true)
}
}
Expand Down Expand Up @@ -632,7 +632,7 @@ func (s *Service) TaskVodSaveLiveInfo(ch *ent.Channel, v *ent.Vod, q *ent.Queue,
return
}
q.Update().SetTaskVodSaveInfo(utils.Success).SaveX(context.Background())
if cont == true {
if cont {

busC := make(chan bool)

Expand Down Expand Up @@ -665,7 +665,7 @@ func (s *Service) TaskVodSaveInfo(ch *ent.Channel, v *ent.Vod, q *ent.Queue, con
return
}
q.Update().SetTaskVodSaveInfo(utils.Success).SaveX(context.Background())
if cont == true {
if cont {
go s.TaskVideoDownload(ch, v, q, true)
// Check if chat download task is set to success
if q.TaskChatDownload == utils.Pending {
Expand Down Expand Up @@ -814,7 +814,7 @@ func (s *Service) TaskVideoMove(ch *ent.Channel, v *ent.Vod, q *ent.Queue, cont
q.Update().SetVideoProcessing(false).SaveX(context.Background())

// Check if all task are done
if q.LiveArchive == true {
if q.LiveArchive {
go s.CheckIfLiveTasksAreDone(ch, v, q)
} else {
go s.CheckIfTasksAreDone(ch, v, q)
Expand All @@ -835,7 +835,7 @@ func (s *Service) TaskChatDownload(ch *ent.Channel, v *ent.Vod, q *ent.Queue, co

q.Update().SetTaskChatDownload(utils.Success).SaveX(context.Background())

if cont == true {
if cont {
go s.TaskChatRender(ch, v, q, true)
}
}
Expand Down Expand Up @@ -913,7 +913,7 @@ func (s *Service) TaskLiveChatConvert(ch *ent.Channel, v *ent.Vod, q *ent.Queue,
return
}

err = utils.ConvertTwitchLiveChatToVodChat(fmt.Sprintf("/tmp/%s_%s-live-chat.json", v.ExtID, v.ID), ch.Name, fmt.Sprintf("%s", v.ID), v.ExtID, cID, q.ChatStart)
err = utils.ConvertTwitchLiveChatToVodChat(fmt.Sprintf("/tmp/%s_%s-live-chat.json", v.ExtID, v.ID), ch.Name, v.ID.String(), v.ExtID, cID, q.ChatStart)
if err != nil {
log.Error().Err(err).Msg("error converting chat")
q.Update().SetTaskChatConvert(utils.Failed).SaveX(context.Background())
Expand Down
13 changes: 6 additions & 7 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package auth
import (
"context"
"fmt"
"os"
"strings"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
Expand All @@ -16,8 +19,6 @@ import (
"github.com/zibbp/ganymede/internal/utils"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
"os"
"strings"
)

type Service struct {
Expand Down Expand Up @@ -193,9 +194,7 @@ func (s *Service) OAuthUserCheck(c echo.Context, idTokenClaims UserInfo) error {
role := utils.Role("user")
// Check what groups the user is in
var groups []string
for _, group := range idTokenClaims.Groups {
groups = append(groups, group)
}
groups = append(groups, idTokenClaims.Groups...)
// If groups contain ganymede-*
if len(groups) > 0 {
for _, group := range groups {
Expand All @@ -214,7 +213,7 @@ func (s *Service) OAuthUserCheck(c echo.Context, idTokenClaims UserInfo) error {
}

// Create user
u, err = s.Store.Client.User.Create().SetSub(idTokenClaims.Sub).SetUsername(idTokenClaims.NickName).SetRole(utils.Role(role)).SetOauth(true).Save(c.Request().Context())
_, err = s.Store.Client.User.Create().SetSub(idTokenClaims.Sub).SetUsername(idTokenClaims.NickName).SetRole(utils.Role(role)).SetOauth(true).Save(c.Request().Context())
if err != nil {
return fmt.Errorf("failed to create user: %w", err)
}
Expand All @@ -223,7 +222,7 @@ func (s *Service) OAuthUserCheck(c echo.Context, idTokenClaims UserInfo) error {
}
} else {
// Update user
u, err = s.Store.Client.User.UpdateOne(u).SetUsername(idTokenClaims.NickName).Save(c.Request().Context())
_, err = s.Store.Client.User.UpdateOne(u).SetUsername(idTokenClaims.NickName).Save(c.Request().Context())
if err != nil {
return fmt.Errorf("failed to update user: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"

"github.com/MicahParks/keyfunc"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/golang-jwt/jwt/v4"
Expand All @@ -14,16 +20,10 @@ import (
"github.com/zibbp/ganymede/internal/kv"
"golang.org/x/oauth2"
"gopkg.in/square/go-jose.v2"
"io"
"net/http"
"os"
"strings"
"time"
)

type OAuthClaims struct {
jwt.RegisteredClaims
userInfo UserInfo
}

type UserInfo struct {
Expand Down
3 changes: 1 addition & 2 deletions internal/chat/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
Expand Down Expand Up @@ -112,7 +111,7 @@ func GetTwitchChannelEmotes(channelId int64) ([]*GanymedeEmote, error) {
return nil, fmt.Errorf("failed to get channel emotes: %v", resp)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
Expand Down
14 changes: 5 additions & 9 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ func RenderTwitchVodChat(v *ent.Vod, q *ent.Queue) (error, bool) {
// Generate args for exec
argArr := []string{"chatrender", "-i", fmt.Sprintf("/tmp/%s_%s-chat.json", v.ExtID, v.ID)}
// add each config param to arg
for _, v := range arr {
argArr = append(argArr, v)
}
argArr = append(argArr, arr...)
// add output file
argArr = append(argArr, "-o", fmt.Sprintf("/tmp/%s_%s-chat.mp4", v.ExtID, v.ID))
log.Debug().Msgf("chat render args: %v", argArr)
Expand Down Expand Up @@ -129,9 +127,7 @@ func ConvertTwitchVodVideo(v *ent.Vod) error {
// Generate args for exec
argArr := []string{"-y", "-hide_banner", "-i", fmt.Sprintf("/tmp/%s_%s-video.mp4", v.ExtID, v.ID)}
// add each config param to arg
for _, v := range arr {
argArr = append(argArr, v)
}
argArr = append(argArr, arr...)
// add output file
argArr = append(argArr, fmt.Sprintf("/tmp/%s_%s-video-convert.mp4", v.ExtID, v.ID))
log.Debug().Msgf("video convert args: %v", argArr)
Expand Down Expand Up @@ -227,8 +223,8 @@ func DownloadTwitchLiveVideo(v *ent.Vod, ch *ent.Channel) error {

if err := cmd.Run(); err != nil {
// Streamlink will error when the stream is offline - do not log this as an error
//log.Error().Err(err).Msg("error running streamlink for live video download")
//return err
log.Debug().Msgf("finished downloading live video for %s", v.ExtID)
return nil
}

log.Debug().Msgf("finished downloading live video for %s", v.ExtID)
Expand Down Expand Up @@ -296,7 +292,7 @@ func GetVideoDuration(path string) (int, error) {
return 1, err
}
durOut := strings.TrimSpace(string(out))
durFloat, err := strconv.ParseFloat(durOut, 8)
durFloat, err := strconv.ParseFloat(durOut, 64)
if err != nil {
log.Error().Err(err).Msg("error converting video duration")
return 1, err
Expand Down
4 changes: 2 additions & 2 deletions internal/live/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *Service) Check() error {
// Check if LWC is in twitchStreams.Data
stream := stringInSlice(lwc.Edges.Channel.Name, twitchStreams.Data)
if len(stream.ID) > 0 {
if lwc.IsLive == false {
if !lwc.IsLive {
log.Debug().Msgf("%s is now live", lwc.Edges.Channel.Name)
// Stream is online, update database
_, err := s.Store.Client.Live.UpdateOneID(lwc.ID).SetIsLive(true).Save(context.Background())
Expand All @@ -154,7 +154,7 @@ func (s *Service) Check() error {
go notification.SendLiveNotification(lwc.Edges.Channel, archiveResp.VOD, archiveResp.Queue)
}
} else {
if lwc.IsLive == true {
if lwc.IsLive {
log.Debug().Msgf("%s is now offline", lwc.Edges.Channel.Name)
// Stream is offline, update database
_, err := s.Store.Client.Live.UpdateOneID(lwc.ID).SetIsLive(false).SetLastLive(time.Now()).Save(context.Background())
Expand Down
Loading