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 566c122b5e
commit a38931bde1
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,
Auth: auth,
FileUploader: fileUploader,
FileCdn: raw.FileCdn,
}
return raw, cfg, nil
}
@ -151,7 +152,6 @@ func main() {
srv := soju.NewServer(db)
srv.SetConfig(serverCfg)
srv.Logger = soju.NewLogger(log.Writer(), debug)
fileUploadHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg := srv.Config()
h := fileupload.Handler{
@ -159,6 +159,7 @@ func main() {
DB: db,
Auth: cfg.Auth,
HTTPOrigins: cfg.HTTPOrigins,
Cdn: cfg.FileCdn,
}
h.ServeHTTP(w, r)
})

View file

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

View file

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

View file

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