-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuploader.go
155 lines (137 loc) · 3.31 KB
/
uploader.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"errors"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/mattermost/mattermost-server/v5/model"
)
func main() {
if len(os.Args) != 4 {
fmt.Println("usage error.")
fmt.Println("mattermost_custom_emoji_uploader http://your-mattermost.example [personal access token] [path of dir or image file]")
os.Exit(1)
}
baseURL := os.Args[1]
token := os.Args[2]
imagePath := os.Args[3]
u, err := url.Parse(baseURL)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
apiUrl := *u
apiUrl.Path = path.Join(apiUrl.Path, "api", "v4")
c := model.Client4{
Url: u.String(),
ApiUrl: apiUrl.String(),
HttpClient: &http.Client{},
AuthToken: token,
AuthType: model.HEADER_BEARER,
HttpHeader: nil,
}
// me で自分自身のデータを取得することができる
user, resp := c.GetUser("me", "")
if resp.Error != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
info, err := os.Stat(imagePath)
if err != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
var paths []string
if info.IsDir() {
pngFiles, err := filepath.Glob(filepath.Join(imagePath, "*.png"))
if err != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
paths = append(paths, pngFiles...)
jpegFiles, err := filepath.Glob(filepath.Join(imagePath, "*.jpg"))
if err != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
paths = append(paths, jpegFiles...)
jpegFiles, err = filepath.Glob(filepath.Join(imagePath, "*.jpeg"))
if err != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
paths = append(paths, jpegFiles...)
gifFiles, err := filepath.Glob(filepath.Join(imagePath, "*.gif"))
if err != nil {
fmt.Println(resp.Error.Error())
os.Exit(1)
}
paths = append(paths, gifFiles...)
} else {
paths = append(paths, imagePath)
}
for _, imagePath := range paths {
fmt.Printf("filepath: %v のファイルを処理中...\n", imagePath)
err = createEmoji(c, imagePath, user.Id)
if err != nil {
fmt.Println("エラー発生。")
fmt.Println(err.Error())
} else {
fmt.Println("処理完了")
}
}
}
func createEmoji(c model.Client4, imagePath string, creatorId string) error {
f, err := os.Open(imagePath)
defer closeFile(f)
if err != nil {
return err
}
decode, extension, err := image.Decode(f)
if err != nil {
return err
}
s, err := f.Stat()
if err != nil {
return err
}
name := strings.TrimSuffix(s.Name(), path.Ext(imagePath))
emoji := &model.Emoji{Name: name, CreatorId: creatorId}
buf := &bytes.Buffer{}
if extension == "png" {
err = png.Encode(buf, decode)
} else if extension == "jpg" || extension == "jpeg" {
err = jpeg.Encode(buf, decode, nil)
} else if extension == "gif" {
err = gif.Encode(buf, decode, nil)
} else {
return errors.New("読み込みに失敗しました。拡張子は jpg, jpeg, gif しか対応していません。")
}
if err != nil {
return err
}
_, resp := c.CreateEmoji(emoji, buf.Bytes(), s.Name())
if resp.Error != nil {
err = errors.New(resp.Error.Error())
return err
} else {
return nil
}
}
func closeFile(f *os.File) {
err := f.Close()
// closeに失敗した場合は潔くコマンドを終了する
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}