mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-01-12 23:20:06 +00:00
now images are lower cased, and searching for names is insensitive.
for your mongo db set, run: db.fs.files.find().forEach( function(e) { e.filename = e.filename.toLowerCase(); db.fs.files.save(e); } )
This commit is contained in:
parent
80e9381754
commit
5eab319c6c
4 changed files with 33 additions and 25 deletions
|
@ -65,7 +65,7 @@ func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
|
||||||
/*
|
/*
|
||||||
get a list of keywords and their frequency count
|
get a list of keywords and their frequency count
|
||||||
*/
|
*/
|
||||||
func (u Util) GetKeywords() (kp []types.KeywordCount, err error) {
|
func (u Util) GetKeywords() (kp []types.IdCount, err error) {
|
||||||
job := &mgo.MapReduce{
|
job := &mgo.MapReduce{
|
||||||
Map: `
|
Map: `
|
||||||
function() {
|
function() {
|
||||||
|
|
|
@ -51,7 +51,6 @@ var navbarTemplateHTML = `
|
||||||
<li><a href="/k/">Keywords</a></li>
|
<li><a href="/k/">Keywords</a></li>
|
||||||
<li><a href="/ext/">File ext</a></li>
|
<li><a href="/ext/">File ext</a></li>
|
||||||
<li><a href="/md5/">MD5s</a></li>
|
<li><a href="/md5/">MD5s</a></li>
|
||||||
<li><a href="/ip/">IPs</a></li>
|
|
||||||
</ul>
|
</ul>
|
||||||
</div> <!-- dropdown -->
|
</div> <!-- dropdown -->
|
||||||
</div> <!-- nav-collapse -->
|
</div> <!-- nav-collapse -->
|
||||||
|
@ -178,7 +177,7 @@ var listTemplateHTML = `
|
||||||
var tagcloudTemplate = template.Must(template.New("tagcloud").Parse(tagcloudTemplateHTML))
|
var tagcloudTemplate = template.Must(template.New("tagcloud").Parse(tagcloudTemplateHTML))
|
||||||
var tagcloudTemplateHTML = `
|
var tagcloudTemplateHTML = `
|
||||||
{{if .}}
|
{{if .}}
|
||||||
<div id="keywordTagCloud">
|
<div id="tagCloud">
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<a href="/k/{{.Id}}" rel="{{.Value}}">{{.Id}}</a>
|
<a href="/k/{{.Id}}" rel="{{.Value}}">{{.Id}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -393,7 +392,7 @@ func ListFilesPage(w io.Writer, files []types.File) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListKeywordsPage(w io.Writer, kc []types.KeywordCount) (err error) {
|
func ListTagCloudPage(w io.Writer, ic []types.IdCount) (err error) {
|
||||||
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
|
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -408,7 +407,7 @@ func ListKeywordsPage(w io.Writer, kc []types.KeywordCount) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// main context of this page
|
// main context of this page
|
||||||
err = tagcloudTemplate.Execute(w, kc)
|
err = tagcloudTemplate.Execute(w, ic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
42
server.go
42
server.go
|
@ -52,9 +52,9 @@ func runServer(c *config.Config) {
|
||||||
http.HandleFunc("/f/", routeFiles)
|
http.HandleFunc("/f/", routeFiles)
|
||||||
http.HandleFunc("/v/", routeViews)
|
http.HandleFunc("/v/", routeViews)
|
||||||
http.HandleFunc("/k/", routeKeywords)
|
http.HandleFunc("/k/", routeKeywords)
|
||||||
http.HandleFunc("/ip/", routeIPs)
|
|
||||||
http.HandleFunc("/ext/", routeExt)
|
|
||||||
http.HandleFunc("/md5/", routeMD5s)
|
http.HandleFunc("/md5/", routeMD5s)
|
||||||
|
http.HandleFunc("/ext/", routeExt)
|
||||||
|
http.HandleFunc("/ip/", routeIPs)
|
||||||
|
|
||||||
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.Printf("Serving on %s ...", addr)
|
||||||
|
@ -119,7 +119,7 @@ func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
||||||
var file types.File
|
var file types.File
|
||||||
err := gfs.Find(bson.M{"filename": uriChunks[1]}).One(&file)
|
err := gfs.Find(bson.M{"filename": strings.ToLower(uriChunks[1])}).One(&file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -157,6 +157,8 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filename := strings.ToLower(uriChunks[1])
|
||||||
|
|
||||||
// if the Request got here by a delete request, confirm it
|
// if the Request got here by a delete request, confirm it
|
||||||
if (len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true") && (len(r.Form["confirm"]) > 0 && r.Form["confirm"][0] == "true") {
|
if (len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true") && (len(r.Form["confirm"]) > 0 && r.Form["confirm"][0] == "true") {
|
||||||
httplog.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
|
@ -164,7 +166,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
||||||
httplog.LogRequest(r, 200)
|
httplog.LogRequest(r, 200)
|
||||||
err = DeleteFilePage(w, uriChunks[1])
|
err = DeleteFilePage(w, filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -172,9 +174,9 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
if len(uriChunks) == 2 && len(filename) > 0 {
|
||||||
log.Printf("Searching for [%s] ...", uriChunks[1])
|
log.Printf("Searching for [%s] ...", filename)
|
||||||
query := gfs.Find(bson.M{"filename": uriChunks[1]})
|
query := gfs.Find(bson.M{"filename": filename})
|
||||||
|
|
||||||
c, err := query.Count()
|
c, err := query.Count()
|
||||||
// preliminary checks, if they've passed an image name
|
// preliminary checks, if they've passed an image name
|
||||||
|
@ -182,19 +184,19 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Results for [%s] = %d", uriChunks[1], c)
|
log.Printf("Results for [%s] = %d", filename, c)
|
||||||
if c == 0 {
|
if c == 0 {
|
||||||
httplog.LogRequest(r, 404)
|
httplog.LogRequest(r, 404)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := filepath.Ext(uriChunks[1])
|
ext := filepath.Ext(filename)
|
||||||
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||||
w.Header().Set("Cache-Control", "max-age=315360000")
|
w.Header().Set("Cache-Control", "max-age=315360000")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
file, err := gfs.Open(uriChunks[1])
|
file, err := gfs.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -233,7 +235,7 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
filename = r.FormValue("filename")
|
filename = r.FormValue("filename")
|
||||||
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
|
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
|
||||||
filename = uriChunks[1]
|
filename = strings.ToLower(uriChunks[1])
|
||||||
}
|
}
|
||||||
log.Printf("%s\n", filename)
|
log.Printf("%s\n", filename)
|
||||||
|
|
||||||
|
@ -462,7 +464,7 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ListKeywordsPage(w, kc)
|
err = ListTagCloudPage(w, kc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
}
|
}
|
||||||
|
@ -506,8 +508,16 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
} else if len(uriChunks) != 2 {
|
} else if len(uriChunks) != 2 {
|
||||||
// they didn't give an MD5, re-route
|
// Path: /md5/
|
||||||
routeRoot(w, r)
|
kc, err := du.GetKeywords()
|
||||||
|
if err != nil {
|
||||||
|
serverErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = ListTagCloudPage(w, kc)
|
||||||
|
if err != nil {
|
||||||
|
serverErr(w, r, err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -604,7 +614,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 := du.HasFileByFilename(filepath.Base(local_filename))
|
exists, err := du.HasFileByFilename(filepath.Base(strings.ToLower(local_filename)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -618,7 +628,7 @@ func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
||||||
stored_filename = filepath.Base(local_filename)
|
stored_filename = filepath.Base(local_filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := gfs.Create(stored_filename)
|
file, err := gfs.Create(strings.ToLower(stored_filename))
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverErr(w, r, err)
|
serverErr(w, r, err)
|
||||||
|
|
|
@ -44,10 +44,9 @@ func (f *File) IsAudio() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Structure used for collecting the keyword usage for the tag cloud
|
Structure used for collecting values from mongo for a tag cloud
|
||||||
*/
|
*/
|
||||||
type KeywordCount struct {
|
type IdCount struct {
|
||||||
Id string "_id"
|
Id string "_id"
|
||||||
Value int
|
Value int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue