(store *storage.Storage, server *settings.Server, assetsFs fs.FS)
| 104 | } |
| 105 | |
| 106 | func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs fs.FS) (index, static http.Handler) { |
| 107 | index = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { |
| 108 | if r.Method != http.MethodGet { |
| 109 | return http.StatusNotFound, nil |
| 110 | } |
| 111 | |
| 112 | w.Header().Set("x-xss-protection", "1; mode=block") |
| 113 | return handleWithStaticData(w, r, d, assetsFs, "public/index.html", "text/html; charset=utf-8") |
| 114 | }, "", store, server) |
| 115 | |
| 116 | static = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { |
| 117 | if r.Method != http.MethodGet { |
| 118 | return http.StatusNotFound, nil |
| 119 | } |
| 120 | |
| 121 | if strings.HasSuffix(r.URL.Path, "/") { |
| 122 | return http.StatusNotFound, nil |
| 123 | } |
| 124 | |
| 125 | const maxAge = 86400 // 1 day |
| 126 | w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%v", maxAge)) |
| 127 | |
| 128 | if d.settings.Branding.Files != "" { |
| 129 | if strings.HasPrefix(r.URL.Path, "img/") { |
| 130 | fPath := filepath.Join(d.settings.Branding.Files, r.URL.Path) |
| 131 | _, err := os.Stat(fPath) |
| 132 | if err != nil && !os.IsNotExist(err) { |
| 133 | log.Printf("could not load branding file override: %v", err) |
| 134 | } else if err == nil { |
| 135 | http.ServeFile(w, r, fPath) |
| 136 | return 0, nil |
| 137 | } |
| 138 | } else if r.URL.Path == "custom.css" && d.settings.Branding.Files != "" { |
| 139 | http.ServeFile(w, r, filepath.Join(d.settings.Branding.Files, "custom.css")) |
| 140 | return 0, nil |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if !strings.HasSuffix(r.URL.Path, ".js") { |
| 145 | http.FileServer(http.FS(assetsFs)).ServeHTTP(w, r) |
| 146 | return 0, nil |
| 147 | } |
| 148 | |
| 149 | f, err := assetsFs.Open(r.URL.Path + ".gz") |
| 150 | if err != nil { |
| 151 | return http.StatusNotFound, err |
| 152 | } |
| 153 | defer f.Close() |
| 154 | |
| 155 | acceptEncoding := r.Header.Get("Accept-Encoding") |
| 156 | if strings.Contains(acceptEncoding, "gzip") { |
| 157 | w.Header().Set("Content-Encoding", "gzip") |
| 158 | w.Header().Set("Content-Type", "application/javascript; charset=utf-8") |
| 159 | |
| 160 | if _, err := io.Copy(w, f); err != nil { |
| 161 | return http.StatusInternalServerError, err |
| 162 | } |
| 163 | } else { |
no test coverage detected