s3-share/internal/web/web.go

169 lines
4.0 KiB
Go
Raw 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-05-09 13:36:43 +00:00
type deleteShare struct {
Slug string `json:"slug"`
}
2022-05-12 21:37:59 +00:00
func StartWebserver(addr string, client client.Client, username, password string) error {
if username == "" || password == "" {
return errors.New("API username and password must be set")
}
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
}).Methods("GET")
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-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")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(share)
}).Methods("POST")
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-09 13:36:43 +00:00
var shareParams deleteShare
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 13:36:43 +00:00
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = client.DeleteShare(r.Context(), shareParams.Slug)
if err != nil {
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-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-05-09 12:52:18 +00:00
logrus.Info("Starting webserver")
return http.ListenAndServe(addr, r)
}
2022-05-10 20:37:30 +00:00
func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request) *types.Share {
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 {
username, password, ok := r.BasicAuth()
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return false
}
if username != username || password != password {
w.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}