fixes some lint errors
This commit is contained in:
parent
5ccf4e6168
commit
12c12e53f7
16 changed files with 108 additions and 93 deletions
|
@ -44,41 +44,7 @@ func downloadMediaItem(
|
|||
}
|
||||
}()
|
||||
|
||||
if format.Type != enums.MediaTypePhoto {
|
||||
if len(format.Segments) == 0 {
|
||||
path, err := util.DownloadFile(
|
||||
ctx, format.URL,
|
||||
fileName, config,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download file: %w", err)
|
||||
}
|
||||
filePath = path
|
||||
} else {
|
||||
path, err := util.DownloadFileWithSegments(
|
||||
ctx, format.Segments,
|
||||
fileName, config,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download segments: %w", err)
|
||||
}
|
||||
filePath = path
|
||||
}
|
||||
|
||||
if format.Type == enums.MediaTypeVideo || format.Type == enums.MediaTypeAudio {
|
||||
path, err := getFileThumbnail(format, filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get thumbnail: %w", err)
|
||||
}
|
||||
thumbnailFilePath = path
|
||||
}
|
||||
|
||||
if format.Type == enums.MediaTypeVideo {
|
||||
if format.Width == 0 || format.Height == 0 || format.Duration == 0 {
|
||||
insertVideoInfo(format, filePath)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if format.Type == enums.MediaTypePhoto {
|
||||
file, err := util.DownloadFileInMemory(ctx, format.URL, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download image: %w", err)
|
||||
|
@ -88,9 +54,42 @@ func downloadMediaItem(
|
|||
return nil, fmt.Errorf("failed to convert image: %w", err)
|
||||
}
|
||||
filePath = path
|
||||
cleanup = false
|
||||
return &models.DownloadedMedia{
|
||||
FilePath: filePath,
|
||||
ThumbnailFilePath: thumbnailFilePath,
|
||||
Media: media,
|
||||
Index: idx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// hndle non-photo (video/audio/other)
|
||||
if len(format.Segments) == 0 {
|
||||
path, err := util.DownloadFile(ctx, format.URL, fileName, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download file: %w", err)
|
||||
}
|
||||
filePath = path
|
||||
} else {
|
||||
path, err := util.DownloadFileWithSegments(ctx, format.Segments, fileName, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to download segments: %w", err)
|
||||
}
|
||||
filePath = path
|
||||
}
|
||||
|
||||
if format.Type == enums.MediaTypeVideo || format.Type == enums.MediaTypeAudio {
|
||||
path, err := getFileThumbnail(format, filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get thumbnail: %w", err)
|
||||
}
|
||||
thumbnailFilePath = path
|
||||
}
|
||||
|
||||
if format.Type == enums.MediaTypeVideo && (format.Width == 0 || format.Height == 0 || format.Duration == 0) {
|
||||
insertVideoInfo(format, filePath)
|
||||
}
|
||||
|
||||
// all good, no need to delete files
|
||||
cleanup = false
|
||||
return &models.DownloadedMedia{
|
||||
FilePath: filePath,
|
||||
|
|
|
@ -107,9 +107,11 @@ func StoreMedias(
|
|||
medias []*models.DownloadedMedia,
|
||||
) error {
|
||||
var storedMedias []*models.Media
|
||||
|
||||
if len(medias) == 0 {
|
||||
return fmt.Errorf("no media to store")
|
||||
}
|
||||
|
||||
for idx, msg := range msgs {
|
||||
fileID := GetMessageFileID(&msg)
|
||||
if len(fileID) == 0 {
|
||||
|
@ -211,27 +213,22 @@ func HandleErrorMessage(
|
|||
for currentError != nil {
|
||||
var botError *util.Error
|
||||
if errors.As(currentError, &botError) {
|
||||
SendErrorMessage(bot, ctx, fmt.Sprintf(
|
||||
"error occurred when downloading: %s",
|
||||
currentError.Error(),
|
||||
))
|
||||
SendErrorMessage(bot, ctx,
|
||||
"error occurred when downloading: "+currentError.Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
currentError = errors.Unwrap(currentError)
|
||||
}
|
||||
|
||||
lastError := util.GetLastError(err)
|
||||
errorMessage := fmt.Sprintf(
|
||||
"error occurred when downloading: %s",
|
||||
lastError.Error(),
|
||||
)
|
||||
errorMessage := "error occurred when downloading: " + lastError.Error()
|
||||
|
||||
if strings.Contains(errorMessage, bot.Token) {
|
||||
errorMessage = "telegram related error, probably connection issue"
|
||||
}
|
||||
|
||||
SendErrorMessage(bot, ctx, errorMessage)
|
||||
|
||||
}
|
||||
|
||||
func SendErrorMessage(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue