mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 15:26:30 +00:00
now we have an /ext/ listing
This commit is contained in:
parent
5eab319c6c
commit
e3548407b2
2 changed files with 77 additions and 5 deletions
|
@ -62,6 +62,39 @@ func (u Util) HasFileByKeyword(keyword string) (exists bool, err error) {
|
|||
return exists, nil
|
||||
}
|
||||
|
||||
/*
|
||||
get a list of file extensions and their frequency count
|
||||
*/
|
||||
func (u Util) GetExtensions() (kp []types.IdCount, err error) {
|
||||
job := &mgo.MapReduce{
|
||||
Map: `
|
||||
function() {
|
||||
if (!this.filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
s = this.filename.split(".")
|
||||
ext = s[s.length - 1] // get the last segment of the split
|
||||
emit(ext,1);
|
||||
}
|
||||
`,
|
||||
Reduce: `
|
||||
function(previous, current) {
|
||||
var count = 0;
|
||||
|
||||
for (index in current) {
|
||||
count += current[index];
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
`,
|
||||
}
|
||||
if _, err := u.Gfs.Find(nil).MapReduce(job, &kp); err != nil {
|
||||
return kp, err
|
||||
}
|
||||
return kp, nil
|
||||
}
|
||||
/*
|
||||
get a list of keywords and their frequency count
|
||||
*/
|
||||
|
|
49
server.go
49
server.go
|
@ -486,13 +486,13 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
|||
iter = gfs.Find(bson.M{"metadata.keywords": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).Iter()
|
||||
}
|
||||
|
||||
var files []types.File
|
||||
files := []types.File{}
|
||||
err := iter.All(&files)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
log.Println(len(files))
|
||||
log.Printf("collected %d files", len(files))
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
|
@ -521,7 +521,7 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var files []types.File
|
||||
files := []types.File{}
|
||||
err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
|
@ -535,12 +535,51 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
|||
httplog.LogRequest(r, 200)
|
||||
}
|
||||
|
||||
// Show a page of file extensions, and allow paging by ext
|
||||
/*
|
||||
GET /ext/
|
||||
GET /ext/:name
|
||||
GET /ext/:name/r
|
||||
|
||||
Show a page of file extensions, and allow paging by ext
|
||||
If /ext/name/r then show a random image by keyword name
|
||||
Otherwise 404
|
||||
*/
|
||||
func routeExt(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" {
|
||||
uriChunks := chunkURI(r.URL.Path)
|
||||
if r.Method != "GET" ||
|
||||
len(uriChunks) > 3 ||
|
||||
(len(uriChunks) == 3 && uriChunks[2] != "r") {
|
||||
httplog.LogRequest(r, 404)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
||||
// Path: /ext/
|
||||
// tag cloud of extensions used
|
||||
ic, err := du.GetExtensions()
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
log.Printf("ext: %#v", ic)
|
||||
err = ListTagCloudPage(w, ic)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ext := strings.ToLower(uriChunks[1])
|
||||
ext_pat := fmt.Sprintf("/%s$/", ext)
|
||||
files := []types.File{}
|
||||
err := gfs.Find(bson.M{"filename": ext_pat}).Sort("-metadata.timestamp").All(&files)
|
||||
if err != nil {
|
||||
serverErr(w, r, err)
|
||||
return
|
||||
}
|
||||
log.Printf("collected %d files, with ext %s", len(files), ext)
|
||||
err = ListFilesPage(w, files)
|
||||
if err != nil {
|
||||
log.Printf("error: %s", err)
|
||||
}
|
||||
|
||||
httplog.LogRequest(r, 200)
|
||||
|
|
Loading…
Reference in a new issue