1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-23 16:45:39 +00:00

seperated out the db utilities. Also adding link for "other" views.

This commit is contained in:
Vincent Batts 2013-08-05 23:51:26 -04:00
parent cacdb3aaa5
commit 83cf991ef9
4 changed files with 50 additions and 33 deletions

View file

@ -1,4 +1,4 @@
package main
package dbutil
import (
"github.com/vbatts/imgsrv/hash"
@ -7,24 +7,26 @@ import (
"labix.org/v2/mgo/bson"
)
/* gfs is a *mgo.GridFS defined in imgsrv.go */
type Util struct {
Gfs *mgo.GridFS
}
func GetFileByFilename(filename string) (this_file types.File, err error) {
err = gfs.Find(bson.M{"filename": filename}).One(&this_file)
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 {
return this_file, err
}
return this_file, nil
}
func GetFileRandom() (this_file types.File, err error) {
func (u Util) GetFileRandom() (this_file types.File, err error) {
r := hash.Rand64()
err = gfs.Find(bson.M{"random": bson.M{"$gt": r}}).One(&this_file)
err = u.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)
err = u.Gfs.Find(bson.M{"random": bson.M{"$lt": r}}).One(&this_file)
}
if err != nil {
return this_file, err
@ -33,8 +35,8 @@ func GetFileRandom() (this_file types.File, err error) {
}
/* Check whether this types.File filename is on Mongo */
func HasFileByFilename(filename string) (exists bool, err error) {
c, err := gfs.Find(bson.M{"filename": filename}).Count()
func (u Util) HasFileByFilename(filename string) (exists bool, err error) {
c, err := u.Gfs.Find(bson.M{"filename": filename}).Count()
if err != nil {
return false, err
}
@ -42,8 +44,8 @@ func HasFileByFilename(filename string) (exists bool, err error) {
return exists, nil
}
func HasFileByMd5(md5 string) (exists bool, err error) {
c, err := gfs.Find(bson.M{"md5": md5}).Count()
func (u Util) HasFileByMd5(md5 string) (exists bool, err error) {
c, err := u.Gfs.Find(bson.M{"md5": md5}).Count()
if err != nil {
return false, err
}
@ -51,8 +53,8 @@ func HasFileByMd5(md5 string) (exists bool, err error) {
return exists, nil
}
func HasFileByKeyword(keyword string) (exists bool, err error) {
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
c, err := u.Gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
if err != nil {
return false, err
}
@ -60,7 +62,10 @@ func HasFileByKeyword(keyword string) (exists bool, err error) {
return exists, nil
}
func GetKeywords() (kp []types.KeywordCount, err error) {
/*
get a list of keywords and their frequency count
*/
func (u Util) GetKeywords() (kp []types.KeywordCount, err error) {
job := &mgo.MapReduce{
Map: `
function() {
@ -85,7 +90,7 @@ func GetKeywords() (kp []types.KeywordCount, err error) {
}
`,
}
if _, err := gfs.Find(nil).MapReduce(job, &kp); err != nil {
if _, err := u.Gfs.Find(nil).MapReduce(job, &kp); err != nil {
return kp, err
}
return kp, nil

View file

@ -69,7 +69,7 @@ func main() {
log.Println(file)
} else {
// we're pushing up a file
// we're pushing up a file
if len(DefaultConfig.RemoteHost) == 0 {
log.Println("Please provide a remotehost!")

View file

@ -45,10 +45,20 @@ var navbarTemplateHTML = `
<li><a href="/urlie">URLie</a></li>
<li><a href="/all">All</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="dropdown nav pull-right">
<a role="button" data-toggle="dropdown" href="#">Other<b class="caret"></b></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
<li><a href="/k/">Keywords</a></li>
<li><a href="/ext/">File ext</a></li>
<li><a href="/md5/">MD5s</a></li>
<li><a href="/ip/">IPs</a></li>
</ul>
</div> <!-- dropdown -->
</div> <!-- nav-collapse -->
</div> <!-- container-fluid -->
</div> <!-- navbar-inner -->
</div> <!-- navbar top -->
<script>$('.dropdown-toggle').dropdown()</script>
`
var containerBeginTemplate = template.Must(template.New("containerBegin").Parse(containerBeginTemplateHTML))
@ -84,9 +94,9 @@ var formDeleteFileTemplateHTML = `
<br/>
<tr>
<td>
<a href="/v/{{.}}">no!</a>
<a role="button" href="/v/{{.}}">no!</a>
<br/>
<a href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
<a role="button" href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
</td>
</tr>
</table>

View file

@ -13,29 +13,30 @@ import (
"github.com/vbatts/go-httplog"
"github.com/vbatts/imgsrv/assets"
"github.com/vbatts/imgsrv/config"
"github.com/vbatts/imgsrv/dbutil"
"github.com/vbatts/imgsrv/hash"
"github.com/vbatts/imgsrv/types"
"github.com/vbatts/imgsrv/util"
"github.com/vbatts/imgsrv/config"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
var (
defaultPageLimit int = 25
serverConfig config.Config
defaultPageLimit int = 25
serverConfig config.Config
mongo_session *mgo.Session // FIXME make this not global
images_db *mgo.Database // FIXME make this not global
gfs *mgo.GridFS // FIXME make this not global
du dbutil.Util
)
/*
Run as the file/image server
*/
func runServer(c *config.Config) {
serverConfig = *c
serverConfig = *c
initMongo()
defer mongo_session.Close()
@ -55,7 +56,7 @@ func runServer(c *config.Config) {
http.HandleFunc("/ext/", routeExt)
http.HandleFunc("/md5/", routeMD5s)
addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
log.Printf("Serving on %s ...", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
@ -73,6 +74,7 @@ func initMongo() {
}
}
gfs = images_db.GridFS("fs")
du.Gfs = gfs
}
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
@ -266,7 +268,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
}
}
exists, err := HasFileByFilename(filename)
exists, err := du.HasFileByFilename(filename)
if err == nil && !exists {
file, err := gfs.Create(filename)
defer file.Close()
@ -340,7 +342,7 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
return
}
exists, err := HasFileByFilename(uriChunks[1])
exists, err := du.HasFileByFilename(uriChunks[1])
if err != nil {
serverErr(w, r, err)
return
@ -455,7 +457,7 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
// Path: /k/
// show a tag cloud!
kc, err := GetKeywords()
kc, err := du.GetKeywords()
if err != nil {
serverErr(w, r, err)
return
@ -602,7 +604,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
}
}
exists, err := HasFileByFilename(filepath.Base(local_filename))
exists, err := du.HasFileByFilename(filepath.Base(local_filename))
if err != nil {
serverErr(w, r, err)
return
@ -691,7 +693,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
filehdr := r.MultipartForm.File["filename"][0]
filename := filehdr.Filename
exists, err := HasFileByFilename(filename)
exists, err := du.HasFileByFilename(filename)
if err != nil {
serverErr(w, r, err)
return