mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-08 17:16:37 +08:00
refactor: improve error handling for empty file, document, and photo cases
This commit is contained in:
+14
-7
@@ -2,7 +2,7 @@ package bot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -15,6 +15,14 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/types"
|
"github.com/krau/SaveAny-Bot/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrEmptyFileName = errors.New("file name is empty")
|
||||||
|
ErrEmptyDocument = errors.New("document is empty")
|
||||||
|
ErrEmptyPhoto = errors.New("photo is empty")
|
||||||
|
ErrEmptyPhotoSize = errors.New("photo size is empty")
|
||||||
|
ErrEmptyPhotoSizes = errors.New("photo size slice is empty")
|
||||||
|
)
|
||||||
|
|
||||||
func supportedMediaFilter(m *tg.Message) (bool, error) {
|
func supportedMediaFilter(m *tg.Message) (bool, error) {
|
||||||
if not := m.Media == nil; not {
|
if not := m.Media == nil; not {
|
||||||
return false, dispatcher.EndGroups
|
return false, dispatcher.EndGroups
|
||||||
@@ -79,7 +87,7 @@ func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) {
|
|||||||
case *tg.MessageMediaDocument:
|
case *tg.MessageMediaDocument:
|
||||||
document, ok := media.Document.AsNotEmpty()
|
document, ok := media.Document.AsNotEmpty()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("document is empty")
|
return nil, ErrEmptyDocument
|
||||||
}
|
}
|
||||||
var fileName string
|
var fileName string
|
||||||
for _, attribute := range document.Attributes {
|
for _, attribute := range document.Attributes {
|
||||||
@@ -89,8 +97,7 @@ func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if fileName == "" {
|
if fileName == "" {
|
||||||
fileName = fmt.Sprintf("%x", md5.Sum(document.GetFileReference()))
|
return nil, ErrEmptyFileName
|
||||||
logger.L.Warnf("File name is empty, using hash: %s", fileName)
|
|
||||||
}
|
}
|
||||||
return &types.File{
|
return &types.File{
|
||||||
Location: document.AsInputDocumentFileLocation(),
|
Location: document.AsInputDocumentFileLocation(),
|
||||||
@@ -100,16 +107,16 @@ func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) {
|
|||||||
case *tg.MessageMediaPhoto:
|
case *tg.MessageMediaPhoto:
|
||||||
photo, ok := media.Photo.AsNotEmpty()
|
photo, ok := media.Photo.AsNotEmpty()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("photo is empty")
|
return nil, ErrEmptyPhoto
|
||||||
}
|
}
|
||||||
sizes := photo.Sizes
|
sizes := photo.Sizes
|
||||||
if len(sizes) == 0 {
|
if len(sizes) == 0 {
|
||||||
return nil, fmt.Errorf("photo sizes is empty")
|
return nil, ErrEmptyPhotoSizes
|
||||||
}
|
}
|
||||||
photoSize := sizes[len(sizes)-1]
|
photoSize := sizes[len(sizes)-1]
|
||||||
size, ok := photoSize.AsNotEmpty()
|
size, ok := photoSize.AsNotEmpty()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("photo size is empty")
|
return nil, ErrEmptyPhotoSize
|
||||||
}
|
}
|
||||||
location := new(tg.InputPhotoFileLocation)
|
location := new(tg.InputPhotoFileLocation)
|
||||||
location.ID = photo.GetID()
|
location.ID = photo.GetID()
|
||||||
|
|||||||
Reference in New Issue
Block a user