1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-26 15:56:31 +00:00
imgsrv/get.go

33 lines
705 B
Go
Raw Normal View History

package main
import (
2013-02-09 04:43:39 +00:00
"math/rand"
"labix.org/v2/mgo/bson"
)
/* gfs is a *mgo.GridFS defined in imgsrv.go */
func GetFileByFilename(filename string) (this_file File, err error) {
err = gfs.Find(bson.M{"filename":filename}).One(&this_file)
if (err != nil) {
return this_file, err
}
return this_file, nil
}
func GetFileRandom() (this_file File, err error) {
2013-02-09 04:43:39 +00:00
r := rand.Int63()
err = gfs.Find(bson.M{"random": bson.M{"$gt" : r } }).One(&this_file)
if (err != nil) {
return this_file, err
}
if (len(this_file.Md5) == 0) {
err = gfs.Find(bson.M{"random": bson.M{"$lt" : r } }).One(&this_file)
}
if (err != nil) {
return this_file, err
}
return this_file, nil
}