Пример #1
0
// SlotHandler handles requests to GET /item/:id/*slot
//                and requests to HEAD /item/:id/*slot
func (s *RESTServer) SlotHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	id := ps.ByName("id")
	// the star parameter in httprouter returns the leading slash
	slot := ps.ByName("slot")[1:]

	item, err := s.Items.Item(id)

	if err != nil {
		// if item store use disabled, return 503
		if err == items.ErrNoStore {
			w.WriteHeader(503)
			log.Printf("GET/HEAD /item/%s/%s returns 503 - tape disabled", id, slot)
		} else {
			w.WriteHeader(404)
		}
		fmt.Fprintln(w, err.Error())
		return
	}
	// if we have the empty path, reroute to the item metadata handler
	if slot == "" {
		s.ItemHandler(w, r, ps)
		return
	}
	// slot might have a "@nnn" version prefix
	bid := item.BlobByExtendedSlot(slot)
	if bid == 0 {
		w.WriteHeader(404)
		fmt.Fprintf(w, "Invalid Version")
		return
	}
	w.Header().Set("X-Content-Sha256", hex.EncodeToString(item.Blobs[bid-1].SHA256))
	w.Header().Set("Location", fmt.Sprintf("/item/%s/@blob/%d", id, bid))
	s.getblob(w, r, id, items.BlobID(bid))
}
Пример #2
0
// BlobHandler handles requests to GET /blob/:id/:bid
func (s *RESTServer) BlobHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	id := ps.ByName("id")
	bid, err := strconv.ParseInt(ps.ByName("bid"), 10, 0)
	if err != nil || bid <= 0 {
		w.WriteHeader(404)
		fmt.Fprintln(w, err)
		return
	}
	s.getblob(w, r, id, items.BlobID(bid))
}
Пример #3
0
func doblob(r *items.Store, id, blob string) {
	bid, _ := strconv.Atoi(blob)

	rc, _, err := r.Blob(id, items.BlobID(bid))
	if err != nil {
		fmt.Printf("%s / %d: Error %s\n", id, bid, err.Error())
	} else {
		io.Copy(os.Stdout, rc)
		rc.Close()
	}
}
Пример #4
0
// add all the files to this item. Directories are automatically recursed into.
// Files and directories which begin with a dot are skipped.
func dodelete(r *items.Store, id string, delblobs []string) {
	var delbid []items.BlobID
	for _, blobid := range delblobs {
		bid, err := strconv.Atoi(blobid)
		if err != nil {
			fmt.Println(err.Error())
			return
		}
		delbid = append(delbid, items.BlobID(bid))
	}
	tx, err := r.Open(id, *creator)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	for _, bid := range delbid {
		tx.DeleteBlob(bid)
		// TODO(dbrower): also remove any slots pointing to this blob
	}
	err = tx.Close()
	if err != nil {
		fmt.Println(err.Error())
	}
}