Esempio n. 1
0
func (ib *ItemBrowser) DownloadHandler(w http.ResponseWriter, req *http.Request) {
	prms := params.NewParams(mux.Vars(req))
	id := prms.Get("id").Int()
	bi, ok := ib.itemsByID[id]
	if !ok {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	if bi.Type != TypeFile || len(bi.Files) == 0 {
		http.Error(w, "not a file", http.StatusNotFound)
		return
	}
	if len(bi.Files[0].Filename) > 0 {
		w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", bi.Files[0].Filename))
	}
	f, err := os.Open(path.Join(ib.dataDir, UrlToFilename(bi.Files[0].Url)))
	if err != nil {
		log.Printf("content for metadata not found: %s", err)
		http.Error(w, "content not found", http.StatusInternalServerError)
		return
	}
	if d, err := io.Copy(w, f); err != nil {
		log.Printf("Could not copy file to response, copied: %d bytes", d)
		http.Error(w, "could not send?", http.StatusInternalServerError)
		return
	}
}
Esempio n. 2
0
func (ib *ItemBrowser) NodeHandler(w http.ResponseWriter, req *http.Request) {
	prms := params.NewParams(mux.Vars(req))
	id := prms.Get("id").Int()
	bi, ok := ib.itemsByID[id]
	if !ok {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	if err := detailTmpl.Execute(w, bi); err != nil {
		log.Printf("%+v", bi)
		log.Print(err)
	}
}