mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-11-27 10:35:41 +00:00
seperated out the db utilities. Also adding link for "other" views.
This commit is contained in:
parent
cacdb3aaa5
commit
83cf991ef9
4 changed files with 50 additions and 33 deletions
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package dbutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/vbatts/imgsrv/hash"
|
"github.com/vbatts/imgsrv/hash"
|
||||||
|
@ -7,24 +7,26 @@ import (
|
||||||
"labix.org/v2/mgo/bson"
|
"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) {
|
func (u Util) GetFileByFilename(filename string) (this_file types.File, err error) {
|
||||||
err = gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
err = u.Gfs.Find(bson.M{"filename": filename}).One(&this_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return this_file, err
|
return this_file, err
|
||||||
}
|
}
|
||||||
return this_file, nil
|
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()
|
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 {
|
if err != nil {
|
||||||
return this_file, err
|
return this_file, err
|
||||||
}
|
}
|
||||||
if len(this_file.Md5) == 0 {
|
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 {
|
if err != nil {
|
||||||
return this_file, err
|
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 */
|
/* Check whether this types.File filename is on Mongo */
|
||||||
func HasFileByFilename(filename string) (exists bool, err error) {
|
func (u Util) HasFileByFilename(filename string) (exists bool, err error) {
|
||||||
c, err := gfs.Find(bson.M{"filename": filename}).Count()
|
c, err := u.Gfs.Find(bson.M{"filename": filename}).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -42,8 +44,8 @@ func HasFileByFilename(filename string) (exists bool, err error) {
|
||||||
return exists, nil
|
return exists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HasFileByMd5(md5 string) (exists bool, err error) {
|
func (u Util) HasFileByMd5(md5 string) (exists bool, err error) {
|
||||||
c, err := gfs.Find(bson.M{"md5": md5}).Count()
|
c, err := u.Gfs.Find(bson.M{"md5": md5}).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -51,8 +53,8 @@ func HasFileByMd5(md5 string) (exists bool, err error) {
|
||||||
return exists, nil
|
return exists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HasFileByKeyword(keyword string) (exists bool, err error) {
|
func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||||
c, err := gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
|
c, err := u.Gfs.Find(bson.M{"metadata": bson.M{"keywords": keyword}}).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -60,7 +62,10 @@ func HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||||
return exists, nil
|
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{
|
job := &mgo.MapReduce{
|
||||||
Map: `
|
Map: `
|
||||||
function() {
|
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, err
|
||||||
}
|
}
|
||||||
return kp, nil
|
return kp, nil
|
22
layouts.go
22
layouts.go
|
@ -45,10 +45,20 @@ var navbarTemplateHTML = `
|
||||||
<li><a href="/urlie">URLie</a></li>
|
<li><a href="/urlie">URLie</a></li>
|
||||||
<li><a href="/all">All</a></li>
|
<li><a href="/all">All</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
<div class="dropdown nav pull-right">
|
||||||
</div>
|
<a role="button" data-toggle="dropdown" href="#">Other<b class="caret"></b></a>
|
||||||
</div>
|
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
|
||||||
</div>
|
<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))
|
var containerBeginTemplate = template.Must(template.New("containerBegin").Parse(containerBeginTemplateHTML))
|
||||||
|
@ -84,9 +94,9 @@ var formDeleteFileTemplateHTML = `
|
||||||
<br/>
|
<br/>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="/v/{{.}}">no!</a>
|
<a role="button" href="/v/{{.}}">no!</a>
|
||||||
<br/>
|
<br/>
|
||||||
<a href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
|
<a role="button" href="/f/{{.}}?delete=true&confirm=true">yes! delete!</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
16
server.go
16
server.go
|
@ -13,10 +13,11 @@ import (
|
||||||
|
|
||||||
"github.com/vbatts/go-httplog"
|
"github.com/vbatts/go-httplog"
|
||||||
"github.com/vbatts/imgsrv/assets"
|
"github.com/vbatts/imgsrv/assets"
|
||||||
|
"github.com/vbatts/imgsrv/config"
|
||||||
|
"github.com/vbatts/imgsrv/dbutil"
|
||||||
"github.com/vbatts/imgsrv/hash"
|
"github.com/vbatts/imgsrv/hash"
|
||||||
"github.com/vbatts/imgsrv/types"
|
"github.com/vbatts/imgsrv/types"
|
||||||
"github.com/vbatts/imgsrv/util"
|
"github.com/vbatts/imgsrv/util"
|
||||||
"github.com/vbatts/imgsrv/config"
|
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
)
|
)
|
||||||
|
@ -28,7 +29,7 @@ var (
|
||||||
mongo_session *mgo.Session // FIXME make this not global
|
mongo_session *mgo.Session // FIXME make this not global
|
||||||
images_db *mgo.Database // FIXME make this not global
|
images_db *mgo.Database // FIXME make this not global
|
||||||
gfs *mgo.GridFS // FIXME make this not global
|
gfs *mgo.GridFS // FIXME make this not global
|
||||||
|
du dbutil.Util
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -73,6 +74,7 @@ func initMongo() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gfs = images_db.GridFS("fs")
|
gfs = images_db.GridFS("fs")
|
||||||
|
du.Gfs = gfs
|
||||||
}
|
}
|
||||||
|
|
||||||
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
|
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 {
|
if err == nil && !exists {
|
||||||
file, err := gfs.Create(filename)
|
file, err := gfs.Create(filename)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
@ -340,7 +342,7 @@ func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := HasFileByFilename(uriChunks[1])
|
exists, err := du.HasFileByFilename(uriChunks[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
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) {
|
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||||
// Path: /k/
|
// Path: /k/
|
||||||
// show a tag cloud!
|
// show a tag cloud!
|
||||||
kc, err := GetKeywords()
|
kc, err := du.GetKeywords()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
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)
|
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 {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -691,7 +693,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
filehdr := r.MultipartForm.File["filename"][0]
|
filehdr := r.MultipartForm.File["filename"][0]
|
||||||
filename := filehdr.Filename
|
filename := filehdr.Filename
|
||||||
exists, err := HasFileByFilename(filename)
|
exists, err := du.HasFileByFilename(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue