add-imgur-api #1

Merged
xuu merged 3 commits from add-imgur-api into master 2023-11-09 13:50:23 -07:00
3 changed files with 18 additions and 7 deletions
Showing only changes of commit 9a26239aa7 - Show all commits

View File

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

View File

@ -3,8 +3,9 @@ export HTTP_LISTEN=:8085
export PASTE_STORE=data/paste/
export ARTIFACT_STORE=data/artifact/
export IMAGE_STORE=data/image/
export CGO_ENABLED=0
SOURCE=$(wildcard v2/*.go) $(wildcard v2/paste/*.go) $(wildcard assets/*.go)
SOURCE=$(wildcard *.go) $(wildcard 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
@ -30,7 +31,7 @@ ${ASSET_FILE}: $(ASSETS)
build: $(BINARY)
$(BINARY): $(SOURCE) $(ASSET_FILE)
go build -o $(BINARY) ./v2
go build -o $(BINARY) .
.PHONEY: all clean build run setup
# DO NOT DELETE

View File

@ -49,7 +49,7 @@ func New(store string, maxSize int64) (a *image, err error) {
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", http.StripPrefix("/3/upload", a))
mux.Handle("/3/upload", a)
}
func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -63,13 +63,23 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
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 err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
}
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, r.Body, length)
id, err := a.put(ctx, w, fd, length)
switch {
case errors.Is(err, ErrGone):
w.WriteHeader(http.StatusGone)
@ -91,7 +101,7 @@ func (a *image) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Status int `json:"status"`
}{
Data: data{
Link: fmt.Sprintf("https://%s/%s", r.Host, id),
Link: fmt.Sprintf("https://%s/i/%s", r.Host, id),
},
Success: true,
Status: 200,