chore: add imgur api

This commit is contained in:
xuu
2023-11-07 15:28:09 -07:00
parent 2cbd981902
commit 9a26239aa7
3 changed files with 18 additions and 7 deletions

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,