feat: handle file upload
Some checks failed
Go Test / test (push) Failing after 45s
Go Bump / bump (push) Successful in 23s

This commit is contained in:
xuu
2024-02-13 09:27:26 -07:00
parent f9c064e948
commit 54fe38821c
3 changed files with 23 additions and 13 deletions

View File

@@ -29,8 +29,8 @@ type image struct {
store string
maxSize int64
m_image_get metric.Int64Counter
m_image_post metric.Int64Counter
m_image_get metric.Int64Counter
m_image_post metric.Int64Counter
m_image_error metric.Int64Counter
}
@@ -68,7 +68,6 @@ func New(ctx context.Context, store string, maxSize int64) (a *image, err error)
)
err = errors.Join(err, merr)
return a, err
}
func (a *image) RegisterHTTP(mux *http.ServeMux) {
@@ -119,6 +118,9 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
defer r.Body.Close()
// var buf bytes.Buffer
// r.Body = io.NopCloser(io.TeeReader(r.Body, &buf))
var fd io.ReadCloser = r.Body
if r.URL.Path == "/3/upload" {
span.AddEvent("Imgur Emulation")
@@ -128,15 +130,19 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rdr = base64.NewDecoder(base64.StdEncoding, rdr)
}
fd = io.NopCloser(rdr)
} else if fd, _, err = r.FormFile("image"); err != nil {
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer fd.Close()
} else if mp, hd, err := r.FormFile("image"); err == nil {
defer mp.Close()
span.AddEvent(fmt.Sprint(hd))
fd = mp
} else if mp, hd, err := r.FormFile("file"); err == nil {
defer mp.Close()
span.AddEvent(fmt.Sprint(hd))
fd = mp
} else if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusCreated)
}