s3-share/internal/web/web.go

209 lines
5.0 KiB
Go
Raw Permalink Normal View History

2022-05-09 12:52:18 +00:00
package web
import (
"encoding/json"
2022-05-12 21:37:59 +00:00
"errors"
2022-05-09 12:52:18 +00:00
"io"
"net/http"
2022-05-10 10:49:34 +00:00
"strconv"
2022-05-09 12:52:18 +00:00
"git.kapelle.org/niklas/s3share/internal/client"
2022-05-10 20:37:30 +00:00
"git.kapelle.org/niklas/s3share/internal/types"
2022-05-09 12:52:18 +00:00
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
type createShare struct {
Key string `json:"key"`
}
2022-06-01 13:02:52 +00:00
func StartWebserver(addr string, client *client.Client, username, password string) error {
2022-05-12 21:37:59 +00:00
if username == "" || password == "" {
return errors.New("API username and password must be set")
}
2022-06-01 13:02:52 +00:00
r := CreateRouter(client, username, password)
return http.ListenAndServe(addr, r)
}
func CreateRouter(client *client.Client, username, password string) *mux.Router {
2022-05-09 12:52:18 +00:00
r := mux.NewRouter()
r.HandleFunc("/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
2022-05-10 14:03:24 +00:00
http.ServeFile(w, r, "./public/index.html")
2022-05-09 12:52:18 +00:00
})
2022-05-14 21:06:16 +00:00
r.HandleFunc("/s/{path:.{6,}}", func(w http.ResponseWriter, r *http.Request) {
2022-05-10 20:37:30 +00:00
share := getShareHead(client, w, r)
2022-05-09 12:52:18 +00:00
if share == nil {
2022-05-10 10:49:34 +00:00
return
}
2022-05-09 12:52:18 +00:00
obj, err := client.GetObjectFromShare(r.Context(), share)
if err != nil {
2022-05-10 10:49:34 +00:00
logrus.Error(err.Error())
2022-05-09 12:52:18 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = io.Copy(w, obj)
if err != nil {
2022-05-14 21:06:16 +00:00
logrus.Error(err.Error())
2022-05-09 12:52:18 +00:00
return
}
})
2022-05-14 21:06:16 +00:00
r.HandleFunc("/s/{path:.{6,}}", func(w http.ResponseWriter, r *http.Request) {
2022-05-10 20:37:30 +00:00
getShareHead(client, w, r)
}).Methods("HEAD")
2022-05-09 12:52:18 +00:00
r.HandleFunc("/api/share", func(w http.ResponseWriter, r *http.Request) {
2022-05-12 21:37:59 +00:00
if !checkAuth(w, r, username, password) {
return
}
2022-05-09 12:52:18 +00:00
2022-05-14 21:25:50 +00:00
shares, err := client.GetAllShares(r.Context())
if err != nil {
logrus.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(shares)
2022-05-09 12:52:18 +00:00
}).Methods("GET")
2022-05-16 23:14:45 +00:00
r.HandleFunc("/api/share/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
if !checkAuth(w, r, username, password) {
return
}
vars := mux.Vars(r)
slug := vars["slug"]
share, err := client.GetShare(r.Context(), slug)
if err != nil {
logrus.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if share == nil {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(share)
}).Methods("GET")
2022-05-09 12:52:18 +00:00
r.HandleFunc("/api/share", func(w http.ResponseWriter, r *http.Request) {
2022-05-12 21:37:59 +00:00
if !checkAuth(w, r, username, password) {
return
}
2022-05-09 12:52:18 +00:00
var shareParams createShare
err := json.NewDecoder(r.Body).Decode(&shareParams)
if err != nil {
2022-05-10 10:49:34 +00:00
logrus.Error(err.Error())
2022-05-09 12:52:18 +00:00
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
share, err := client.CreateShare(r.Context(), shareParams.Key)
if err != nil {
2022-06-03 12:27:40 +00:00
if err == types.ErrKeyNotFound {
http.Error(w, "The specified key does not exist", http.StatusBadRequest)
return
}
2022-05-10 10:49:34 +00:00
logrus.Error(err.Error())
2022-05-09 12:52:18 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
2022-06-01 13:02:52 +00:00
w.WriteHeader(http.StatusCreated)
2022-05-09 12:52:18 +00:00
json.NewEncoder(w).Encode(share)
2022-06-01 13:02:52 +00:00
}).Methods("POST", "PUT")
2022-05-09 12:52:18 +00:00
2022-05-16 23:10:36 +00:00
r.HandleFunc("/api/share/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
2022-05-12 21:37:59 +00:00
if !checkAuth(w, r, username, password) {
return
}
2022-05-09 12:52:18 +00:00
2022-05-16 23:10:36 +00:00
vars := mux.Vars(r)
2022-05-09 13:36:43 +00:00
2022-05-16 23:10:36 +00:00
err := client.DeleteShare(r.Context(), vars["slug"])
2022-05-09 13:36:43 +00:00
if err != nil {
2022-06-03 12:27:40 +00:00
if err == types.ErrShareNotFound {
http.NotFound(w, r)
return
}
2022-05-10 10:49:34 +00:00
logrus.Error(err.Error())
2022-05-09 13:36:43 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2022-06-01 13:02:52 +00:00
w.WriteHeader(http.StatusNoContent)
2022-05-09 12:52:18 +00:00
}).Methods("DELETE")
2022-05-10 14:03:24 +00:00
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
2022-06-01 13:02:52 +00:00
return r
2022-05-09 12:52:18 +00:00
}
2022-05-10 20:37:30 +00:00
2022-06-01 13:02:52 +00:00
func getShareHead(client *client.Client, w http.ResponseWriter, r *http.Request) *types.Share {
2022-05-10 20:37:30 +00:00
vars := mux.Vars(r)
2022-05-14 21:06:16 +00:00
slug := vars["path"][0:6]
share, err := client.GetShare(r.Context(), slug)
2022-05-10 20:37:30 +00:00
if err != nil {
logrus.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
if share == nil {
http.NotFound(w, r)
return nil
}
metadata, err := client.GetObjectMetadata(r.Context(), share.Key)
if err != nil {
logrus.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
2022-05-14 21:06:16 +00:00
// Prevent using the default Content-Type. Can cause some confusion when the browser tries to open images in a new tab.
if metadata.ContentType != "application/octet-stream" {
w.Header().Set("Content-Type", metadata.ContentType)
}
2022-05-10 20:37:30 +00:00
w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10))
2022-05-14 21:06:16 +00:00
w.Header().Set("Content-Disposition", "inline; filename=\""+metadata.Filename+"\"")
2022-05-10 20:46:28 +00:00
if metadata.ETag != "" {
w.Header().Set("ETag", metadata.ETag)
}
2022-05-10 20:37:30 +00:00
return share
}
2022-05-12 21:37:59 +00:00
func checkAuth(w http.ResponseWriter, r *http.Request, username, password string) bool {
2022-06-03 17:56:07 +00:00
authUsername, authPassword, ok := r.BasicAuth()
2022-05-12 21:37:59 +00:00
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return false
}
2022-06-03 17:56:07 +00:00
if username != authUsername || password != authPassword {
2022-05-12 21:37:59 +00:00
w.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}