Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 15, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/e4fb09ede89bedfae6e2e9a36794684a to your computer and use it in GitHub Desktop.
Save parzibyte/e4fb09ede89bedfae6e2e9a36794684a to your computer and use it in GitHub Desktop.
enrutador.HandleFunc("/foto_producto", func(w http.ResponseWriter, r *http.Request) {
idsProductos, existenIdsProductos := r.URL.Query()["id"]
if !existenIdsProductos || len(idsProductos) < 0 {
responderHttpConError(errors.New("no hay id en la URL"), w, r)
return
}
idProducto := idsProductos[0]
pc := ProductosController{
AjustesUsuario: AjustesDeUsuarioLogueado{
httpResponseWriter: w,
httpRequest: r,
},
}
idProductoEntero, err := strconv.Atoi(idProducto)
if err != nil {
responderHttpConError(err, w, r)
return
}
producto := pc.porRowid(idProductoEntero)
if producto.Foto != "" {
rutaImagen := filepath.Join(DirectorioArchivos, producto.Foto)
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(rutaImagen)))
http.ServeFile(w, r, rutaImagen)
return
} else {
w.WriteHeader(http.StatusNotFound)
}
}).Name("VerProductos").Methods(http.MethodGet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment