(media tg.MessageMediaClass)
| 64 | } |
| 65 | |
| 66 | func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) { |
| 67 | switch media := media.(type) { |
| 68 | case *tg.MessageMediaDocument: |
| 69 | document, ok := media.Document.AsNotEmpty() |
| 70 | if !ok { |
| 71 | return nil, fmt.Errorf("unexpected type %T", media) |
| 72 | } |
| 73 | var fileName string |
| 74 | for _, attribute := range document.Attributes { |
| 75 | if name, ok := attribute.(*tg.DocumentAttributeFilename); ok { |
| 76 | fileName = name.FileName |
| 77 | break |
| 78 | } |
| 79 | } |
| 80 | return &types.File{ |
| 81 | Location: document.AsInputDocumentFileLocation(), |
| 82 | FileSize: document.Size, |
| 83 | FileName: fileName, |
| 84 | MimeType: document.MimeType, |
| 85 | ID: document.ID, |
| 86 | }, nil |
| 87 | case *tg.MessageMediaPhoto: |
| 88 | photo, ok := media.Photo.AsNotEmpty() |
| 89 | if !ok { |
| 90 | return nil, fmt.Errorf("unexpected type %T", media) |
| 91 | } |
| 92 | sizes := photo.Sizes |
| 93 | if len(sizes) == 0 { |
| 94 | return nil, errors.New("photo has no sizes") |
| 95 | } |
| 96 | photoSize := sizes[len(sizes)-1] |
| 97 | size, ok := photoSize.AsNotEmpty() |
| 98 | if !ok { |
| 99 | return nil, errors.New("photo size is empty") |
| 100 | } |
| 101 | location := new(tg.InputPhotoFileLocation) |
| 102 | location.ID = photo.GetID() |
| 103 | location.AccessHash = photo.GetAccessHash() |
| 104 | location.FileReference = photo.GetFileReference() |
| 105 | location.ThumbSize = size.GetType() |
| 106 | return &types.File{ |
| 107 | Location: location, |
| 108 | FileSize: 0, // caller should judge if this is a photo or not |
| 109 | FileName: fmt.Sprintf("photo_%d.jpg", photo.GetID()), |
| 110 | MimeType: "image/jpeg", |
| 111 | ID: photo.GetID(), |
| 112 | }, nil |
| 113 | } |
| 114 | return nil, fmt.Errorf("unexpected type %T", media) |
| 115 | } |
| 116 | |
| 117 | func FileFromMessage(ctx context.Context, client *gotgproto.Client, messageID int) (*types.File, error) { |
| 118 | key := fmt.Sprintf("file:%d:%d", messageID, client.Self.ID) |
no outgoing calls
no test coverage detected