func sendFileBlobs(c *blobserv.Client, part *multipart.Part) (respMeta map[string]interface{}) { meta := blob.NewMeta() defer func() { respMeta = map[string]interface{}{} respMeta["name"] = meta.Name respMeta["size"] = meta.Size if r := recover(); r != nil { respMeta["error"] = r.(error).Error() } }() obj := blob.NewObject() meta.RcasObjectRef = obj.Ref() meta.Name = part.FileName() data, err := ioutil.ReadAll(part) util.Check(err) meta.Size = int64(len(data)) blobs := blob.SplitRaw(data, blob.DefaultChunkSize) meta.ContentRefs = blob.RefsFor(blobs) m, err := blob.Marshal(meta) util.Check(err) blobs = append(blobs, m, obj) for _, b := range blobs { err = c.PutBlob(b) util.Check(err) } return respMeta }
func Handler(nc *blobserv.Client, w http.ResponseWriter, r *http.Request) { defer util.DeferWrite(w) c = nc tmpl := template.Must(template.ParseFiles(appserv.Static("pics/index.tmpl"))) if picIndex == nil { loadPicIndex() } updateIndex() pth := strings.Trim(r.URL.Path, "/") if pth == "pics" { links := picLinks(picIndex.Newest(10)) err := tmpl.Execute(w, links) util.Check(err) } else if strings.HasPrefix(pth, "pics/ref/") { ref := path.Base(pth) ref = ref[:len(ref)-len(path.Ext(ref))] fblob, err := c.ObjectTip(ref) util.Check(err) m, data, err := c.ReconstituteFile(fblob.Ref()) util.Check(err) ext := path.Ext(m.Name) w.Header().Set("Content-Type", mime.TypeByExtension(ext)) w.Write(data) } else { err := util.LoadStatic(appserv.Static(pth), w) util.Check(err) } }
func picForObj(ref string) *photos.Photo { b, err := c.ObjectTip(ref) util.Check(err) p := &photos.Photo{} err = blob.Unmarshal(b, p) util.Check(err) return p }
func Handler(c *blobserv.Client, w http.ResponseWriter, r *http.Request) { defer util.DeferWrite(w) pth := strings.Trim(r.URL.Path, "/") if pth == "fupload" { err := util.LoadStatic(appserv.Static("fupload/index.html"), w) util.Check(err) } else if pth == "fupload/putfiles" { putfiles(c, w, r) } else { err := util.LoadStatic(appserv.Static(pth), w) util.Check(err) } }
// SetMeta sets the mount Notes associated with the given file. func (m *Mount) SetMeta(pth string, mm *Meta) (err error) { defer func() { recover() }() fm, err := m.getTip(pth) util.Check(err) err = fm.SetNotes(Key, mm) util.Check(err) b, err := blob.Marshal(fm) util.Check(err) err = m.Client.PutBlob(b) util.Check(err) return nil }
func Handler(c *blobserv.Client, w http.ResponseWriter, r *http.Request) { defer util.DeferWrite(w) tmpl := template.Must(template.ParseFiles(appserv.Static("recent/index.tmpl"))) pth := strings.Trim(r.URL.Path, "/") if pth == "recent" { data := stripBlobs(c) err := tmpl.Execute(w, data) util.Check(err) } else { err := util.LoadStatic(appserv.Static(pth), w) util.Check(err) } }
func loadPicIndex() { nBlobs := 10 for skip := 0; true; skip += nBlobs { blobs, err := c.BlobsBackward(time.Now(), nBlobs, skip) if err != nil { break } for _, b := range blobs { if b.Type() == photos.IndexType { err := blob.Unmarshal(b, picIndex) util.Check(err) return } } if len(blobs) < nBlobs { break } } // no pre-existing photo index found picIndex = photos.NewIndex() obj := blob.NewObject() picIndex.RcasObjectRef = obj.Ref() err := c.PutBlob(obj) if err != nil { panic("pics: could not create photo index") } }
func putnote(c *blobserv.Client, w http.ResponseWriter, req *http.Request) { defer util.DeferWrite(w) body, err := ioutil.ReadAll(req.Body) util.Check(err) var note map[string]interface{} err = json.Unmarshal(body, ¬e) util.Check(err) note[blob.Type] = myType b, err := blob.Marshal(note) util.Check(err) err = c.PutBlob(b) util.Check(err) w.Write(b.Content()) }
func (h *indexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { w.Header().Set(ActionStatus, ActionFailed) fmt.Println("index access failed: ", r) } }() name := req.Header.Get(IndexField) ind, ok := h.bs.inds[name] if !ok { panic("invalid index name") } it, err := ind.GetIter(req) util.Check(err) n, err := strconv.Atoi(req.Header.Get(ResultCountField)) util.Check(err) refs := multipart.NewWriter(w) w.Header().Set("Content-Type", "multipart/mixed") w.Header().Set(BoundaryField, refs.Boundary()) defer refs.Close() for i := 0; i < n; i++ { ref, err := it.Next() if err != nil { break } b, err := h.bs.Db.Get(ref) util.Check(err) part, err := refs.CreateFormFile("blob-ref", ref) util.Check(err) part.Write(b.Content()) } }
// GetMeta returns the Mount Notes associated with the given file. func (m *Mount) GetMeta(pth string) (mm *Meta, err error) { defer func() { recover() }() fm, err := m.getTip(pth) util.Check(err) mm = &Meta{} err = fm.GetNotes(Key, mm) if err != nil { mm = &Meta{} } return mm, nil }
func stripBlobs(c *blobserv.Client) []*shortblob { blobs, err := c.BlobsBackward(time.Now(), 20, 0) util.Check(err) short := []*shortblob{} for _, b := range blobs { buf := bytes.NewBuffer([]byte{}) json.Indent(buf, b.Content(), "", " ") short = append(short, &shortblob{b.Ref(), buf.String()}) } return short }
// Unauthorized checks for and handles cases where authentication can occur via // share blobs. func (h *getHandler) Unauthorized(w http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { w.Header().Set(ActionStatus, ActionFailed) fmt.Println("blob post failed: ", r) } }() shareRef := req.FormValue("via") if shareRef == "" { auth.SendUnauthorized(w) return } b, err := h.bs.Db.Get(shareRef) if err != nil { auth.SendUnauthorized(w) return } share := &blob.Share{} err = blob.Unmarshal(b, share) util.Check(err) ref := path.Base(req.URL.Path) b, err = h.bs.Db.Get(ref) util.Check(err) if !share.AuthorizedGet(b) { auth.SendUnauthorized(w) return } w.Header().Set(ActionStatus, ActionSuccess) w.Write(b.Content()) fmt.Println("successful retrieval") }
func (h *getHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { w.Header().Set(ActionStatus, ActionFailed) fmt.Println("blob post failed: ", r) } }() ref := path.Base(req.URL.Path) b, err := h.bs.Db.Get(ref) util.Check(err) w.Header().Set(ActionStatus, ActionSuccess) w.Write(b.Content()) fmt.Println("successful retrieval") }
func putfiles(c *blobserv.Client, w http.ResponseWriter, req *http.Request) { defer util.DeferPrint() mr, err := req.MultipartReader() util.Check(err) resps := []interface{}{} for part, err := mr.NextPart(); err == nil; { if name := part.FormName(); name == "" { continue } else if part.FileName() == "" { continue } resp := sendFileBlobs(c, part) resps = append(resps, resp) part, err = mr.NextPart() } data, _ := json.Marshal(resps) w.Write(data) }
func (h *putHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { w.Header().Set(ActionStatus, ActionFailed) fmt.Println("blob post issues: ", r) } }() body, err := ioutil.ReadAll(req.Body) util.Check(err) b := blob.NewRaw(body) err = h.bs.Db.Put(b) if err == blobdb.HashCollideErr { w.Header().Set(ActionStatus, ActionFailed) return } h.bs.notify(b) w.Header().Set(ActionStatus, ActionSuccess) fmt.Println("successful post") }
func (o *object) Add(b *blob.Blob) { o.versions = append(o.versions, b.Ref()) t, err := b.Timestamp() util.Check(err) o.tms = append(o.tms, t) }