-
Notifications
You must be signed in to change notification settings - Fork 406
/
froala.go
1138 lines (1103 loc) · 36.5 KB
/
froala.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package controllers
import (
// "bytes"
"encoding/json"
"fmt"
// beego "github.com/beego/beego/v2/adapter"
"github.com/pborman/uuid"
"image"
"io"
// "log"
"net/http"
"os"
"path"
// "hydrocms/models"
"encoding/base64"
"github.com/3xxx/engineercms/models"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/nfnt/resize"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
// "path/filepath"
"bytes"
"github.com/3xxx/engineercms/controllers/utils"
"regexp"
"strconv"
"strings"
"time"
)
// CMSWX froala API
type FroalaController struct {
web.Controller
}
type UploadimgFroala struct {
url string
title string
original string
state string
// "url": fmt.Sprintf("/static/upload/%s", filename),
// "title": "demo.jpg",
// "original": header.Filename,
// "state": "SUCCESS"
}
// 下面这个没用
func (c *FroalaController) ControllerFroala() {
op := c.GetString("action")
key := c.GetString("key") //这里进行判断各个页面,如果是addtopic,如果是addcategory
switch op {
case "config": //这里还是要优化成conf/config.json
// $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("config.json")), true);
// $action = $_GET['action'];
// switch ($action) {
// case 'config':
// $result = json_encode($CONFIG);
// var configJson []byte // 当客户端请求/controller?action=config 返回的json内容
file, err := os.Open("conf/config.json")
if err != nil {
// log.Fatal(err)
logs.Error(err)
os.Exit(1)
}
defer file.Close()
// fi, err := os.Open("d:/config.json")
// if err != nil {
// panic(err)
// }
// defer fi.Close()
fd, err := ioutil.ReadAll(file)
src := string(fd)
re, _ := regexp.Compile("\\/\\*[\\S\\s]+?\\*\\/") //参考php的$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("config.json")), true);
//将php中的正则移植到go中,需要将/ \/\*[\s\S]+?\*\/ /去掉前后的/,然后将\改成2个\\
//参考//去除所有尖括号内的HTML代码,并换成换行符
// re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
// src = re.ReplaceAllString(src, "\n")
//当把<和>换成/*和*\时,斜杠/和*之间加双斜杠\\才行。
src = re.ReplaceAllString(src, "")
tt := []byte(src)
// buf := bytes.NewBuffer(nil)
// buf.ReadFrom(file)
// configJson = buf.Bytes()
// w.Write(configJson)
var r interface{}
json.Unmarshal(tt, &r) //这个byte要解码
c.Data["json"] = r
c.ServeJSON()
//下面这段是测试用的
// b := []byte(`{
// "imageActionName": "uploadimage",
// "imageFieldName": "upfile",
// "imageMaxSize": 2048000,
// "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
// "imageCompressEnable": true,
// "imageCompressBorder": 1600,
// "imageInsertAlign": "none",
// "imageUrlPrefix": "",
// "imagePathFormat": "/static/upload/{yyyy}{mm}{dd}/{time}{rand:6}"
// }`)
// var r interface{}
// json.Unmarshal(b, &r)
// c.Data["json"] = r
// c.ServeJSON()
case "uploadimage", "uploadfile", "uploadvideo":
switch key {
case "wiki": //添加wiki
//保存上传的图片
_, h, err := c.GetFile("upfile")
if err != nil {
logs.Error(err)
}
var filesize int64
fileSuffix := path.Ext(h.Filename)
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
year, month, _ := time.Now().Date()
err = os.MkdirAll("./attachment/wiki/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
path1 := "./attachment/wiki/" + strconv.Itoa(year) + month.String() + "/" + newname //h.Filename
Url := "/attachment/wiki/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("upfile", path1) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(path1)
filesize = filesize / 1000.0
c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "url": Url + newname, "title": h.Filename, "original": h.Filename}
c.ServeJSON()
default:
//解析表单
pid := c.GetString("pid")
// beego.Info(pid)
//pid转成64为
pidNum, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
logs.Error(err)
}
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(pidNum)
if err != nil {
logs.Error(err)
}
// beego.Info(DiskDirectory)
//获取上传的文件
_, h, err := c.GetFile("upfile")
if err != nil {
logs.Error(err)
}
// beego.Info(h.Filename)
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
// err = ioutil.WriteFile(path1+newname+".jpg", ddd, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)
// if err != nil {
// logs.Error(err)
// }
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
var filesize int64
if h != nil {
//保存附件
path = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("upfile", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(path)
filesize = filesize / 1000.0
c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "url": Url + newname, "title": h.Filename, "original": h.Filename}
c.ServeJSON()
} else {
c.Data["json"] = map[string]interface{}{"state": "ERROR", "url": "", "title": "", "original": ""}
c.ServeJSON()
}
}
case "uploadscrawl":
number := c.GetString("number")
name := c.GetString("name")
err := os.MkdirAll("./attachment/"+number+name, 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
path1 := "./attachment/" + number + name + "/"
//保存上传的图片
//upfile为base64格式文件,转成图片保存
ww := c.GetString("upfile")
ddd, _ := base64.StdEncoding.DecodeString(ww) //成图片文件并把文件写入到buffer
newname := strconv.FormatInt(time.Now().Unix(), 10) // + "_" + filename
err = ioutil.WriteFile(path1+newname+".jpg", ddd, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)
if err != nil {
logs.Error(err)
}
// var filesize int64
// filesize, _ = FileSize(path1)
// filesize = filesize / 1000.0
c.Data["json"] = map[string]interface{}{
"state": "SUCCESS",
"url": "/attachment/" + number + name + "/" + newname + ".jpg",
"title": newname + ".jpg",
"original": newname + ".jpg",
}
c.ServeJSON()
case "listimage":
type List struct {
Url string `json:"url"`
// Source string
// State string
}
type Listimage struct {
State string `json:"state"` //这些第一个字母要大写,否则不出结果
List []List `json:"list"`
Start int `json:"start"`
Total int `json:"total"`
// Name string
// Age int
// Slices []string //slice
// Mapstring map[string]string
// StructArray []List //结构体的切片型
// MapStruct map[string][]List //map:key类型是string或struct,value类型是切片,切片的类型是string或struct
// Desks List
}
// var m map[string]string = make(map[string]string)
// m["Go"] = "No.1"
// m["Java"] = "No.2"
// m["C"] = "No.3"
// fmt.Println(m)
list := []List{
{"/static/upload/1.jpg"},
{"/static/upload/2.jpg"},
// {"upload/1.jpg", "http://a.com/1.jpg", "SUCCESS"},
// {"upload/2.jpg", "http://b.com/2.jpg", "SUCCESS"},
}
// var mm map[string][]List = make(map[string][]List)
// mm["Go"] = list
// mm["Java"] = list
// fmt.Println(mm)
listimage := Listimage{"SUCCESS", list, 1, 21}
// beego.Info(listimage){SUCCESS [{/static/upload/1.jpg} {/static/upload/2.jpg}] 1 21}
// fmt.Println(listimage)
// b, _ := json.Marshal(listimage)
// mystruct := { ... }
// c.Data["jsonp"] = listimage
// beego.Info(string(b)){"State":"SUCCESS","List":[{"Url":"/static/upload/1.jpg"},{"Url":"/static/upload/2.jpg"}],"Start":1,"Total":21}
// c.ServeJSONP()
c.Data["json"] = listimage
c.ServeJSON()
// c.Data["json"] = map[string]interface{}{"State":"SUCCESS","List":[{"Url":"/static/upload/1.jpg"},{"Url":"/static/upload/2.jpg"}],"Start":1,"Total":21}
// 需要支持callback参数,返回jsonp格式
// {
// "state": "SUCCESS",
// "list": [{
// "url": "upload/1.jpg"
// }, {
// "url": "upload/2.jpg"
// }, ],
// "start": 20,
// "total": 100
// }
case "catchimage":
type List struct {
Url string `json:"url"`
Source string `json:"source"`
State string `json:"state"`
}
type Catchimage struct {
State string `json:"state"` //这些第一个字母要大写,否则不出结果
List []List `json:"list"`
// Start int
// Total int
// Name string
// Age int
// Slices []string //slice
// Mapstring map[string]string
// StructArray []List //结构体的切片型
// MapStruct map[string][]List //map:key类型是string或struct,value类型是切片,切片的类型是string或struct
// Desks List
}
// var m map[string]string = make(map[string]string)
// m["Go"] = "No.1"
// m["Java"] = "No.2"
// m["C"] = "No.3"
// fmt.Println(m)
list := []List{
{"/static/upload/1.jpg", "https://pic2.zhimg.com/7c4a389acaa008a6d1fe5a0083c86975_b.png", "SUCCESS"},
{"/static/upload/2.jpg", "https://pic2.zhimg.com/7c4a389acaa008a6d1fe5a0083c86975_b.png", "SUCCESS"},
// {"upload/1.jpg", "http://a.com/1.jpg", "SUCCESS"},
// {"upload/2.jpg", "http://b.com/2.jpg", "SUCCESS"},
}
// var mm map[string][]List = make(map[string][]List)
// mm["Go"] = list
// mm["Java"] = list
// fmt.Println(mm)
catchimage := Catchimage{"SUCCESS", list}
// beego.Info(catchimage){SUCCESS [{/static/upload/1.jpg} {/static/upload/2.jpg}] 1 21}
// fmt.Println(catchimage)
// b, _ := json.Marshal(catchimage)
// mystruct := { ... }
// c.Data["jsonp"] = catchimage
// beego.Info(string(b)){"State":"SUCCESS","List":[{"Url":"/static/upload/1.jpg"},{"Url":"/static/upload/2.jpg"}],"Start":1,"Total":21}
// c.ServeJSONP()
c.Data["json"] = catchimage
c.ServeJSON()
file, header, err := c.GetFile("source") // r.FormFile("upfile")
// beego.Info(header.Filename)
if err != nil {
panic(err)
}
defer file.Close()
filename := strings.Replace(uuid.NewUUID().String(), "-", "", -1) + path.Ext(header.Filename)
err = os.MkdirAll(path.Join("static", "upload"), 0775)
if err != nil {
panic(err)
}
outFile, err := os.Create(path.Join("static", "upload", filename))
if err != nil {
panic(err)
}
defer outFile.Close()
io.Copy(outFile, file)
}
}
// 这个没用
func UploadImg(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("upfile")
if err != nil {
panic(err)
}
defer file.Close()
filename := strings.Replace(uuid.NewUUID().String(), "-", "", -1) + path.Ext(header.Filename)
err = os.MkdirAll(path.Join("static", "upload"), 0775)
if err != nil {
panic(err)
}
outFile, err := os.Create(path.Join("static", "upload", filename))
if err != nil {
panic(err)
}
defer outFile.Close()
io.Copy(outFile, file)
b, err := json.Marshal(map[string]string{
"url": fmt.Sprintf("/static/upload/%s", filename), //保存后的文件路径
"title": "", //文件描述,对图片来说在前端会添加到title属性上
"original": header.Filename, //原始文件名
"state": "SUCCESS", //上传状态,成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
})
if err != nil {
panic(err)
}
fmt.Println(string(b))
w.Write(b)
}
// 添加文章里的图片上传
func (c *FroalaController) UploadImg() {
// 登录
_, _, _, _, islogin := checkprodRole(c.Ctx)
if !islogin {
c.Data["json"] = map[string]interface{}{"state": "ERROR", "data": "用户未登录!"}
c.ServeJSON()
}
//解析表单
pid := c.GetString("pid")
// beego.Info(pid)
//pid转成64为
pidNum, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
logs.Error(err)
}
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(pidNum)
if err != nil {
logs.Error(err)
}
// beego.Info(DiskDirectory)
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
// beego.Info(h.Filename)
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
// err = ioutil.WriteFile(path1+newname+".jpg", ddd, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)
// if err != nil {
// logs.Error(err)
// }
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
var filesize int64
if h != nil {
//保存附件
path = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(path)
filesize = filesize / 1000.0
c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "link": Url + newname, "title": "111", "original": "demo.jpg"}
c.ServeJSON()
} else {
c.Data["json"] = map[string]interface{}{"state": "ERROR", "link": "", "title": "", "original": ""}
c.ServeJSON()
}
}
// @Title post wx artile img by catalogId
// @Description post article img by catalogid
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /uploadwximg [post]
// 微信wx添加文章里的图片上传_独立上传图片模式
func (c *FroalaController) UploadWxImg() {
//解析表单
pid, err := web.AppConfig.String("wxcatalogid") //"26159" //c.GetString("pid")
if err != nil {
logs.Error(err)
}
//pid转成64为
pidNum, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
logs.Error(err)
}
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(pidNum)
if err != nil {
logs.Error(err)
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
var filesize int64
if h != nil {
//保存附件
path = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(path)
filesize = filesize / 1000.0
c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "link": Url + newname, "title": "111", "original": "demo.jpg"}
c.ServeJSON()
} else {
c.Data["json"] = map[string]interface{}{"state": "ERROR", "link": "", "title": "", "original": ""}
c.ServeJSON()
}
}
// @Title post wx artile img by catalogId
// @Description post article img by catalogid
// @Param projectid query string true "The projectid of wxeditor"
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /uploadwxeditorimg [post]
// 微信wx添加文章里的图片上传_小程序富文本里的上传图片
func (c *FroalaController) UploadWxEditorImg() {
var ProjectId int64
var err error
projectid := c.GetString("projectid")
if projectid != "" {
ProjectId, err = strconv.ParseInt(projectid, 10, 64)
if err != nil {
logs.Error(err)
}
}
//解析表单
// pid := web.AppConfig.String("wxcatalogid") //"26159" //c.GetString("pid")
// //pid转成64为
// pidNum, err := strconv.ParseInt(pid, 10, 64)
// if err != nil {
// logs.Error(err)
// }
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(ProjectId)
if err != nil {
logs.Error(err)
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
nanoname := strconv.FormatInt(time.Now().UnixNano(), 10)
newname := nanoname + fileSuffix // + "_" + filename
small_newname := nanoname + "_small" + fileSuffix
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var imagepath, new_imagepath string
var filesize int64
if h != nil {
//保存附件
imagepath = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
new_imagepath = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + small_newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("file", imagepath) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(imagepath)
filesize = filesize / 1000.0
//*****压缩图片***
file, err := os.Open(imagepath)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
defer file.Close()
var img image.Image
var typeImage int
// ext := filepath.Ext(imagepath)
if strings.EqualFold(fileSuffix, ".jpg") || strings.EqualFold(fileSuffix, ".jpeg") {
img, err = jpeg.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 0
} else if strings.EqualFold(fileSuffix, ".png") {
// decode png into image.Image
img, err = png.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 1
} else if strings.EqualFold(fileSuffix, ".gif") {
img, err = gif.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 2
}
// file.Close()
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(1000, 0, img, resize.Lanczos3)
// m := resize.Thumbnail(1000, 0, img, resize.Lanczos3)
out, err := os.Create(new_imagepath)
defer out.Close()
if err != nil {
logs.Error(err)
}
if typeImage == 0 {
err = jpeg.Encode(out, m, &jpeg.Options{Quality: 80})
if err != nil {
logs.Error(err)
}
} else {
err = png.Encode(out, m)
if err != nil {
logs.Error(err)
}
}
c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "link": Url + small_newname, "title": "111", "original": "demo.jpg"}
c.ServeJSON()
} else {
c.Data["json"] = map[string]interface{}{"state": "ERROR", "link": "", "title": "", "original": ""}
c.ServeJSON()
}
}
// @Title post wx artile img by catalogId
// @Description post article img by catalogid
// @Param id path string true "The id of project"
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /uploadwximgs/:id [post]
// 微信wx添加文章里的图片上传——这个鲁班宝,但这个id更通用
func (c *FroalaController) UploadWxImgs() {
//解析表单
pid := c.Ctx.Input.Param(":id")
// pid := web.AppConfig.String("wxcatalogid") //"26159" //c.GetString("pid")
//pid转成64为
pidNum, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
logs.Error(err)
}
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(pidNum)
if err != nil {
logs.Error(err)
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
nanoname := strconv.FormatInt(time.Now().UnixNano(), 10)
newname := nanoname + fileSuffix // + "_" + filename
small_newname := nanoname + "_small" + fileSuffix
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var imagepath, new_imagepath string
var filesize int64
if h != nil {
//保存附件
imagepath = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
new_imagepath = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + small_newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("file", imagepath) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
}
filesize, _ = FileSize(imagepath)
filesize = filesize / 1000.0
//*****压缩图片***
file, err := os.Open(imagepath)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
defer file.Close()
var img image.Image
var typeImage int
// ext := filepath.Ext(imagepath)
if strings.EqualFold(fileSuffix, ".jpg") || strings.EqualFold(fileSuffix, ".jpeg") {
img, err = jpeg.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 0
} else if strings.EqualFold(fileSuffix, ".png") {
// decode png into image.Image
img, err = png.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 1
} else if strings.EqualFold(fileSuffix, ".gif") {
img, err = gif.Decode(file)
if err != nil {
// log.Fatal(err)
logs.Error(err)
}
typeImage = 2
}
// file.Close()
// 用https://github.com/gographics/imagick 进行裁剪图片 https://www.cnblogs.com/Torrance/p/8877039.html
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
// m := resize.Resize(1000, 0, img, resize.Lanczos3)
m := resize.Resize(500, 0, img, resize.Lanczos3)
// m := resize.Thumbnail(1000, 0, img, resize.Lanczos3)
out, err := os.Create(new_imagepath)
defer out.Close()
if err != nil {
logs.Error(err)
}
if typeImage == 0 {
err = jpeg.Encode(out, m, &jpeg.Options{Quality: 80})
if err != nil {
logs.Error(err)
}
} else {
err = png.Encode(out, m)
if err != nil {
logs.Error(err)
}
}
// 开始图片鉴别
app_version := c.GetString("app_version")
accessToken, _, _, err := utils.GetAccessToken(app_version)
if err != nil {
logs.Error(err)
c.Data["json"] = map[string]interface{}{"info": "ERROR", "data": err}
c.ServeJSON()
return
}
// 把上传文件转为byte
// file, header, err := ctx.Request.FormFile("file")
// defer file.Close()
// if err != nil {
// return nil, err
// }
// buf := bytes.NewBuffer(nil)
// if _, err := io.Copy(buf, file); err != nil {
// return nil, err
// }
// fmt.Println(buf.Bytes())
// 将os.File输出转换为String
var buf bytes.Buffer
io.Copy(&buf, out)
// asString := string(buf.Bytes())
errcode, errmsg, err := utils.ImgSecCheck(buf.Bytes(), accessToken)
// beego.Info(errcode)
// beego.Info(errmsg)
if err != nil {
logs.Error(err)
c.Data["json"] = map[string]interface{}{"info": err, "state": "ERROR", "data": errmsg}
c.ServeJSON()
} else if errcode != 87014 {
c.Data["json"] = map[string]interface{}{"info": "SUCCESS", "state": "SUCCESS", "link": Url + small_newname, "title": small_newname, "original": "demo.jpg"}
c.ServeJSON()
}
} else {
c.Data["json"] = map[string]interface{}{"info": "ERROR", "state": "ERROR", "data": "文件为空!"}
c.ServeJSON()
}
}
// @Title post wx user avatar
// @Description post user avatar
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /uploadwxavatar [post]
// 微信wx添加用户头像上传
func (c *FroalaController) UploadWxAvatar() {
var user models.User
var err error
openID := c.GetSession("openID")
if openID != nil {
user, err = models.GetUserByOpenID(openID.(string))
if err != nil {
logs.Error(err)
}
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
err = os.MkdirAll("./static/avatar/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
var filesize int64
if h != nil {
//保存附件
path = "./static/avatar/" + newname
Url := "/static/avatar/"
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
c.Data["json"] = map[string]interface{}{"state": "ERROR", "photo": "", "title": "", "original": ""}
c.ServeJSON()
} else {
filesize, _ = FileSize(path)
filesize = filesize / 1000.0
_, err = models.AddUserAvator(user.Id, Url+newname)
if err != nil {
logs.Error(err)
}
wxsite, err := web.AppConfig.String("wxreqeustsite")
if err != nil {
logs.Error(err)
}
// c.Data["json"] = map[string]interface{}{"errNo": 1, "msg": "success", "photo": wxsite + Url + newname, "title": newname, "original": newname}
// c.ServeJSON()
c.Ctx.WriteString(wxsite + Url + newname)
}
} else {
c.Data["json"] = map[string]interface{}{"errNo": 0, "state": "ERROR", "photo": "", "title": "", "original": ""}
c.ServeJSON()
}
}
// @Title post wx user avatar
// @Description post user avatar
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 articl not found
// @router /uploadappreciationphoto [post]
// 小程序wx添加用户赞赏码上传
func (c *FroalaController) UploadAppreciationPhoto() {
var user models.User
var err error
openID := c.GetSession("openID")
if openID != nil {
user, err = models.GetUserByOpenID(openID.(string))
if err != nil {
logs.Error(err)
}
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
err = os.MkdirAll("./static/appreciation/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
var filesize int64
if h != nil {
//保存附件
path = "./static/appreciation/" + newname
Url := "/static/appreciation/"
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
c.Data["json"] = map[string]interface{}{"state": "ERROR", "photo": "", "title": "", "original": ""}
c.ServeJSON()
} else {
filesize, _ = FileSize(path)
filesize = filesize / 1000.0
_, err = models.AddUserAppreciation(user.Id, Url+newname)
if err != nil {
logs.Error(err)
}
wxsite, err := web.AppConfig.String("wxreqeustsite")
if err != nil {
logs.Error(err)
}
// c.Data["json"] = map[string]interface{}{"errNo": 1, "msg": "success", "photo": wxsite + Url + newname, "title": newname, "original": newname}
// c.ServeJSON()
c.Ctx.WriteString(wxsite + Url + newname)
}
} else {
c.Data["json"] = map[string]interface{}{"errNo": 0, "state": "ERROR", "photo": "", "title": "", "original": ""}
c.ServeJSON()
}
}
// @Title post wx video by catalogId
// @Description post video by catalogid
// @Param id path string true "The id of project"
// @Param desc query string true "The descript of video"
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 article not found
// @router /uploadwxvideo/:id [post]
// 微信wx添加视频
func (c *FroalaController) UploadWxVideo() {
var user models.User
var err error
openID := c.GetSession("openID")
if openID != nil {
user, err = models.GetUserByOpenID(openID.(string))
if err != nil {
logs.Error(err)
}
} else {
// c.Data["json"] = map[string]interface{}{"info": "用户未登录", "id": 0}
// c.ServeJSON()
// return //本地调试的时候,由于小程序无法登陆服务器,所以这里要注释掉。
}
c.Ctx.Request.Body = http.MaxBytesReader(c.Ctx.ResponseWriter, c.Ctx.Request.Body, MAX_UPLOAD_SIZE)
if err := c.Ctx.Request.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
// sendErrorResponse(w, http.StatusBadRequest, "File is too big")
return
}
//解析表单
content := c.GetString("desc")
pid := c.Ctx.Input.Param(":id")
// pid := web.AppConfig.String("wxcatalogid") //"26159" //c.GetString("pid")
//pid转成64为
pidNum, err := strconv.ParseInt(pid, 10, 64)
if err != nil {
logs.Error(err)
}
//根据proj的parentIdpath
Url, DiskDirectory, err := GetUrlPath(pidNum)
if err != nil {
logs.Error(err)
}
//获取上传的文件
_, h, err := c.GetFile("file")
if err != nil {
logs.Error(err)
}
fileSuffix := path.Ext(h.Filename)
// random_name
newname := strconv.FormatInt(time.Now().UnixNano(), 10) + fileSuffix // + "_" + filename
year, month, _ := time.Now().Date()
err = os.MkdirAll(DiskDirectory+"/"+strconv.Itoa(year)+month.String()+"/", 0777) //..代表本当前exe文件目录的上级,.表示当前目录,没有.表示盘的根目录
if err != nil {
logs.Error(err)
}
var path string
// var filesize int64
if h != nil {
//保存附件
path = DiskDirectory + "/" + strconv.Itoa(year) + month.String() + "/" + newname
Url = "/" + Url + "/" + strconv.Itoa(year) + month.String() + "/"
err = c.SaveToFile("file", path) //.Join("attachment", attachment)) //存文件 WaterMark(path) //给文件加水印
if err != nil {
logs.Error(err)
return
}
//根据pid查出项目id
proj, err := models.GetProj(pidNum)
if err != nil {
logs.Error(err)
}
//写入数据表
vid, err := models.CreateVideo(proj.Id, user.Id, content, newname, Url+newname)
if err != nil {
logs.Error(err)
c.Data["json"] = map[string]interface{}{"state": "ERROR", "info": "ERR", "id": vid}
c.ServeJSON()
} else {
c.Data["json"] = vid
// c.Data["json"] = map[string]interface{}{"state": "SUCCESS", "info": "SUCCESS", "link": Url + newname, "id": vid}
c.ServeJSON()
}
// filesize, _ = FileSize(path)
// filesize = filesize / 1000.0
}
}
// @Title post wx videoCover by videoid
// @Description post videoCover by videoid
// @Param id query string true "The id of video"
// @Success 200 {object} SUCCESS
// @Failure 400 Invalid page supplied
// @Failure 404 article not found
// @router /uploadwxvideocover/:id [post]
// 微信wx添加视频封面
func (c *FroalaController) UploadWxVideoCover() {
//解析表单
vid := c.Ctx.Input.Param(":id")
// pidNum, err := strconv.ParseInt(id, 10, 64)
// if err != nil {
// logs.Error(err)
// }
//根据proj的parentIdpath
// Url, DiskDirectory, err := GetUrlPath(pidNum)
// if err != nil {
// logs.Error(err)
// }
// vid := c.GetString("id")
//pid转成64为
vidNum, err := strconv.ParseInt(vid, 10, 64)
if err != nil {
logs.Error(err)
}
video, err := models.GetVideobyId(vidNum)
if err != nil {