-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
78 lines (63 loc) · 1.38 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/google/uuid"
)
const (
_ int = iota
Video
Audio
Unknown
)
type BotConfig struct {
BotToken string
BotAPI string
}
type MediaInfo struct {
Title string `json:"title"`
Filename string `json:"filename"`
Duration float64 `json:"duration"`
Thumbnail string `json:"thumbnail"`
Formats []Format `json:"formats"`
Format string `json:"format"`
Width int `json:"width"`
Height int `json:"height"`
Ext string `json:"ext"`
}
type Format struct {
Ext string `json:"ext"`
FormatId string `json:"format_id"`
Acodec string `json:"acodec"`
Vcodec string `json:"vcodec"`
}
func (m *MediaInfo) GetThumbnail() (string, error) {
if m.Thumbnail == "" {
return "", errors.New("no thumbnail URL provided")
}
thumbId := uuid.New().String()
filename := fmt.Sprintf(tempDir, "%s.jpg", thumbId)
thumbnailPath := filepath.Join(tempDir, filename)
resp, err := http.Get(m.Thumbnail)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download thumbnail: %s", resp.Status)
}
out, err := os.Create(thumbnailPath)
if err != nil {
return "", err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
}
return thumbnailPath, nil
}