feat: file_save_disk, file_delete, file_serve, upload_parse, upload_handler, thumbnail_generate (issue 0014 fase 3)
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FileServe retorna un http.Handler que sirve archivos estaticos desde dir.
|
||||
// Stripea pathPrefix del request URL antes de buscar el archivo, y setea el
|
||||
// header Cache-Control con max-age=maxAge segundos.
|
||||
//
|
||||
// El handler rechaza cualquier path que contenga ".." para mitigar path traversal,
|
||||
// aunque http.FileServer ya hace su propia normalizacion.
|
||||
func FileServe(dir string, pathPrefix string, maxAge int) http.Handler {
|
||||
fs := http.FileServer(http.Dir(dir))
|
||||
cacheControl := fmt.Sprintf("public, max-age=%d", maxAge)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Rechazar path traversal explicito
|
||||
if strings.Contains(r.URL.Path, "..") {
|
||||
http.Error(w, "path traversal not allowed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", cacheControl)
|
||||
http.StripPrefix(pathPrefix, fs).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user