Break out file host URL to allow custom CDNs

This commit is contained in:
Gabriel Simmer 2024-06-30 15:56:49 +01:00
parent b9a0b0e454
commit e145fa579a
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
4 changed files with 18 additions and 2 deletions

View file

@ -113,6 +113,7 @@ func loadConfig() (*config.Server, *soju.Config, error) {
MOTD: motd, MOTD: motd,
Auth: auth, Auth: auth,
FileUploader: fileUploader, FileUploader: fileUploader,
FileCdn: raw.FileCdn,
} }
return raw, cfg, nil return raw, cfg, nil
} }
@ -151,7 +152,6 @@ func main() {
srv := soju.NewServer(db) srv := soju.NewServer(db)
srv.SetConfig(serverCfg) srv.SetConfig(serverCfg)
srv.Logger = soju.NewLogger(log.Writer(), debug) srv.Logger = soju.NewLogger(log.Writer(), debug)
fileUploadHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fileUploadHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg := srv.Config() cfg := srv.Config()
h := fileupload.Handler{ h := fileupload.Handler{
@ -159,6 +159,7 @@ func main() {
DB: db, DB: db,
Auth: cfg.Auth, Auth: cfg.Auth,
HTTPOrigins: cfg.HTTPOrigins, HTTPOrigins: cfg.HTTPOrigins,
Cdn: cfg.FileCdn,
} }
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
}) })

View file

@ -83,6 +83,7 @@ type Server struct {
MsgStore MsgStore MsgStore MsgStore
Auth Auth Auth Auth
FileUpload *FileUpload FileUpload *FileUpload
FileCdn string
HTTPOrigins []string HTTPOrigins []string
HTTPIngress string HTTPIngress string
@ -130,6 +131,7 @@ func Load(filename string) (*Server, error) {
Log []string `scfg:"log"` Log []string `scfg:"log"`
Auth []string `scfg:"auth"` Auth []string `scfg:"auth"`
FileUpload []string `scfg:"file-upload"` FileUpload []string `scfg:"file-upload"`
FileCdn string `scfg:"file-cdn"`
HTTPOrigin []string `scfg:"http-origin"` HTTPOrigin []string `scfg:"http-origin"`
HTTPIngress string `scfg:"http-ingress"` HTTPIngress string `scfg:"http-ingress"`
AcceptProxyIP []string `scfg:"accept-proxy-ip"` AcceptProxyIP []string `scfg:"accept-proxy-ip"`
@ -224,6 +226,7 @@ func Load(filename string) (*Server, error) {
} }
srv.FileUpload = &FileUpload{driver, source} srv.FileUpload = &FileUpload{driver, source}
} }
srv.FileCdn = raw.FileCdn
for _, origin := range raw.HTTPOrigin { for _, origin := range raw.HTTPOrigin {
if _, err := path.Match(origin, origin); err != nil { if _, err := path.Match(origin, origin); err != nil {
return nil, fmt.Errorf("directive http-origin: %v", err) return nil, fmt.Errorf("directive http-origin: %v", err)

View file

@ -77,6 +77,7 @@ type Handler struct {
Auth auth.Authenticator Auth auth.Authenticator
DB database.Database DB database.Database
HTTPOrigins []string HTTPOrigins []string
Cdn string
} }
func (h *Handler) checkOrigin(reqOrigin string) bool { func (h *Handler) checkOrigin(reqOrigin string) bool {
@ -153,6 +154,12 @@ func (h *Handler) fetch(resp http.ResponseWriter, req *http.Request) {
return return
} }
if h.Cdn != "" {
resp.Header().Set("Location", h.Cdn + "/" + filename)
resp.WriteHeader(http.StatusCreated)
return
}
basename, modTime, content, err := h.Uploader.load(filename) basename, modTime, content, err := h.Uploader.load(filename)
if err != nil { if err != nil {
http.Error(resp, "failed to open file", http.StatusNotFound) http.Error(resp, "failed to open file", http.StatusNotFound)
@ -285,7 +292,11 @@ func (h *Handler) store(resp http.ResponseWriter, req *http.Request) {
return return
} }
resp.Header().Set("Location", "/uploads/"+outFilename) if h.Cdn != "" {
resp.Header().Set("Location", h.Cdn + "/" + outFilename)
} else {
resp.Header().Set("Location", "/uploads/"+outFilename)
}
resp.WriteHeader(http.StatusCreated) resp.WriteHeader(http.StatusCreated)
} }

View file

@ -152,6 +152,7 @@ type Config struct {
EnableUsersOnAuth bool EnableUsersOnAuth bool
Auth auth.Authenticator Auth auth.Authenticator
FileUploader fileupload.Uploader FileUploader fileupload.Uploader
FileCdn string
} }
type Server struct { type Server struct {