Esempio n. 1
0
func main() {
	flag.Parse()
	if *dirPath == "" {
		log.Fatal("Please specify directory path for uploaded files")
	}

	var s *Server = NewServer()
	s.Run()
	//For listing the available GridFS objects

	defer s.session.Close()

	var result *mgo.GridFile
	iter := s.gfs.Find(nil).Iter()

	for s.gfs.OpenNext(iter, &result) {
		fmt.Printf("Filename: %s\n", result.Name())
	}
	if iter.Err() != nil {
		panic(iter.Err())
	}
	//

	//http.HandleFunc("/", handleHome)
	/*addr := fmt.Sprintf(":%d", *port)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatal("Failed to run server: ", err)
	}*/

}
Esempio n. 2
0
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
}
Esempio n. 3
0
File: fs.go Progetto: 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
}
Esempio n. 4
0
func (s *S) TestGridFSOpenNext(c *C) {
	session, err := mgo.Dial("localhost:40011")
	c.Assert(err, IsNil)
	defer session.Close()

	db := session.DB("mydb")

	gfs := db.GridFS("fs")

	file, err := gfs.Create("myfile1.txt")
	c.Assert(err, IsNil)
	file.Write([]byte{'1'})
	file.Close()

	file, err = gfs.Create("myfile2.txt")
	c.Assert(err, IsNil)
	file.Write([]byte{'2'})
	file.Close()

	var f *mgo.GridFile
	var b [1]byte

	iter := gfs.Find(nil).Sort("-filename").Iter()

	ok := gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile2.txt")

	_, err = f.Read(b[:])
	c.Assert(err, IsNil)
	c.Assert(string(b[:]), Equals, "2")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile1.txt")

	_, err = f.Read(b[:])
	c.Assert(err, IsNil)
	c.Assert(string(b[:]), Equals, "1")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, false)
	c.Assert(iter.Close(), IsNil)
	c.Assert(f, IsNil)

	// Do it again with a more restrictive query to make sure
	// it's actually taken into account.
	iter = gfs.Find(bson.M{"filename": "myfile1.txt"}).Iter()

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, true)
	c.Check(f.Name(), Equals, "myfile1.txt")

	ok = gfs.OpenNext(iter, &f)
	c.Assert(ok, Equals, false)
	c.Assert(iter.Close(), IsNil)
	c.Assert(f, IsNil)
}
Esempio n. 5
0
// 查找文件
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())
	}
}
Esempio n. 6
0
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
}