chore: fixes to paste ui
This commit is contained in:
141
image/image.go
141
image/image.go
@@ -13,6 +13,7 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
@@ -58,63 +59,98 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
defer span.End()
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
case http.MethodGet, http.MethodHead:
|
||||
name := strings.TrimPrefix(r.URL.Path, "/")
|
||||
|
||||
a.get(ctx, w, name)
|
||||
case http.MethodPost:
|
||||
var err error
|
||||
var fd io.ReadCloser = r.Body
|
||||
if r.URL.Path == "/3/upload" {
|
||||
fd, _, err = r.FormFile("image")
|
||||
if name != "" {
|
||||
rdr, head, err := a.loadFile(ctx, name)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
writeError(w, err)
|
||||
span.RecordError(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer rdr.Close()
|
||||
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
w.Header().Set("Content-Type", head.Mime)
|
||||
w.Header().Set("ETag", head.ETag)
|
||||
w.Header().Set("Last-Modified", head.Modified.Format(time.RFC850))
|
||||
|
||||
if r.Method == http.MethodHead {
|
||||
return
|
||||
}
|
||||
|
||||
// io.Copy(w, rdr)
|
||||
http.ServeContent(w, r, "", head.Modified, rdr)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
case http.MethodPost:
|
||||
var err error
|
||||
defer r.Body.Close()
|
||||
|
||||
var fd io.ReadCloser = r.Body
|
||||
if r.URL.Path == "/3/upload" {
|
||||
span.AddEvent("Imgur Emulation")
|
||||
if data := r.FormValue("image"); data != "" {
|
||||
var rdr io.Reader = strings.NewReader(data)
|
||||
if t := r.FormValue("type"); t == "base64" {
|
||||
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()
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
length := 0
|
||||
if h := r.Header.Get("Content-Length"); h != "" {
|
||||
if i, err := strconv.Atoi(h); err != nil {
|
||||
length = i
|
||||
}
|
||||
}
|
||||
id, err := a.put(ctx, w, fd, length)
|
||||
switch {
|
||||
case errors.Is(err, ErrGone):
|
||||
w.WriteHeader(http.StatusGone)
|
||||
case errors.Is(err, ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Is(err, ErrReadingContent):
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
case errors.Is(err, ErrUnsupportedType):
|
||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||
id, err := a.put(ctx, fd, length)
|
||||
if err != nil {
|
||||
writeError(w, err)
|
||||
span.RecordError(err)
|
||||
return
|
||||
}
|
||||
|
||||
type data struct{
|
||||
Link string `json:"link"`
|
||||
type data struct {
|
||||
Link string `json:"link"`
|
||||
DeleteHash string `json:"deletehash"`
|
||||
}
|
||||
var resp = struct{
|
||||
Data data `json:"data"`
|
||||
var resp = struct {
|
||||
Data data `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
Status int `json:"status"`
|
||||
Status int `json:"status"`
|
||||
}{
|
||||
Data: data{
|
||||
Link: fmt.Sprintf("https://%s/i/%s", r.Host, id),
|
||||
},
|
||||
Success: true,
|
||||
Status: 200,
|
||||
Status: 200,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
err = json.NewEncoder(w).Encode(resp)
|
||||
span.RecordError(err)
|
||||
|
||||
default:
|
||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *image) get(ctx context.Context, w http.ResponseWriter, name string) error {
|
||||
func (a *image) loadFile(ctx context.Context, name string) (io.ReadSeekCloser, *Header, error) {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
@@ -124,38 +160,44 @@ func (a *image) get(ctx context.Context, w http.ResponseWriter, name string) err
|
||||
fname := filepath.Join(a.store, id)
|
||||
|
||||
if !chkFile(fname) {
|
||||
return fmt.Errorf("%w: %s", ErrNotFound, fname)
|
||||
return nil, nil, fmt.Errorf("%w: %s", ErrNotFound, fname)
|
||||
}
|
||||
|
||||
if chkGone(fname) {
|
||||
return fmt.Errorf("%w: %s", ErrGone, fname)
|
||||
return nil, nil, fmt.Errorf("%w: %s", ErrGone, fname)
|
||||
}
|
||||
|
||||
f, err := os.Open(fname)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, nil, err
|
||||
}
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pr := readutil.NewPreviewReader(f)
|
||||
|
||||
mime, err := readutil.ReadMIME(pr, name)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, nil, err
|
||||
}
|
||||
w.Header().Set("Content-Type", mime)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
_, _ = io.Copy(w, pr.Drain())
|
||||
return nil
|
||||
f.Seek(0,0)
|
||||
|
||||
return f,
|
||||
&Header{Mime: mime, Modified: stat.ModTime(), ETag: name},
|
||||
nil
|
||||
}
|
||||
|
||||
func (a *image) put(ctx context.Context, w http.ResponseWriter, r io.ReadCloser, length int) (string, error) {
|
||||
func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, error) {
|
||||
_, span := lg.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
defer r.Close()
|
||||
|
||||
span.AddEvent("content length", trace.WithAttributes(attribute.Int64("max-size", a.maxSize), attribute.Int("length", length)))
|
||||
|
||||
if length > 0 {
|
||||
if int64(length) > a.maxSize {
|
||||
return "", ErrSizeTooLarge
|
||||
@@ -165,6 +207,7 @@ func (a *image) put(ctx context.Context, w http.ResponseWriter, r io.ReadCloser,
|
||||
rdr := io.LimitReader(r, a.maxSize)
|
||||
pr := readutil.NewPreviewReader(rdr)
|
||||
if !isImageOrVideo(pr) {
|
||||
span.AddEvent("not image")
|
||||
return "", ErrUnsupportedType
|
||||
}
|
||||
rdr = pr.Drain()
|
||||
@@ -172,6 +215,7 @@ func (a *image) put(ctx context.Context, w http.ResponseWriter, r io.ReadCloser,
|
||||
s256 := sha256.New()
|
||||
tmp, err := os.CreateTemp(a.store, "image-")
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
|
||||
}
|
||||
|
||||
@@ -179,6 +223,7 @@ func (a *image) put(ctx context.Context, w http.ResponseWriter, r io.ReadCloser,
|
||||
|
||||
m := io.MultiWriter(s256, tmp)
|
||||
if _, err := io.Copy(m, rdr); err != nil {
|
||||
span.RecordError(err)
|
||||
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
|
||||
}
|
||||
tmp.Close()
|
||||
@@ -186,7 +231,7 @@ func (a *image) put(ctx context.Context, w http.ResponseWriter, r io.ReadCloser,
|
||||
id := base64.RawURLEncoding.EncodeToString(s256.Sum(nil)[12:])
|
||||
fname := filepath.Join(a.store, id)
|
||||
|
||||
span.AddEvent("image: moving file", trace.WithAttributes(attribute.String("src", tmp.Name()), attribute.String("dst", fname)))
|
||||
span.AddEvent("image: moving file", trace.WithAttributes(attribute.String("image-src", tmp.Name()), attribute.String("image-dst", fname)))
|
||||
_ = os.Rename(tmp.Name(), fname)
|
||||
|
||||
return id, nil
|
||||
@@ -255,6 +300,24 @@ func chkGone(path string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
func writeError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, ErrGone):
|
||||
w.WriteHeader(http.StatusGone)
|
||||
case errors.Is(err, ErrNotFound):
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
case errors.Is(err, ErrReadingContent):
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
case errors.Is(err, ErrUnsupportedType):
|
||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||
}
|
||||
}
|
||||
|
||||
type Header struct {
|
||||
Mime string
|
||||
Modified time.Time
|
||||
ETag string
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
|
||||
115
image/image_test.go
Normal file
115
image/image_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package image_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
"go.sour.is/paste/v2/image"
|
||||
)
|
||||
|
||||
var testImage = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="
|
||||
|
||||
|
||||
func TestPostImgur(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
dir, err := os.MkdirTemp("", "image")
|
||||
is.NoErr(err)
|
||||
|
||||
im, err := image.New(dir, 0)
|
||||
is.NoErr(err)
|
||||
|
||||
{
|
||||
body := &bytes.Buffer{}
|
||||
wr := multipart.NewWriter(body)
|
||||
w, err := wr.CreateFormField("type")
|
||||
is.NoErr(err)
|
||||
|
||||
fmt.Fprint(w, "base64")
|
||||
|
||||
w, err = wr.CreateFormField("image")
|
||||
is.NoErr(err)
|
||||
|
||||
fmt.Fprint(w, testImage)
|
||||
|
||||
err = wr.Close()
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(body.String())
|
||||
|
||||
req := httptest.NewRequest("POST", "/3/upload", body)
|
||||
req.Header.Set("Content-Type", "multipart/form-data; boundary="+wr.Boundary())
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
im.ServeHTTP(res, req)
|
||||
|
||||
is.Equal(res.Code, http.StatusOK)
|
||||
is.Equal(res.Body.String(), `{"data":{"link":"https://example.com/i/Igg59VTi6JNuXVriXMR3U_lzfAc","deletehash":""},"success":true,"status":200}
|
||||
`)
|
||||
}
|
||||
|
||||
{
|
||||
req := httptest.NewRequest("GET", "/Igg59VTi6JNuXVriXMR3U_lzfAc", nil)
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
im.ServeHTTP(res, req)
|
||||
|
||||
is.Equal(res.Code, http.StatusOK)
|
||||
|
||||
s := base64.StdEncoding.EncodeToString(res.Body.Bytes())
|
||||
is.Equal(s, testImage)
|
||||
t.Log(res.Header())
|
||||
is.Equal(res.Header().Get("ETag"), "Igg59VTi6JNuXVriXMR3U_lzfAc")
|
||||
|
||||
}
|
||||
|
||||
err = os.RemoveAll(dir)
|
||||
is.NoErr(err)
|
||||
}
|
||||
|
||||
func TestPost(t *testing.T) {
|
||||
is := is.New(t)
|
||||
|
||||
|
||||
dir, err := os.MkdirTemp("", "image")
|
||||
is.NoErr(err)
|
||||
|
||||
im, err := image.New(dir, 0)
|
||||
is.NoErr(err)
|
||||
|
||||
{
|
||||
req := httptest.NewRequest("POST", "/i", base64.NewDecoder(base64.StdEncoding, strings.NewReader(testImage)))
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
im.ServeHTTP(res, req)
|
||||
|
||||
is.Equal(res.Code, http.StatusCreated)
|
||||
is.Equal(res.Body.String(), `{"data":{"link":"https://example.com/i/Igg59VTi6JNuXVriXMR3U_lzfAc","deletehash":""},"success":true,"status":200}
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
req := httptest.NewRequest("GET", "/Igg59VTi6JNuXVriXMR3U_lzfAc", nil)
|
||||
res := httptest.NewRecorder()
|
||||
|
||||
im.ServeHTTP(res, req)
|
||||
|
||||
is.Equal(res.Code, http.StatusOK)
|
||||
|
||||
s := base64.StdEncoding.EncodeToString(res.Body.Bytes())
|
||||
is.Equal(s, testImage)
|
||||
|
||||
}
|
||||
|
||||
err = os.RemoveAll(dir)
|
||||
is.NoErr(err)
|
||||
}
|
||||
Reference in New Issue
Block a user