refactor: migrate to gotd (wip)
This commit is contained in:
35
common/utils.go
Normal file
35
common/utils.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gotd/td/tg"
|
||||
"github.com/krau/SaveAny-Bot/logger"
|
||||
"github.com/krau/SaveAny-Bot/types"
|
||||
)
|
||||
|
||||
func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) {
|
||||
logger.L.Debug("FileFromMedia")
|
||||
switch media := media.(type) {
|
||||
case *tg.MessageMediaDocument:
|
||||
document, ok := media.Document.AsNotEmpty()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected type %T", media)
|
||||
}
|
||||
var fileName string
|
||||
for _, attribute := range document.Attributes {
|
||||
if name, ok := attribute.(*tg.DocumentAttributeFilename); ok {
|
||||
fileName = name.FileName
|
||||
break
|
||||
}
|
||||
}
|
||||
return &types.File{
|
||||
Location: document.AsInputDocumentFileLocation(),
|
||||
FileSize: document.Size,
|
||||
FileName: fileName,
|
||||
MimeType: document.MimeType,
|
||||
ID: document.ID,
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected type %T", media)
|
||||
}
|
||||
Reference in New Issue
Block a user