コード例 #1
0
ファイル: mongodb.go プロジェクト: jetsanix/jfu
// ContentType returns the content type for the saved file indicated by key,
// or returns a FileNotFoundError if no such file exists.
func (s mongoStore) ContentType(key string) (ct string, err error) {
	var file mgo.GridFile
	q := s.gfs.Find(bson.M{"_id": key})
	err = q.One(&file)
	switch {
	case err == mgo.ErrNotFound:
		return ct, FileNotFoundError
	case err != nil:
		return
	}
	return file.ContentType(), nil
}
コード例 #2
0
ファイル: fs.go プロジェクト: jmptrader/tenpu
func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile
	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		if attachment.Id != "" {
			f.SetId(bson.ObjectIdHex(attachment.Id))
		}
		f.SetContentType(contentType)
		_, err = io.Copy(f, body)
	})

	if err == io.ErrUnexpectedEOF {
		attId := f.Id().(bson.ObjectId)
		s.Delete(attId.Hex())
		return
	}

	if attachment.Id == "" {
		attachment.Id = f.Id().(bson.ObjectId).Hex()
		attachment.ContentLength = f.Size()
		attachment.ContentType = f.ContentType()
		attachment.Filename = f.Name()
		attachment.MD5 = f.MD5()
		if attachment.IsImage() {
			s.database.DatabaseDo(func(db *mgo.Database) {
				f, err := db.GridFS("fs").OpenId(bson.ObjectIdHex(attachment.Id))
				if err == nil {
					config, _, err := image.DecodeConfig(f)
					f.Close()
					if err == nil {
						attachment.Width = config.Width
						attachment.Height = config.Height
					}
				}
			})
		}
	}

	return
}
コード例 #3
0
ファイル: fs.go プロジェクト: athom/tenpu
func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile

	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		f.SetContentType(contentType)
		io.Copy(f, body)

	})

	attachment.Id = f.Id().(bson.ObjectId).Hex()
	attachment.ContentLength = f.Size()
	attachment.ContentType = f.ContentType()
	attachment.Filename = f.Name()
	attachment.MD5 = f.MD5()
	return
}