func (self *backend) LoadImage(id string) (*media.Image, error) { doc, err := self.images.DocumentWithID(bson.ObjectIdHex(id)) if err != nil { return nil, err } return doc.(*ImageDoc).GetAndInitImage(), nil }
func (s *S) TestObjectIdJSONMarshaling(c *C) { id := bson.ObjectIdHex("4d88e15b60f486e428412dc9") v := jsonType{Id: &id} data, err := json.Marshal(&v) c.Assert(err, IsNil) c.Assert(string(data), Equals, `{"Id":"4d88e15b60f486e428412dc9"}`) }
func (s *S) TestObjectIdJSONUnmarshaling(c *C) { data := []byte(`{"Id":"4d88e15b60f486e428412dc9"}`) v := jsonType{} err := json.Unmarshal(data, &v) c.Assert(err, IsNil) c.Assert(*v.Id, Equals, bson.ObjectIdHex("4d88e15b60f486e428412dc9")) }
func (self *Backend) TryLoadImage(id string) (*media.Image, bool, error) { doc, found, err := self.Images.TryDocumentWithID(bson.ObjectIdHex(id)) if !found { return nil, found, err } return doc.(*ImageDoc).GetAndInitImage(), true, nil }
func (self *backend) ImageVersionReader(id string) (reader io.ReadCloser, ctype string, err error) { file, err := self.gridFS.OpenId(bson.ObjectIdHex(id)) if err == mgo.NotFound { return nil, "", media.ErrInvalidImageID(id) } else if err != nil { return nil, "", err } return file, file.ContentType(), nil }
func (self *backend) DeleteImage(image *media.Image) error { for i := range image.Versions { err := self.DeleteImageVersion(image.Versions[i].ID.Get()) if err != nil { return err } } return self.images.Remove(bson.ObjectIdHex(image.ID.Get())) }
func (self *Backend) LoadImage(id string) (*media.Image, error) { doc, err := self.images.DocumentWithID(bson.ObjectIdHex(id)) if err != nil { return nil, err } image := &doc.(*ImageDoc).Image image.Init() return image, nil }
func (self *Ref) SetString(str string) error { switch len(str) { case 0, 12: self.ID = bson.ObjectId(str) case 24: self.ID = bson.ObjectIdHex(str) default: return errs.Format("Invalid string for bson.ObjectId: '%s'", str) } return nil }
func (self *backend) SaveImage(image *media.Image) error { if image.ID == "" { doc := self.images.NewDocument().(*ImageDoc) doc.Image = *image id, err := self.images.Insert(doc) if err != nil { return err } image.ID.Set(id.Hex()) doc.Image.ID = image.ID return self.images.Update(id, doc) } id := bson.ObjectIdHex(image.ID.Get()) doc := self.images.NewDocument().(*ImageDoc) doc.SetObjectId(id) doc.Image = *image return self.images.Update(id, doc) }
func (s *S) TestObjectIdHex(c *C) { id := bson.ObjectIdHex("4d88e15b60f486e428412dc9") c.Assert(id.String(), Equals, `ObjectIdHex("4d88e15b60f486e428412dc9")`) c.Assert(id.Hex(), Equals, "4d88e15b60f486e428412dc9") }
} // -------------------------------------------------------------------------- // ObjectId parts extraction tests. type objectIdParts struct { id bson.ObjectId timestamp int64 machine []byte pid uint16 counter int32 } var objectIds = []objectIdParts{ objectIdParts{ bson.ObjectIdHex("4d88e15b60f486e428412dc9"), 1300816219, []byte{0x60, 0xf4, 0x86}, 0xe428, 4271561, }, objectIdParts{ bson.ObjectIdHex("000000000000000000000000"), 0, []byte{0x00, 0x00, 0x00}, 0x0000, 0, }, objectIdParts{ bson.ObjectIdHex("00000000aabbccddee000001"), 0,
func FindByID(id string) (userDoc interface{}, found bool, err error) { return Config.Collection.TryDocumentWithID(bson.ObjectIdHex(id)) }