code cleanup

This commit is contained in:
stefanodvx 2025-04-24 12:20:35 +02:00
parent 917be1687f
commit 5336968e05
34 changed files with 193 additions and 115 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/pkg/errors"
)
func HandleDefaultFormatDownload(
@ -59,7 +60,7 @@ func HandleDefaultFormatDownload(
}
if len(medias) == 0 {
return fmt.Errorf("no formats downloaded")
return errors.New("no formats downloaded")
}
isCaptionEnabled := true

View file

@ -11,6 +11,8 @@ import (
"govd/enums"
"govd/models"
"govd/util"
"github.com/pkg/errors"
)
func downloadMediaItem(
@ -25,7 +27,7 @@ func downloadMediaItem(
format := media.Format
if format == nil {
return nil, fmt.Errorf("media format is nil")
return nil, errors.New("media format is nil")
}
fileName := format.GetFileName()
@ -79,7 +81,7 @@ func downloadMediaItem(
}
if format.Type == enums.MediaTypeVideo || format.Type == enums.MediaTypeAudio {
path, err := getFileThumbnail(format, filePath)
path, err := getFileThumbnail(ctx, format, filePath)
if err != nil {
return nil, fmt.Errorf("failed to get thumbnail: %w", err)
}

View file

@ -100,8 +100,7 @@ func HandleInline(
return util.ErrInlineMediaGroup
}
err = HandleInlineCached(
bot, ctx,
dlCtx, cached[0],
bot, ctx, cached[0],
)
if err != nil {
return err
@ -118,7 +117,6 @@ func HandleInline(
func HandleInlineCached(
bot *gotgbot.Bot,
ctx *ext.Context,
dlCtx *models.DownloadContext,
media *models.Media,
) error {
var result gotgbot.InlineQueryResult
@ -177,7 +175,6 @@ func HandleInlineCached(
func HandleInlineCachedResult(
bot *gotgbot.Bot,
ctx *ext.Context,
dlCtx *models.DownloadContext,
media *models.Media,
) error {
format := media.Format

View file

@ -25,7 +25,7 @@ func HandleDownloadRequest(
) error {
chatID := ctx.EffectiveMessage.Chat.Id
if dlCtx.Extractor.Type == enums.ExtractorTypeSingle {
TypingEffect(bot, ctx, chatID)
TypingEffect(bot, chatID)
err := HandleDefaultFormatDownload(bot, ctx, taskCtx, dlCtx)
if err != nil {
return err
@ -115,7 +115,7 @@ func SendMedias(
inputMediaList = append(inputMediaList, inputMedia)
}
mediaType := chunk[0].Media.Format.Type
SendingEffect(bot, ctx, chatID, mediaType)
SendingEffect(bot, chatID, mediaType)
msgs, err := bot.SendMediaGroup(
chatID,
inputMediaList,

View file

@ -6,8 +6,6 @@ import (
"path/filepath"
"strings"
"github.com/pkg/errors"
"govd/database"
"govd/enums"
"govd/models"
@ -17,9 +15,11 @@ import (
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/pkg/errors"
)
func getFileThumbnail(
ctx context.Context,
format *models.MediaFormat,
filePath string,
) (string, error) {
@ -29,9 +29,6 @@ func getFileThumbnail(
fileBaseName := fileName[:len(fileName)-len(fileExt)]
thumbnailFilePath := filepath.Join(fileDir, fileBaseName+".thumb.jpeg")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if len(format.Thumbnail) > 0 {
file, err := util.DownloadFileInMemory(ctx, format.Thumbnail, nil)
if err != nil {
@ -109,7 +106,7 @@ func StoreMedias(
var storedMedias []*models.Media
if len(medias) == 0 {
return fmt.Errorf("no media to store")
return errors.New("no media to store")
}
for idx, msg := range msgs {
@ -161,7 +158,6 @@ func FormatCaption(
func TypingEffect(
bot *gotgbot.Bot,
ctx *ext.Context,
chatID int64,
) {
bot.SendChatAction(
@ -173,7 +169,6 @@ func TypingEffect(
func SendingEffect(
bot *gotgbot.Bot,
ctx *ext.Context,
chatID int64,
mediaType enums.MediaType,
) {