Skip to content

Commit 6255bb4

Browse files
committed
v1
1 parent bc2fd64 commit 6255bb4

File tree

3 files changed

+89
-51
lines changed

3 files changed

+89
-51
lines changed

fileupload.go

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package fileupload
22

33
import (
4+
"bufio"
45
"bytes"
5-
"context"
66
"encoding/json"
7+
"errors"
78
"io"
9+
"io/ioutil"
810
"mime/multipart"
911
"net/http"
1012
"net/url"
1113
"os"
1214
"strconv"
1315

14-
"cloud.google.com/go/storage"
1516
"github.com/kennygrant/sanitize"
1617
)
1718

@@ -43,44 +44,14 @@ func FromRequestToFile(req *http.Request, path string) (string, string, error) {
4344
return filename, fullpath, nil
4445
}
4546

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-
8147
func FromBuffer(name string, path string, body io.Reader) (string, string, error) {
8248
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
8455
f, err := os.OpenFile(fullpath, os.O_RDWR|os.O_CREATE, 0666)
8556
if err != nil {
8657
return "", "", err
@@ -91,7 +62,7 @@ func FromBuffer(name string, path string, body io.Reader) (string, string, error
9162
if err != nil {
9263
return "", "", err
9364
}
94-
return filename, fullpath, nil
65+
return name, fullpath, nil
9566
}
9667

9768
func getValidFileName(path string, filename string) string {
@@ -145,7 +116,7 @@ func (o *operations) last() *operation {
145116
return o.Ops[len(o.Ops)-1]
146117
}
147118

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) {
149120
err := req.ParseMultipartForm(32)
150121
if err != nil {
151122
return nil, err
@@ -155,11 +126,10 @@ func ProcessedImageFromRequest(req *http.Request, imageType string, maxWidth int
155126
return nil, err
156127
}
157128
defer file.Close()
158-
159-
return ProcessedImage(file, imageType, maxWidth, quality, convert)
129+
return ProcessedImage(file, imageType, width, height, quality, convert)
160130
}
161131

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) {
163133
ops := &operations{}
164134

165135
originalImageType := "jpg"
@@ -177,46 +147,98 @@ func ProcessedImage(r io.Reader, imageType string, maxWidth int, quality int, co
177147
}
178148

179149
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
185153
ops.last().addParam("quality", quality)
186154
// ops.last().addParam("compression", quality)
187155
bOps, err := json.Marshal(ops.Ops)
188156
if err != nil {
189157
return nil, err
190158
}
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))
193160

194161
var b bytes.Buffer
195162
w := multipart.NewWriter(&b)
196163
fw, err := w.CreateFormFile("file", "filename_placeholder."+originalImageType)
197164
if err != nil {
198165
return nil, err
166+
// ctx.ErrorJSON(http.StatusOK, "couldn't create form file ", err)
199167
}
200168
_, err = io.Copy(fw, r)
201169
if err != nil {
170+
// ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
202171
return nil, err
203172
}
204173
err = w.Close()
205174
if err != nil {
175+
// ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
206176
return nil, err
207177
}
208178

209179
req, err := http.NewRequest("POST", endpoint, &b)
210180
if err != nil {
181+
// ctx.ErrorJSON(http.StatusOK, "failed to copy from reqFile", err)
211182
return nil, err
212183
}
213184
req.Header.Set("Content-Type", w.FormDataContentType())
214185

215186
client := &http.Client{}
216187
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)
217198
if err != nil {
199+
// ctx.ErrorJSON(http.StatusInternalServerError, "Failed to create image", err)
218200
return nil, err
219201
}
220202

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)
222244
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/nerdynz/fileupload
2+
3+
go 1.12
4+
5+
require (
6+
github.com/kennygrant/sanitize v1.2.4
7+
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd // indirect
8+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
2+
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
3+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4+
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd h1:QPwSajcTUrFriMF1nJ3XzgoqakqQEsnZf9LdXdi2nkI=
5+
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
6+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)