Compare commits

..

No commits in common. "1b8e8ad26a30142b5a84ddf5e606202ce5886ab7" and "2cbd98190220e7b290682130dba8234c24994ace" have entirely different histories.

22 changed files with 60 additions and 328 deletions

View File

@ -1,8 +1,8 @@
name: Deploy
on:
# push:
# branches: [ "master" ]
push:
branches: [ "master" ]
release:
types: [ published ]

View File

@ -4,7 +4,7 @@ export PASTE_STORE=data/paste/
export ARTIFACT_STORE=data/artifact/
export IMAGE_STORE=data/image/
SOURCE=$(wildcard *.go) $(wildcard paste/*.go) $(wildcard assets/*.go)
SOURCE=$(wildcard v2/*.go) $(wildcard v2/paste/*.go) $(wildcard assets/*.go)
ASSETS=$(wildcard assets/*) $(wildcard assets/public/*) $(wildcard assets/src/*) $(wildcard assets/src/paste/*)
ASSET_FILE=assets/build/index.html
BINARY=sour.is-paste
@ -20,7 +20,8 @@ test:
go test ./...
go vet ./...
run: $(BINARY)
go run .
# go run ./v2
./$(BINARY)
build-assets: $(ASSET_FILE)
${ASSET_FILE}: $(ASSETS)
@ -29,7 +30,7 @@ ${ASSET_FILE}: $(ASSETS)
build: $(BINARY)
$(BINARY): $(SOURCE) $(ASSET_FILE)
go build -o $(BINARY) .
go build -o $(BINARY) ./v2
.PHONEY: all clean build run setup
# DO NOT DELETE

View File

@ -1,39 +0,0 @@
package main
import (
"context"
"embed"
"io/fs"
"net/http"
"go.sour.is/pkg/lg"
"go.sour.is/pkg/service"
)
var _ = apps.Register(10, func(ctx context.Context, svc *service.Harness) error {
_, span := lg.Span(ctx)
defer span.End()
svc.Add(&favicon{})
return nil
})
//go:embed favicon/*
var faviconAsset embed.FS
type favicon struct{}
func (favicon) RegisterHTTP(mux *http.ServeMux) {
dir, _ := fs.Sub(faviconAsset, "favicon")
srv := http.FileServer(http.FS(dir))
mux.Handle("/favicon.ico", srv)
mux.Handle("/favicon.txt", srv)
mux.Handle("/favicon-16x16.png", srv)
mux.Handle("/favicon-32x32.png", srv)
mux.Handle("/android-chrome-192x192.png", srv)
mux.Handle("/android-chrome-512x512.png", srv)
mux.Handle("/apple-touch-icon.png", srv)
mux.Handle("/site.webmanifest", srv)
}

View File

@ -15,7 +15,7 @@ var _ = apps.Register(40, func(ctx context.Context, svc *service.Harness) error
store := env.Default("IMAGE_STORE", "data/")
a, err := image.New(ctx, store, -1)
a, err := image.New(store, -1)
if err != nil {
return err
}

View File

@ -1,13 +1,13 @@
{
"files": {
"main.css": "/ui/static/css/main.d4025777.css",
"main.js": "/ui/static/js/main.939bead5.js",
"main.js": "/ui/static/js/main.1d06557a.js",
"index.html": "/ui/index.html",
"main.d4025777.css.map": "/ui/static/css/main.d4025777.css.map",
"main.939bead5.js.map": "/ui/static/js/main.939bead5.js.map"
"main.1d06557a.js.map": "/ui/static/js/main.1d06557a.js.map"
},
"entrypoints": [
"static/css/main.d4025777.css",
"static/js/main.939bead5.js"
"static/js/main.1d06557a.js"
]
}

View File

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="manifest" href="/ui/manifest.json"><link rel="shortcut icon" href="/ui/favicon.ico"><title>DN42 Paste</title><script defer="defer" src="/ui/static/js/main.939bead5.js"></script><link href="/ui/static/css/main.d4025777.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="manifest" href="/ui/manifest.json"><link rel="shortcut icon" href="/ui/favicon.ico"><title>DN42 Paste</title><script defer="defer" src="/ui/static/js/main.1d06557a.js"></script><link href="/ui/static/css/main.d4025777.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

File diff suppressed because one or more lines are too long

View File

@ -200,17 +200,17 @@ class Paste extends Component {
onSubmit(event) {
event.preventDefault();
const { plain } = this.state;
// const { history } = this.props;
const { history } = this.props;
this.encrypt(plain).then(([hash, decryptKey]) => { history.pushState({}, '', '/ui/#/' + hash + '!' + decryptKey) });
this.encrypt(plain).then(([hash, decryptKey]) => { history.push('/#/' + hash + '!' + decryptKey) });
}
onNew(event) {
// const { history } = this.props;
this.setState({cipher:"", plain:"", hash: "", decryptKey: "", syntax: "text", expire: 604800, burn: false}, () => history.pushState({}, '', '/ui'));
const { history } = this.props;
this.setState({cipher:"", plain:"", hash: "", decryptKey: "", syntax: "text", expire: 604800, burn: false}, () => history.push('/'));
}
onCopy(event) {
// const { history } = this.props;
this.setState({hash: "", decryptKey: ""}, () => history.pushState({}, '', '/ui'))
const { history } = this.props;
this.setState({hash: "", decryptKey: ""}, () => history.push('/'))
}
decrypt(tx) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 472 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 847 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

View File

@ -1,6 +0,0 @@
This favicon was generated using the following graphics from Twitter Twemoji:
- Graphics Title: 1f4cb.svg
- Graphics Author: Copyright 2020 Twitter, Inc and other contributors (https://github.com/twitter/twemoji)
- Graphics Source: https://github.com/twitter/twemoji/blob/master/assets/svg/1f4cb.svg
- Graphics License: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

View File

@ -1 +0,0 @@
{"name":"sour.is paste","short_name":"paste","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"}

3
go.mod
View File

@ -5,7 +5,6 @@ go 1.21
require (
github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167
github.com/h2non/filetype v1.1.0
github.com/matryer/is v1.4.1
github.com/rs/cors v1.6.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/trace v1.18.0
@ -48,7 +47,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.41.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/sdk v1.18.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.41.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect

View File

@ -13,11 +13,9 @@ import (
"path/filepath"
"strconv"
"strings"
"time"
"github.com/h2non/filetype"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sys/unix"
@ -28,15 +26,11 @@ import (
type image struct {
store string
maxSize int64
m_image_get metric.Int64Counter
m_image_post metric.Int64Counter
m_image_error metric.Int64Counter
}
const DefaultMaxSize = 500 * 1024 * 1024
func New(ctx context.Context, store string, maxSize int64) (a *image, err error) {
func New(store string, maxSize int64) (a *image, err error) {
a = &image{
store: store,
maxSize: DefaultMaxSize,
@ -50,31 +44,13 @@ func New(ctx context.Context, store string, maxSize int64) (a *image, err error)
return nil, fmt.Errorf("image Store location [%s] does not exist or is not writable", a.store)
}
m := lg.Meter(ctx)
var merr error
a.m_image_get, merr = m.Int64Counter("m_image_get",
metric.WithDescription("retrieve image from store"),
)
err = errors.Join(err, merr)
a.m_image_post, merr = m.Int64Counter("m_image_post",
metric.WithDescription("save image to store"),
)
err = errors.Join(err, merr)
a.m_image_error, merr = m.Int64Counter("m_image_error",
metric.WithDescription("image api error"),
)
err = errors.Join(err, merr)
return a, err
return a, nil
}
func (a *image) RegisterHTTP(mux *http.ServeMux) {
mux.Handle("/i", http.StripPrefix("/i", a))
mux.Handle("/i/", http.StripPrefix("/i/", a))
mux.Handle("/3/upload", a)
mux.Handle("3/upload", http.StripPrefix("/3/upload", a))
}
func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
@ -82,104 +58,53 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer span.End()
switch r.Method {
case http.MethodGet, http.MethodHead:
a.m_image_get.Add(ctx, 1)
case http.MethodGet:
name := strings.TrimPrefix(r.URL.Path, "/")
if name != "" {
rdr, head, err := a.loadFile(ctx, name)
if err != nil {
a.m_image_error.Add(ctx, 1)
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
}
a.get(ctx, w, name)
case http.MethodPost:
a.m_image_post.Add(ctx, 1)
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, fd, length)
if err != nil {
a.m_image_error.Add(ctx, 1)
writeError(w, err)
span.RecordError(err)
return
id, err := a.put(ctx, w, r.Body, 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)
}
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),
Link: fmt.Sprintf("https://%s/%s", r.Host, id),
},
Success: true,
Status: 200,
Status: 200,
}
err = json.NewEncoder(w).Encode(resp)
span.RecordError(err)
json.NewEncoder(w).Encode(resp)
default:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
}
func (a *image) loadFile(ctx context.Context, name string) (io.ReadSeekCloser, *Header, error) {
func (a *image) get(ctx context.Context, w http.ResponseWriter, name string) error {
_, span := lg.Span(ctx)
defer span.End()
@ -189,44 +114,38 @@ func (a *image) loadFile(ctx context.Context, name string) (io.ReadSeekCloser, *
fname := filepath.Join(a.store, id)
if !chkFile(fname) {
return nil, nil, fmt.Errorf("%w: %s", ErrNotFound, fname)
return fmt.Errorf("%w: %s", ErrNotFound, fname)
}
if chkGone(fname) {
return nil, nil, fmt.Errorf("%w: %s", ErrGone, fname)
return fmt.Errorf("%w: %s", ErrGone, fname)
}
f, err := os.Open(fname)
if err != nil {
return nil, nil, err
}
stat, err := f.Stat()
if err != nil {
return nil, nil, err
return err
}
defer f.Close()
pr := readutil.NewPreviewReader(f)
mime, err := readutil.ReadMIME(pr, name)
if err != nil {
return nil, nil, err
return err
}
w.Header().Set("Content-Type", mime)
w.Header().Set("X-Content-Type-Options", "nosniff")
f.Seek(0, 0)
return f,
&Header{Mime: mime, Modified: stat.ModTime(), ETag: name},
nil
_, _ = io.Copy(w, pr.Drain())
return nil
}
func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, error) {
func (a *image) put(ctx context.Context, w http.ResponseWriter, 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
@ -236,7 +155,6 @@ func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, e
rdr := io.LimitReader(r, a.maxSize)
pr := readutil.NewPreviewReader(rdr)
if !isImageOrVideo(pr) {
span.AddEvent("not image")
return "", ErrUnsupportedType
}
rdr = pr.Drain()
@ -244,7 +162,6 @@ func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, e
s256 := sha256.New()
tmp, err := os.CreateTemp(a.store, "image-")
if err != nil {
span.RecordError(err)
return "", fmt.Errorf("%w: %w", ErrBadInput, err)
}
@ -252,7 +169,6 @@ func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, e
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()
@ -260,7 +176,7 @@ func (a *image) put(ctx context.Context, r io.ReadCloser, length int) (string, e
id := base64.RawURLEncoding.EncodeToString(s256.Sum(nil)[12:])
fname := filepath.Join(a.store, id)
span.AddEvent("image: moving file", trace.WithAttributes(attribute.String("image-src", tmp.Name()), attribute.String("image-dst", fname)))
span.AddEvent("image: moving file", trace.WithAttributes(attribute.String("src", tmp.Name()), attribute.String("dst", fname)))
_ = os.Rename(tmp.Name(), fname)
return id, nil
@ -329,24 +245,6 @@ 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")

View File

@ -1,118 +0,0 @@
package image_test
import (
"bytes"
"context"
"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)
ctx := context.Background()
dir, err := os.MkdirTemp("", "image")
is.NoErr(err)
im, err := image.New(ctx, 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)
ctx := context.Background()
dir, err := os.MkdirTemp("", "image")
is.NoErr(err)
im, err := image.New(ctx, 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)
}

View File

@ -20,13 +20,11 @@ var _ = apps.Register(20, func(ctx context.Context, svc *service.Harness) error
svc.Add(s)
mux := mux.New()
s.Handler = cors.AllowAll().Handler(mux)
hdlr := s.Handler
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.URL.Path)
hdlr.ServeHTTP(w, r)
mux.ServeHTTP(w, r)
})
s.Addr = env.Default("HTTP_LISTEN", ":8080")