-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
533 lines (483 loc) · 12 KB
/
utils.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package main
import (
"archive/zip"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"html/template"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/axgle/mahonia"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/nfnt/resize"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/teris-io/shortid"
"golang.org/x/text/encoding/simplifiedchinese"
gomail "gopkg.in/gomail.v2"
)
var codes = map[int]string{
0: "检查消息",
200: "成功",
201: "已创建",
202: "已接受",
204: "无内容",
400: "请求无法解析",
4001: "必填参数校验错误",
4002: "达到最大尝试登录次数,稍后再试",
4003: "瓦片请求格式错误",
4004: "符号请求格式错误",
4005: "字体请求格式错误",
401: "未授权",
4011: "用户名或密码错误",
4012: "用户名非法,请使用字母,数字,短划线,下划线组合或用户名需少于32个字符",
4013: "邮箱非法,请使用能收到验证邮件的正确邮箱",
4014: "密码非法,请使用至少4位以上密码字符",
4015: "用户名已注册,请使用新的用户名",
4016: "邮箱已注册,请使用新的邮箱",
403: "禁止访问",
4031: "邮箱不存在",
404: "找不到资源",
4041: "用户不存在",
4042: "角色不存在",
4043: "服务不存在",
4044: "找不到样式",
4045: "找不到瓦片集",
4046: "找不到数据集",
4047: "找不到字体库",
4048: "找不到上传文件",
4049: "服务不存在",
408: "请求超时",
500: "系统错误",
5001: "数据库错误",
5002: "文件读写错误",
5003: "IO读写错误",
5004: "MBTiles读写错误",
5005: "系统配置错误",
501: "维护中",
503: "服务不可用",
}
//Res response schema
type Res struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
//NewRes Create Res
func NewRes() *Res {
return &Res{
Code: http.StatusOK,
Msg: codes[http.StatusOK],
}
}
//Fail failed error
func (res *Res) Fail(c *gin.Context, code int) {
res.Code = code
res.Msg = codes[code]
c.JSON(http.StatusOK, res)
}
//FailErr failed string
func (res *Res) FailErr(c *gin.Context, err error) {
res.Code = 0
if err != nil {
res.Msg = err.Error()
}
c.JSON(http.StatusOK, res)
}
//FailMsg failed string
func (res *Res) FailMsg(c *gin.Context, msg string) {
res.Code = 0
res.Msg = msg
c.JSON(http.StatusOK, res)
}
//Done done
func (res *Res) Done(c *gin.Context, msg string) {
res.Code = http.StatusOK
res.Msg = codes[http.StatusOK]
if msg != "" {
res.Msg = msg
}
c.JSON(http.StatusOK, res)
}
//DoneCode done
func (res *Res) DoneCode(c *gin.Context, code int) {
res.Code = code
res.Msg = codes[code]
c.JSON(http.StatusOK, res)
}
//DoneData done
func (res *Res) DoneData(c *gin.Context, data interface{}) {
res.Code = http.StatusOK
res.Msg = codes[http.StatusOK]
res.Data = data
c.JSON(http.StatusOK, res)
}
//Reset reset to init
func (res *Res) Reset() {
res.Code = http.StatusOK
res.Msg = codes[http.StatusOK]
}
// scheme returns the underlying URL scheme of the original request.
func scheme(r *http.Request) string {
if r.TLS != nil {
return "https"
}
if scheme := r.Header.Get("X-Forwarded-Proto"); scheme != "" {
return scheme
}
if scheme := r.Header.Get("X-Forwarded-Protocol"); scheme != "" {
return scheme
}
if ssl := r.Header.Get("X-Forwarded-Ssl"); ssl == "on" {
return "https"
}
if scheme := r.Header.Get("X-Url-Scheme"); scheme != "" {
return scheme
}
return "http"
}
// rootURL returns the root URL of the service. If s.Domain is non-empty, it
// will be used as the hostname. If s.Path is non-empty, it will be used as a
// prefix.
func rootURL(r *http.Request) string {
return fmt.Sprintf("%s://%s", scheme(r), r.Host)
}
//MailConfig email config and data
type MailConfig struct {
From string
ReplyTo string
Subject string
TextPath string
HTMLPath string
Data interface{}
}
//SendMail send email
func (conf *MailConfig) SendMail() (err error) {
m := gomail.NewMessage()
m.SetHeader("From", conf.From)
m.SetHeader("To", conf.ReplyTo)
m.SetHeader("Subject", conf.Subject)
m.SetHeader("ReplyTo", conf.ReplyTo)
m.AddAlternativeWriter("text/html", func(w io.Writer) error {
return template.Must(template.ParseFiles(conf.HTMLPath)).Execute(w, conf.Data)
})
d := gomail.NewDialer(viper.GetString("smtp.credentials.host"), viper.GetInt("smtp.credentials.port"), viper.GetString("smtp.credentials.user"), viper.GetString("smtp.credentials.password"))
return d.DialAndSend(m)
}
func getEscapedString(str string) string {
return strings.Replace(url.QueryEscape(str), "+", "%20", -1)
}
func generateToken(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return b
}
token := make([]byte, n*2)
hex.Encode(token, b)
return token
}
//CreatePaths 创建用户目录
func CreatePaths(name string) {
os.MkdirAll(filepath.Join(viper.GetString("paths.styles"), name), os.ModePerm)
os.MkdirAll(filepath.Join(viper.GetString("paths.tilesets"), name), os.ModePerm)
os.MkdirAll(filepath.Join(viper.GetString("paths.datasets"), name), os.ModePerm)
os.MkdirAll(filepath.Join(viper.GetString("paths.uploads"), name), os.ModePerm)
// os.MkdirAll(filepath.Join(viper.GetString("paths.fonts"), name), os.ModePerm)
}
func checkUser(uid string) int {
if uid == "" {
return 4001
}
user := &User{}
if err := db.Where("name = ?", uid).First(&user).Error; err != nil {
if !gorm.IsRecordNotFoundError(err) {
log.Error(err)
return 5001
}
return 4041
}
return 200
}
func checkRole(rid string) int {
if rid == "" {
return 4001
}
role := &Role{}
if err := db.Where("id = ?", rid).First(&role).Error; err != nil {
if !gorm.IsRecordNotFoundError(err) {
log.Error(err)
return 5001
}
return 4042
}
return 200
}
func checkMap(mid string) int {
if mid == "" {
return 4001
}
m := &Map{}
if err := db.Where("id = ?", mid).First(&m).Error; err != nil {
if !gorm.IsRecordNotFoundError(err) {
log.Error(err)
return 5001
}
return 4049
}
return 200
}
func checkDataset(did string) int {
if did == "" {
return 4001
}
if !db.HasTable(did) {
return 4046
}
return 200
}
func newFeatrue(geoType string) *geojson.Feature {
var geometry orb.Geometry
switch geoType {
case "POINT":
geometry = orb.Point{}
case "MULTIPOINT":
geometry = orb.MultiPoint{}
case "LINESTRING":
geometry = orb.LineString{}
case "MULTILINESTRING":
geometry = orb.MultiLineString{}
case "POLYGON":
geometry = orb.Polygon{}
case "MULTIPOLYGON":
geometry = orb.MultiPolygon{}
default:
return nil
}
return &geojson.Feature{
Type: "Feature",
Geometry: geometry,
Properties: make(map[string]interface{}),
}
//test
// var t string
// s := fmt.Sprintf(`SELECT geometrytype(geom) FROM %s LIMIT 1;`, name)
// err = db.Raw(s).Row().Scan(&t)
// if err != nil {
// log.Error(err)
// res.Fail(c, 5001)
// return
// }
// if newFeatrue(t) == nil {
// log.Error("postgis 'geometrytype(geom)' return error")
// res.Fail(c, 5001)
// return
// }
}
//Thumbnail 缩略图
func Thumbnail(width, height uint, b64img string) string {
if b64img == "" {
return ""
}
pos := strings.Index(b64img, ";base64,")
imgbuf, err := base64.StdEncoding.DecodeString(b64img[pos+8:])
if err != nil {
log.Error(err)
return ""
}
switch b64img[5:pos] {
case "image/png":
pngI, err := png.Decode(bytes.NewReader(imgbuf))
if err != nil {
log.Error(err)
return ""
}
thumbnail := resize.Thumbnail(width, height, pngI, resize.Lanczos3)
data := new(bytes.Buffer)
err = png.Encode(data, thumbnail)
if err != nil {
log.Error(err)
return ""
}
src := base64.StdEncoding.EncodeToString(data.Bytes())
return "data:image/png;base64," + src
case "image/jpeg":
jpgI, err := jpeg.Decode(bytes.NewReader(imgbuf))
if err != nil {
log.Error(err)
return ""
}
thumbnail := resize.Thumbnail(width, height, jpgI, resize.Lanczos3)
data := new(bytes.Buffer)
err = jpeg.Encode(data, thumbnail, &jpeg.Options{
Quality: 80,
})
if err != nil {
log.Error(err)
return ""
}
src := base64.StdEncoding.EncodeToString(data.Bytes())
return "data:image/jpeg;base64," + src
}
return ""
}
//ConvertToByte 编码转换
func ConvertToByte(src string, srcCode string, targetCode string) []byte {
srcCoder := mahonia.NewDecoder(srcCode)
srcResult := srcCoder.ConvertString(src)
tagCoder := mahonia.NewDecoder(targetCode)
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
return cdata
}
// FileCopy copies a single file from src to dst
func FileCopy(src, dst string) error {
var err error
var srcfd *os.File
var dstfd *os.File
var srcinfo os.FileInfo
if srcfd, err = os.Open(src); err != nil {
return err
}
defer srcfd.Close()
if dstfd, err = os.Create(dst); err != nil {
return err
}
defer dstfd.Close()
if _, err = io.Copy(dstfd, srcfd); err != nil {
return err
}
if srcinfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcinfo.Mode())
}
// DirCopy copies a whole directory recursively
func DirCopy(src string, dst string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo
if srcinfo, err = os.Stat(src); err != nil {
return err
}
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}
if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = DirCopy(srcfp, dstfp); err != nil {
fmt.Println(err)
}
} else {
if err = FileCopy(srcfp, dstfp); err != nil {
fmt.Println(err)
}
}
}
return nil
}
//UnZipToDir 解压文件
func UnZipToDir(zipfile string, outdir string) error {
if outdir == "" {
outdir = strings.TrimSuffix(zipfile, filepath.Ext(zipfile))
}
err := os.MkdirAll(outdir, os.ModePerm)
if err != nil {
log.Error(err)
return err
}
zr, err := zip.OpenReader(zipfile)
if err != nil {
log.Error(err)
return err
}
defer zr.Close()
for _, f := range zr.File {
name := f.Name
if f.NonUTF8 {
// simplifiedchinese.GBK.NewDecoder().String
ns, err := simplifiedchinese.GB18030.NewDecoder().String(f.Name)
if err == nil {
name = ns
}
}
pn := filepath.Join(outdir, name)
// log.Infof("Uncompress: %s -> %s", name, pn)
if f.FileInfo().IsDir() {
err := os.MkdirAll(pn, os.ModePerm)
if err != nil {
log.Warnf("unzip %s: %v", zipfile, err)
return err
}
continue
}
ext := filepath.Ext(name)
pn = strings.TrimSuffix(pn, ext) + strings.ToLower(ext)
w, err := os.Create(pn)
if err != nil {
log.Warnf("Cannot unzip %s: %v", zipfile, err)
return err
}
defer w.Close()
r, err := f.Open()
if err != nil {
log.Warnf("Cannot unzip %s: %v", zipfile, err)
return err
}
defer r.Close()
_, err = io.Copy(w, r)
if err != nil {
log.Warnf("Cannot unzip %s: %v", zipfile, err)
return err
}
}
return nil
}
// detectUTF8 reports whether s is a valid UTF-8 string, and whether the string
// must be considered UTF-8 encoding (i.e., not compatible with CP-437, ASCII,
// or any other common encoding).
func detectUTF8(s string) (valid, require bool) {
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
i += size
// Officially, ZIP uses CP-437, but many readers use the system's
// local character encoding. Most encoding are compatible with a large
// subset of CP-437, which itself is ASCII-like.
//
// Forbid 0x7e and 0x5c since EUC-KR and Shift-JIS replace those
// characters with localized currency and overline characters.
if r < 0x20 || r > 0x7d || r == 0x5c {
if !utf8.ValidRune(r) || (r == utf8.RuneError && size == 1) {
return false, false
}
require = true
}
}
return true, require
}
//ShortID 生成shortid 不含中划线
func ShortID() string {
id, err := shortid.Generate()
for strings.Contains(id, "-") || err != nil {
id, err = shortid.Generate()
}
return id
}