예제 #1
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
}
예제 #2
0
파일: mongodb.go 프로젝트: petemoore/runlib
func (s *mongodbStorage) Cleanup(latest int) error {
	pids := s.getAllProblemIds()
	for _, v := range pids {
		s.doCleanup(v, latest)
	}

	pids = s.getAllGridPrefixes()
	iter := s.GridFS.Find(nil).Sort("filename").Iter()
	var f *mgo.GridFile
	for s.GridFS.OpenNext(iter, &f) {
		if !strings.HasPrefix(f.Name(), "problem/") {
			continue
		}
		if !hasAnyPrefix(f.Name(), pids) {
			fmt.Printf("Remove: %s\n", f.Name())
			s.GridFS.RemoveId(f.Id())
		}
	}
	return nil
}
예제 #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
}
예제 #4
0
파일: file.go 프로젝트: hyndio/hyd.me
// 查找文件
func FindAllFiles() {
	gfs := db.GridFS("fs")
	iter := gfs.Find(nil).Iter()
	var f *mgo.GridFile
	for gfs.OpenNext(iter, &f) {
		fmt.Printf("Filename: %s, %v\n", f.Name(), f.Id())
		b := make([]byte, 512)
		w, err := os.Create("upload/" + f.Name())
		if err != nil {
			continue
		}
		for {
			n, _ := f.Read(b)
			if n == 0 {
				break
			}
			w.Write(b[0:n])
		}
		defer w.Close()
	}
	if iter.Err() != nil {
		panic(iter.Err())
	}
}