1
1
package fileupload
2
2
3
3
import (
4
+ "bufio"
4
5
"bytes"
5
- "context"
6
6
"encoding/json"
7
+ "errors"
7
8
"io"
9
+ "io/ioutil"
8
10
"mime/multipart"
9
11
"net/http"
10
12
"net/url"
11
13
"os"
12
14
"strconv"
13
15
14
- "cloud.google.com/go/storage"
15
16
"github.com/kennygrant/sanitize"
16
17
)
17
18
@@ -43,44 +44,14 @@ func FromRequestToFile(req *http.Request, path string) (string, string, error) {
43
44
return filename , fullpath , nil
44
45
}
45
46
46
- func FromRequestToGoogleBucket (req * http.Request , bucketName string ) (string , string , error ) {
47
- req .ParseMultipartForm (32 )
48
- file , handler , err := req .FormFile ("file" )
49
- if err != nil {
50
- return "" , "" , err
51
- }
52
- defer file .Close ()
53
-
54
- ctx := context .Background ()
55
- client , err := storage .NewClient (ctx )
56
- if err != nil {
57
- // TODO: Handle error.
58
- }
59
- bkt := client .Bucket (bucketName )
60
-
61
- filename := handler .Filename
62
- // fullpath := path + filename
63
-
64
- obj := bkt .Object (filename )
65
- if err != nil {
66
- return "" , "" , err
67
- }
68
- w := obj .NewWriter (ctx )
69
- _ , err = io .Copy (w , file )
70
- if err != nil {
71
- return "" , "" , err
72
- }
73
-
74
- if err := w .Close (); err != nil {
75
- return "" , "" , err
76
- }
77
-
78
- return filename , bucketName , nil
79
- }
80
-
81
47
func FromBuffer (name string , path string , body io.Reader ) (string , string , error ) {
82
48
filename := getValidFileName (path , name )
83
- fullpath := path + filename
49
+ return FromBufferNoSanitize (filename , path , body )
50
+ }
51
+
52
+ func FromBufferNoSanitize (name string , path string , body io.Reader ) (string , string , error ) {
53
+ // filename := getValidFileName(path, name)
54
+ fullpath := path + name
84
55
f , err := os .OpenFile (fullpath , os .O_RDWR | os .O_CREATE , 0666 )
85
56
if err != nil {
86
57
return "" , "" , err
@@ -91,7 +62,7 @@ func FromBuffer(name string, path string, body io.Reader) (string, string, error
91
62
if err != nil {
92
63
return "" , "" , err
93
64
}
94
- return filename , fullpath , nil
65
+ return name , fullpath , nil
95
66
}
96
67
97
68
func getValidFileName (path string , filename string ) string {
@@ -145,7 +116,7 @@ func (o *operations) last() *operation {
145
116
return o .Ops [len (o .Ops )- 1 ]
146
117
}
147
118
148
- func ProcessedImageFromRequest (req * http.Request , imageType string , maxWidth int , quality int , convert bool ) (* http. Response , error ) {
119
+ func ProcessedImageFromRequest (req * http.Request , imageType string , width int , height int , quality int , convert bool ) ([] byte , error ) {
149
120
err := req .ParseMultipartForm (32 )
150
121
if err != nil {
151
122
return nil , err
@@ -155,11 +126,10 @@ func ProcessedImageFromRequest(req *http.Request, imageType string, maxWidth int
155
126
return nil , err
156
127
}
157
128
defer file .Close ()
158
-
159
- return ProcessedImage (file , imageType , maxWidth , quality , convert )
129
+ return ProcessedImage (file , imageType , width , height , quality , convert )
160
130
}
161
131
162
- func ProcessedImage (r io.Reader , imageType string , maxWidth int , quality int , convert bool ) (* http. Response , error ) {
132
+ func ProcessedImage (r io.Reader , imageType string , width int , height int , quality int , convert bool ) ([] byte , error ) {
163
133
ops := & operations {}
164
134
165
135
originalImageType := "jpg"
@@ -177,46 +147,98 @@ func ProcessedImage(r io.Reader, imageType string, maxWidth int, quality int, co
177
147
}
178
148
179
149
ops .add ("fit" )
180
- // ops.last().addParam("rotate", "0")
181
- // ops.last().addParam("background", "255,255,255")
182
- ops .last ().addParam ("width" , maxWidth ) //absolute max
183
- ops .last ().addParam ("height" , maxWidth ) // dont need its ratio based
184
- // ops.last().addParam("stripmeta", true) // dont need its ratio based
150
+ ops .last ().addParam ("width" , width ) //absolute max
151
+ ops .last ().addParam ("height" , height ) // dont need its ratio based
152
+ ops .last ().addParam ("stripmeta" , true ) // dont need its ratio based
185
153
ops .last ().addParam ("quality" , quality )
186
154
// ops.last().addParam("compression", quality)
187
155
bOps , err := json .Marshal (ops .Ops )
188
156
if err != nil {
189
157
return nil , err
190
158
}
191
- endpoint := "https://images.nerdy.co.nz/pipeline?operations=" + url .QueryEscape (string (bOps ))
192
- // endpoint = "https://images.nerdy.co.nz/fit?width=200&height=200"
159
+ endpoint := os .Getenv ("IMAGE_PROCESSING_ENDPOINT" ) + "pipeline?operations=" + url .QueryEscape (string (bOps ))
193
160
194
161
var b bytes.Buffer
195
162
w := multipart .NewWriter (& b )
196
163
fw , err := w .CreateFormFile ("file" , "filename_placeholder." + originalImageType )
197
164
if err != nil {
198
165
return nil , err
166
+ // ctx.ErrorJSON(http.StatusOK, "couldn't create form file ", err)
199
167
}
200
168
_ , err = io .Copy (fw , r )
201
169
if err != nil {
170
+ // ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
202
171
return nil , err
203
172
}
204
173
err = w .Close ()
205
174
if err != nil {
175
+ // ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
206
176
return nil , err
207
177
}
208
178
209
179
req , err := http .NewRequest ("POST" , endpoint , & b )
210
180
if err != nil {
181
+ // ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
211
182
return nil , err
212
183
}
213
184
req .Header .Set ("Content-Type" , w .FormDataContentType ())
214
185
215
186
client := & http.Client {}
216
187
res , err := client .Do (req )
188
+ if err != nil || res .StatusCode != 200 {
189
+ // ctx.ErrorJSON(http.StatusInternalServerError, "bad request", err)
190
+ return nil , err
191
+ }
192
+ defer res .Body .Close ()
193
+
194
+ var finalBts bytes.Buffer
195
+ wr := bufio .NewWriter (& finalBts )
196
+ // we read from tee reader as it hasn't already done its scan
197
+ _ , err = io .Copy (wr , res .Body )
217
198
if err != nil {
199
+ // ctx.ErrorJSON(http.StatusInternalServerError, "Failed to create image", err)
218
200
return nil , err
219
201
}
220
202
221
- return res , nil
203
+ return finalBts .Bytes (), nil
204
+ }
205
+
206
+ func FromBytes (name string , path string , b []byte ) (string , string , error ) {
207
+ return FromBuffer (name , path , bytes .NewReader (b ))
208
+ }
209
+
210
+ func FromBytesNoSanitize (name string , path string , b []byte ) (string , string , error ) {
211
+ return FromBufferNoSanitize (name , path , bytes .NewReader (b ))
212
+ }
213
+
214
+ func DownloadFile (url string ) ([]byte , error ) {
215
+ resp , err := http .Get (url )
216
+ if err != nil {
217
+ return nil , err
218
+ }
219
+ if resp .StatusCode != 200 {
220
+ return nil , errors .New ("download failed for " + url )
221
+ }
222
+ defer resp .Body .Close ()
223
+ b , err := ioutil .ReadAll (resp .Body )
224
+ if err != nil {
225
+ return nil , err
226
+ }
227
+ return b , err
228
+ }
229
+
230
+ func DownloadToFile (url string , filename string , filepath string ) (string , string , error ) {
231
+ b , err := DownloadFile (url )
232
+ if err != nil {
233
+ return "" , "" , err
234
+ }
235
+ return FromBytes (filename , filepath , b )
236
+ }
237
+
238
+ func DownloadToFileNoSanitize (url string , filename string , filepath string ) (string , string , error ) {
239
+ b , err := DownloadFile (url )
240
+ if err != nil {
241
+ return "" , "" , err
242
+ }
243
+ return FromBytesNoSanitize (filename , filepath , b )
222
244
}
0 commit comments