mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-07-29 02:10:29 +00:00
all gfs calls are inside dbutil now
This commit is contained in:
parent
c737976121
commit
cebd3acc3c
2 changed files with 90 additions and 40 deletions
|
@ -5,15 +5,32 @@ import (
|
|||
"github.com/vbatts/imgsrv/types"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"strings"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Util struct {
|
||||
Gfs *mgo.GridFS
|
||||
}
|
||||
|
||||
/*
|
||||
pass through for GridFs
|
||||
*/
|
||||
func (u Util) Open(filename string) (file *mgo.GridFile, err error) {
|
||||
return u.Gfs.Open(strings.ToLower(filename))
|
||||
}
|
||||
|
||||
/*
|
||||
pass through for GridFs
|
||||
*/
|
||||
func (u Util) Create(filename string) (file *mgo.GridFile, err error) {
|
||||
return u.Gfs.Create(strings.ToLower(filename))
|
||||
return u.Gfs.Create(strings.ToLower(filename))
|
||||
}
|
||||
|
||||
/*
|
||||
pass through for GridFs
|
||||
*/
|
||||
func (u Util) Remove(filename string) (err error) {
|
||||
return u.Gfs.Remove(strings.ToLower(filename))
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -21,27 +38,60 @@ 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
|
||||
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
|
||||
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
|
||||
err = u.Gfs.Find(bson.M{"filename": bson.M{"$regex": filename_pat, "$options": "i"}}).Sort("-metadata.timestamp").All(&files)
|
||||
return files, err
|
||||
}
|
||||
|
||||
/*
|
||||
Case-insensitive pattern match for file name
|
||||
*/
|
||||
func (u Util) FindFilesByKeyword(keyword string) (files []types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"metadata.keywords": strings.ToLower(keyword)}).Sort("-metadata.timestamp").All(&files)
|
||||
return files, err
|
||||
}
|
||||
|
||||
/*
|
||||
Get all the files.
|
||||
|
||||
pass -1 for all files
|
||||
*/
|
||||
func (u Util) GetFiles(limit int) (files []types.File, err error) {
|
||||
if limit == -1 {
|
||||
err = u.Gfs.Find(nil).Sort("-metadata.timestamp").Limit(limit).All(&files)
|
||||
} else {
|
||||
err = u.Gfs.Find(nil).Sort("-metadata.timestamp").All(&files)
|
||||
}
|
||||
return files, err
|
||||
}
|
||||
|
||||
/*
|
||||
Count the filename matches
|
||||
*/
|
||||
func (u Util) CountFiles(filename string) (count int, err error) {
|
||||
query := u.Gfs.Find(bson.M{"filename": strings.ToLower(filename)})
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
/*
|
||||
Get one file back, by searching by file name
|
||||
*/
|
||||
func (u Util) GetFileByFilename(filename string) (this_file types.File, err error) {
|
||||
err = u.Gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
||||
err = u.Gfs.Find(bson.M{"filename": strings.ToLower(filename)}).One(&this_file)
|
||||
if err != nil {
|
||||
return this_file, err
|
||||
}
|
||||
|
@ -63,9 +113,11 @@ func (u Util) GetFileRandom() (this_file types.File, err error) {
|
|||
return this_file, nil
|
||||
}
|
||||
|
||||
/* Check whether this types.File filename is on Mongo */
|
||||
/*
|
||||
Check whether this types.File filename is on Mongo
|
||||
*/
|
||||
func (u Util) HasFileByFilename(filename string) (exists bool, err error) {
|
||||
c, err := u.Gfs.Find(bson.M{"filename": filename}).Count()
|
||||
c, err := u.CountFiles(filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -83,7 +135,7 @@ func (u Util) HasFileByMd5(md5 string) (exists bool, err error) {
|
|||
}
|
||||
|
||||
func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||
c, err := u.Gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
|
||||
c, err := u.Gfs.Find(bson.M{"metadata": bson.M{"keywords": strings.ToLower(keyword)}}).Count()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue