1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-27 10:35:41 +00:00

moar pulling back of database calls from the server

This commit is contained in:
Vincent Batts 2013-10-03 17:04:37 -04:00
parent 6e439b0de4
commit c737976121
3 changed files with 45 additions and 5 deletions

View file

@ -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 {

View file

@ -521,8 +521,7 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
return
}
files := []types.File{}
err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
files, err := du.FindFilesByMd5(uriChunks[1])
if err != nil {
serverErr(w, r, err)
return
@ -570,8 +569,7 @@ func routeExt(w http.ResponseWriter, r *http.Request) {
ext := strings.ToLower(uriChunks[1])
ext_pat := fmt.Sprintf("%s$", ext)
files := []types.File{}
err := gfs.Find(bson.M{"filename": bson.M{ "$regex": ext_pat, "$options": "i"}}).Sort("-metadata.timestamp").All(&files)
files, err := du.FindFilesByPatt(ext_pat)
if err != nil {
serverErr(w, r, err)
return
@ -667,7 +665,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
stored_filename = filepath.Base(local_filename)
}
file, err := gfs.Create(strings.ToLower(stored_filename))
file, err := du.Create(stored_filename)
defer file.Close()
if err != nil {
serverErr(w, r, err)

13
server/server.go Normal file
View file

@ -0,0 +1,13 @@
package server
import (
"github.com/gorilla/mux"
"github.com/vbatts/imgsrv/dbutil"
)
type Web struct {
Router *mux.Router
Store dbutil.Util
}