forked from go-catupiry/files_processor_imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
189 lines (157 loc) · 4.45 KB
/
client.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
package files_processor_imaginary
import (
"fmt"
"os"
files_processor "github.com/go-bolo/files/processor"
"github.com/google/uuid"
"github.com/imroc/req/v3"
"github.com/sirupsen/logrus"
)
type ClientConfiguration struct {
URL string
}
type Client struct {
HTTP *req.Client
Cfg *ClientConfiguration
}
func NewClient(cfg *ClientConfiguration) *Client {
c := Client{
HTTP: req.C(),
Cfg: cfg,
}
return &c
}
func (c *Client) Resize(sourcePath, destPath, fileName string, opts files_processor.Options) error {
if _, ok := opts["url"]; ok {
err := c.ResizeFromWeb(sourcePath, destPath, fileName, opts)
if err != nil {
return err
}
} else {
err := c.ResizeFromLocalhost(sourcePath, destPath, opts)
if err != nil {
return err
}
}
return nil
}
func (c *Client) ResizeFromWeb(sourcePath, destPath, fileName string, opts files_processor.Options) error {
url := c.Cfg.URL + "/resize"
if f, ok := opts["format"]; ok {
opts["type"] = f
} else if _, ok := opts["type"]; !ok {
opts["type"] = "webp"
}
if _, ok := opts["nocrop"]; !ok {
opts["nocrop"] = "false"
}
res, err := c.HTTP.R().
SetQueryParams(opts).
SetOutputFile(destPath).
Get(url)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
"file_name": fileName,
"source_path": sourcePath,
"dest_path": destPath,
}).Error("ResizeFromWeb error", err)
return fmt.Errorf("ResizeFromWeb error on access imaginary api: %w", err)
}
if res.IsErrorState() {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
"status": res.StatusCode,
"dump": res.Dump(),
"file_name": fileName,
"source_path": sourcePath,
"dest_path": destPath,
}).Error("ResizeFromWeb Response error", err)
return fmt.Errorf(res.String())
}
return nil
}
func (c *Client) ResizeFromLocalhost(sourcePath string, destPath string, opts files_processor.Options) error {
url := c.Cfg.URL + "/resize"
f, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("ResizeFromLocalhost error on open file from sourcePath: %w", err)
}
defer f.Close()
if f, ok := opts["format"]; ok {
opts["type"] = f
} else if _, ok := opts["type"]; !ok {
opts["type"] = "webp"
}
if _, ok := opts["nocrop"]; !ok {
opts["nocrop"] = "false"
}
id := uuid.New()
res, err := c.HTTP.R().
SetQueryParams(opts).
SetFileReader("file", id.String(), f).
SetOutputFile(destPath).
Post(url)
// execution error
if err != nil {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
"file_id": id.String(),
"source_path": sourcePath,
"dest_path": destPath,
}).Error("ResizeFromLocalhost error on access imaginary api", err)
return fmt.Errorf("ResizeFromLocalhost error on access imaginary api: %w", err)
}
// http error
if res.IsErrorState() {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
"status": res.StatusCode,
"dump": res.Dump(),
"source_path": sourcePath,
"dest_path": destPath,
}).Error("ResizeFromLocalhost response error", err)
return fmt.Errorf(res.String())
}
return nil
}
// DownloadFile
// Usage:
// originalPath := path.Join(os.TempDir(), fileName) + "_original"
// defer os.Remove(originalPath)
// DownloadFile(fileURL, originalPath, fileName string) (error)
func (c *Client) DownloadFile(fileURL, donwloadedFilePath, fileName string) error {
res, err := c.HTTP.R().
SetOutputFile(donwloadedFilePath).
Get(fileURL)
// execution error
if err != nil {
return fmt.Errorf("resizeFromLocalhost error on download original image: %w", err)
}
// http error
if res.IsErrorState() {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
"status": res.StatusCode,
"body": res.String(),
"file_url": fileURL,
}).Error("resizeFromLocalhost Response error", err)
return fmt.Errorf(res.String())
}
return nil
}
// // TODO!
// crop - Same as /crop endpoint.
// smartcrop - Same as /smartcrop endpoint.
// enlarge - Same as /enlarge endpoint.
// extract - Same as /extract endpoint.
// rotate - Same as /rotate endpoint.
// autorotate - Same as /autorotate endpoint.
// flip - Same as /flip endpoint.
// flop - Same as /flop endpoint.
// thumbnail - Same as /thumbnail endpoint.
// zoom - Same as /zoom endpoint.
// convert - Same as /convert endpoint.
// watermark - Same as /watermark endpoint.
// watermarkimage - Same as /watermarkimage endpoint.
// blur - Same as /blur endpoint.