-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_mode_handler.go
57 lines (48 loc) · 1.3 KB
/
dev_mode_handler.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
package coolCaptcha
import (
"fmt"
"image/gif"
"os"
"time"
"github.com/fogleman/gg"
)
// devModeHandler
// @Description: In the development mode, the generated image is saved as a local file for easy viewing
// @receiver c
// @param dc
func (c *Config) devModeHandler(dc *gg.Context) {
outputFolderPath := "output"
if _, err := os.Stat(outputFolderPath); os.IsNotExist(err) {
err := os.Mkdir(outputFolderPath, 0700)
if err != nil {
panic(err)
}
}
outputFilePath := fmt.Sprintf("%s/%s.png", outputFolderPath, time.Now().Format("2006-01-02 15:04:05"))
if saveErr := dc.SavePNG(outputFilePath); saveErr != nil {
panic(saveErr)
}
}
// devModelHandlerForGif
// @Description: In the development mode, the generated image is saved as a local file for easy viewing
// @receiver c
// @param gifData
func (c *Config) devModelHandlerForGif(gifData *gif.GIF) {
outputFolderPath := "output"
if _, err := os.Stat(outputFolderPath); os.IsNotExist(err) {
err := os.Mkdir(outputFolderPath, 0700)
if err != nil {
panic(err)
}
}
outputFilePath := fmt.Sprintf("%s/%s.gif", outputFolderPath, time.Now().Format("2006-01-02 15:04:05"))
f, err := os.Create(outputFilePath)
if err != nil {
fmt.Println(err)
}
defer func() { _ = f.Close() }()
err = gif.EncodeAll(f, gifData)
if err != nil {
return
}
}