-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fileid): add some creation helpers
- Loading branch information
Showing
5 changed files
with
240 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fileid | ||
|
||
import "github.com/gotd/td/tg" | ||
|
||
// FromDocument creates FileID from tg.Document. | ||
func FromDocument(doc *tg.Document) FileID { | ||
fileID := FileID{ | ||
Type: DocumentAsFile, | ||
DC: doc.DCID, | ||
ID: doc.ID, | ||
AccessHash: doc.AccessHash, | ||
FileReference: doc.FileReference, | ||
} | ||
for _, attr := range doc.Attributes { | ||
switch attr := attr.(type) { | ||
case *tg.DocumentAttributeAnimated: | ||
fileID.Type = Animation | ||
case *tg.DocumentAttributeSticker: | ||
fileID.Type = Sticker | ||
case *tg.DocumentAttributeVideo: | ||
fileID.Type = Video | ||
if attr.RoundMessage { | ||
fileID.Type = VideoNote | ||
} | ||
case *tg.DocumentAttributeAudio: | ||
fileID.Type = Audio | ||
if attr.Voice { | ||
fileID.Type = Voice | ||
} | ||
} | ||
} | ||
return fileID | ||
} | ||
|
||
// FromPhoto creates FileID from tg.Photo. | ||
func FromPhoto(photo *tg.Photo, thumbType rune) FileID { | ||
return FileID{ | ||
Type: Photo, | ||
DC: photo.DCID, | ||
ID: photo.ID, | ||
AccessHash: photo.AccessHash, | ||
FileReference: photo.FileReference, | ||
PhotoSizeSource: PhotoSizeSource{ | ||
Type: PhotoSizeSourceThumbnail, | ||
FileType: Photo, | ||
ThumbnailType: thumbType, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package fileid | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gotd/td/tg" | ||
) | ||
|
||
func TestFromDocument(t *testing.T) { | ||
doc := func(attrs ...tg.DocumentAttributeClass) *tg.Document { | ||
return &tg.Document{ | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DCID: 4, | ||
Attributes: attrs, | ||
} | ||
} | ||
fileID := func(typ Type) FileID { | ||
return FileID{ | ||
Type: typ, | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DC: 4, | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
doc *tg.Document | ||
want FileID | ||
}{ | ||
{"File", doc(), fileID(DocumentAsFile)}, | ||
{"Animation", doc(&tg.DocumentAttributeAnimated{}), fileID(Animation)}, | ||
{"Sticker", doc(&tg.DocumentAttributeSticker{}), fileID(Sticker)}, | ||
{"Video", doc(&tg.DocumentAttributeVideo{}), fileID(Video)}, | ||
{"VideoNote", doc(&tg.DocumentAttributeVideo{RoundMessage: true}), fileID(VideoNote)}, | ||
{"Audio", doc(&tg.DocumentAttributeAudio{}), fileID(Audio)}, | ||
{"Voice", doc(&tg.DocumentAttributeAudio{Voice: true}), fileID(Voice)}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, FromDocument(tt.doc)) | ||
}) | ||
} | ||
} | ||
|
||
func TestFromPhoto(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
photo *tg.Photo | ||
size rune | ||
want FileID | ||
}{ | ||
{ | ||
"Photo", | ||
&tg.Photo{ | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DCID: 4, | ||
}, | ||
'x', | ||
FileID{ | ||
Type: Photo, | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DC: 4, | ||
PhotoSizeSource: PhotoSizeSource{ | ||
Type: PhotoSizeSourceThumbnail, | ||
FileType: Photo, | ||
ThumbnailType: 'x', | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, FromPhoto(tt.photo, tt.size)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package fileid | ||
|
||
import "github.com/gotd/td/tg" | ||
|
||
// AsInputWebFileLocation converts file ID to tg.InputWebFileLocationClass. | ||
func (f FileID) AsInputWebFileLocation() (tg.InputWebFileLocationClass, bool) { | ||
if f.URL == "" { | ||
return nil, false | ||
} | ||
|
||
return &tg.InputWebFileLocation{ | ||
URL: f.URL, | ||
AccessHash: f.AccessHash, | ||
}, true | ||
} | ||
|
||
func (f FileID) asPhotoLocation() (tg.InputFileLocationClass, bool) { | ||
switch src := f.PhotoSizeSource; src.Type { | ||
case PhotoSizeSourceLegacy: | ||
case PhotoSizeSourceThumbnail: | ||
switch src.FileType { | ||
case Photo, Thumbnail: | ||
return &tg.InputPhotoFileLocation{ | ||
ID: f.ID, | ||
AccessHash: f.AccessHash, | ||
FileReference: f.FileReference, | ||
ThumbSize: string(f.PhotoSizeSource.ThumbnailType), | ||
}, true | ||
} | ||
case PhotoSizeSourceDialogPhotoSmall, | ||
PhotoSizeSourceDialogPhotoBig: | ||
return &tg.InputPeerPhotoFileLocation{ | ||
Big: src.Type == PhotoSizeSourceDialogPhotoBig, | ||
Peer: src.dialogPeer(), | ||
PhotoID: f.ID, | ||
}, true | ||
case PhotoSizeSourceStickerSetThumbnail: | ||
case PhotoSizeSourceFullLegacy: | ||
return &tg.InputPhotoLegacyFileLocation{ | ||
ID: f.ID, | ||
AccessHash: f.AccessHash, | ||
FileReference: f.FileReference, | ||
VolumeID: f.PhotoSizeSource.VolumeID, | ||
LocalID: f.PhotoSizeSource.LocalID, | ||
Secret: f.PhotoSizeSource.Secret, | ||
}, true | ||
case PhotoSizeSourceDialogPhotoSmallLegacy, | ||
PhotoSizeSourceDialogPhotoBigLegacy: | ||
return &tg.InputPeerPhotoFileLocationLegacy{ | ||
Big: src.Type == PhotoSizeSourceDialogPhotoBigLegacy, | ||
Peer: src.dialogPeer(), | ||
VolumeID: f.PhotoSizeSource.VolumeID, | ||
LocalID: f.PhotoSizeSource.LocalID, | ||
}, true | ||
case PhotoSizeSourceStickerSetThumbnailLegacy: | ||
return &tg.InputStickerSetThumbLegacy{ | ||
Stickerset: f.PhotoSizeSource.stickerSet(), | ||
VolumeID: f.PhotoSizeSource.VolumeID, | ||
LocalID: f.PhotoSizeSource.LocalID, | ||
}, true | ||
case PhotoSizeSourceStickerSetThumbnailVersion: | ||
return &tg.InputStickerSetThumb{ | ||
Stickerset: f.PhotoSizeSource.stickerSet(), | ||
ThumbVersion: int(f.PhotoSizeSource.StickerVersion), | ||
}, true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
// AsInputFileLocation converts file ID to tg.InputFileLocationClass. | ||
func (f FileID) AsInputFileLocation() (tg.InputFileLocationClass, bool) { | ||
switch f.Type { | ||
case Thumbnail, ProfilePhoto, Photo: | ||
return f.asPhotoLocation() | ||
case Encrypted: | ||
return &tg.InputEncryptedFileLocation{ | ||
ID: f.ID, | ||
AccessHash: f.AccessHash, | ||
}, true | ||
case SecureRaw, | ||
Secure: | ||
return &tg.InputSecureFileLocation{ | ||
ID: f.ID, | ||
AccessHash: f.AccessHash, | ||
}, true | ||
case Video, | ||
Voice, | ||
Document, | ||
Sticker, | ||
Audio, | ||
Animation, | ||
VideoNote, | ||
Background, | ||
DocumentAsFile: | ||
return &tg.InputDocumentFileLocation{ | ||
ID: f.ID, | ||
AccessHash: f.AccessHash, | ||
FileReference: f.FileReference, | ||
ThumbSize: "", // ? | ||
}, true | ||
} | ||
|
||
return nil, false | ||
} |
File renamed without changes.