@@ -195,10 +195,13 @@ func MatchError(err error, str string) bool {
195195type FileLocationOptions struct {
196196 ThumbOnly bool
197197 ThumbSize PhotoSize
198+ Video bool
198199}
199200
200201// GetFileLocation returns file location, datacenter, file size and file name
201202func GetFileLocation (file any , opts ... FileLocationOptions ) (InputFileLocation , int32 , int64 , string , error ) {
203+ var opt = getVariadic (opts , FileLocationOptions {})
204+
202205 var (
203206 location any
204207 dataCenter int32 = 4
@@ -247,10 +250,10 @@ mediaMessageSwitch:
247250 }
248251 switch l := location .(type ) {
249252 case * DocumentObj :
250- if len ( opts ) > 0 && opts [ 0 ] .ThumbOnly {
253+ if opt .ThumbOnly {
251254 var selectedThumb = l .Thumbs [0 ]
252- if opts [ 0 ] .ThumbSize != nil {
253- selectedThumb = opts [ 0 ] .ThumbSize
255+ if opt .ThumbSize != nil {
256+ selectedThumb = opt .ThumbSize
254257 }
255258
256259 size , sizeType := getPhotoSize (selectedThumb )
@@ -269,6 +272,17 @@ mediaMessageSwitch:
269272 ThumbSize : "" ,
270273 }, l .DcID , l .Size , GetFileName (l ), nil
271274 case * PhotoObj :
275+ if len (l .VideoSizes ) > 0 && opt .Video {
276+ var selectedThumb = l .VideoSizes [len (l .VideoSizes )- 1 ]
277+ size , sizeType := getVideoSize (selectedThumb )
278+ return & InputPhotoFileLocation {
279+ ID : l .ID ,
280+ AccessHash : l .AccessHash ,
281+ FileReference : l .FileReference ,
282+ ThumbSize : sizeType ,
283+ }, l .DcID , size , GetFileName (l , true ), nil
284+ }
285+
272286 var selectedThumb = l .Sizes [len (l .Sizes )- 1 ]
273287 if len (opts ) > 0 && opts [0 ].ThumbOnly {
274288 if opts [0 ].ThumbSize != nil {
@@ -312,6 +326,15 @@ func getPhotoSize(sizes PhotoSize) (int64, string) {
312326 }
313327}
314328
329+ func getVideoSize (sizes VideoSize ) (int64 , string ) {
330+ switch s := sizes .(type ) {
331+ case * VideoSizeObj :
332+ return int64 (s .Size ), s .Type
333+ }
334+
335+ return 0 , ""
336+ }
337+
315338func getMax (a []int32 ) int32 {
316339 if len (a ) == 0 {
317340 return 0
@@ -342,7 +365,9 @@ func sanitizePath(path string, filename string) string {
342365// *MessageMedia
343366// *Document
344367// *Photo
345- func GetFileName (f any ) string {
368+ func GetFileName (f any , video ... bool ) string {
369+ var isVid = getVariadic (video , false )
370+
346371 getDocName := func (doc * DocumentObj ) string {
347372 var name string
348373 for _ , attr := range doc .Attributes {
@@ -377,12 +402,18 @@ func GetFileName(f any) string {
377402 case * MessageMediaDocument :
378403 return getDocName (f .Document .(* DocumentObj ))
379404 case * MessageMediaPhoto :
405+ if isVid {
406+ return fmt .Sprintf ("video_%s_%d.mp4" , time .Now ().Format ("2006-01-02_15-04-05" ), rand .Intn (1000 ))
407+ }
380408 return fmt .Sprintf ("photo_%s_%d.jpg" , time .Now ().Format ("2006-01-02_15-04-05" ), rand .Intn (1000 ))
381409 case * MessageMediaContact :
382410 return fmt .Sprintf ("contact_%s_%d.vcf" , f .FirstName , rand .Intn (1000 ))
383411 case * DocumentObj :
384412 return getDocName (f )
385413 case * PhotoObj :
414+ if isVid {
415+ return fmt .Sprintf ("video_%s_%d.mp4" , time .Now ().Format ("2006-01-02_15-04-05" ), rand .Intn (1000 ))
416+ }
386417 return fmt .Sprintf ("photo_%s_%d.jpg" , time .Now ().Format ("2006-01-02_15-04-05" ), rand .Intn (1000 ))
387418 case * InputPeerPhotoFileLocation :
388419 return fmt .Sprintf ("profile_photo_%s_%d.jpg" , time .Now ().Format ("2006-01-02_15-04-05" ), rand .Intn (1000 ))
0 commit comments