ali_oss.go
1.7 KB
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
79
80
81
82
83
84
85
86
package oss
import (
"encoding/json"
"fmt"
"image"
_ "image/jpeg"
"net/http"
"strings"
"time"
)
type FileInfo struct {
FileSize struct {
Value string
}
Format struct {
Value string
}
ImageHeight struct {
Value string
}
ImageWidth struct {
Value string
}
}
func GetImageInfo(url string) (info FileInfo, err error) {
//ok := strings.HasPrefix(url, "https://timeless-world.oss-cn-shenzhen.aliyuncs.com")
ok := strings.HasPrefix(url, "http")
if !ok {
return
}
ok = strings.Contains(url, "aliyuncs")
if !ok {
return
}
apiUrl := url + `?x-oss-process=image/info`
req, err := http.NewRequest(http.MethodGet, apiUrl, nil)
if err != nil {
return info, err
}
httpclient := http.Client{
Timeout: 5 * time.Second,
}
resp, err := httpclient.Do(req)
if err != nil {
return info, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
}
jDecoder := json.NewDecoder(resp.Body)
err = jDecoder.Decode(&info)
if err != nil {
return info, err
}
return info, nil
}
// 获取视频封面图
func GetVideoCover(videoUrl string) (coverUrl string, w int, h int, err error) {
ok := strings.HasPrefix(videoUrl, "http")
if !ok {
return
}
ok = strings.Contains(videoUrl, "aliyuncs")
if !ok {
return
}
videoUrl = videoUrl + "?x-oss-process=video/snapshot,t_100,f_jpg,m_fast"
httpclient := http.Client{
Timeout: 5 * time.Second,
}
res, err := httpclient.Get(videoUrl)
if err != nil || res.StatusCode != http.StatusOK {
return videoUrl, 600, 600, fmt.Errorf("获取图片失败:%s", err)
}
defer res.Body.Close()
m, _, err := image.Decode(res.Body)
if err != nil {
return videoUrl, 600, 600, fmt.Errorf("获取图片失败:%s", err)
}
return videoUrl, m.Bounds().Dx(), m.Bounds().Dy(), nil
}