mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-07-29 02:10:29 +00:00
moar pulling back of database calls from the server
This commit is contained in:
parent
6e439b0de4
commit
c737976121
3 changed files with 45 additions and 5 deletions
|
@ -5,12 +5,41 @@ import (
|
|||
"github.com/vbatts/imgsrv/types"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Util struct {
|
||||
Gfs *mgo.GridFS
|
||||
}
|
||||
|
||||
func (u Util) Create(filename string) (file *mgo.GridFile, err error) {
|
||||
return u.Gfs.Create(strings.ToLower(filename))
|
||||
}
|
||||
|
||||
/*
|
||||
Find files by their MD5 checksum
|
||||
*/
|
||||
func (u Util) FindFilesByMd5(md5 string) (files []types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"md5": md5}).Sort("-metadata.timestamp").All(&files)
|
||||
return files, err
|
||||
}
|
||||
|
||||
/*
|
||||
match for file name
|
||||
*/
|
||||
func (u Util) FindFilesByName(filename string) (files []types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"filename": filename}).Sort("-metadata.timestamp").All(&files)
|
||||
return files, err
|
||||
}
|
||||
|
||||
/*
|
||||
Case-insensitive pattern match for file name
|
||||
*/
|
||||
func (u Util) FindFilesByPatt(filename_pat string) (files []types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"filename": bson.M{ "$regex": filename_pat, "$options": "i"}}).Sort("-metadata.timestamp").All(&files)
|
||||
return files, err
|
||||
}
|
||||
|
||||
func (u Util) GetFileByFilename(filename string) (this_file types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue