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

* more logging on upload failures.

* adding an error page
* trying to fix the tag cloud
This commit is contained in:
Vincent Batts 2013-09-10 14:51:44 -04:00
parent 1c14a76877
commit d4824ad6a2
4 changed files with 68 additions and 18 deletions

View file

@ -93,8 +93,13 @@ func (u Util) GetExtensions() (kp []types.IdCount, err error) {
if _, err := u.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
} }
// Less than effecient, but cleanest place to put this
for i := range kp {
kp[i].Root = "ext" // for extension. Maps to /ext/
}
return kp, nil return kp, nil
} }
/* /*
get a list of keywords and their frequency count get a list of keywords and their frequency count
*/ */
@ -126,5 +131,9 @@ func (u Util) GetKeywords() (kp []types.IdCount, err error) {
if _, err := u.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
} }
// Less than effecient, but cleanest place to put this
for i := range kp {
kp[i].Root = "k" // for keyword. Maps to /k/
}
return kp, nil return kp, nil
} }

View file

@ -179,7 +179,7 @@ var tagcloudTemplateHTML = `
{{if .}} {{if .}}
<div id="tagCloud"> <div id="tagCloud">
{{range .}} {{range .}}
<a href="/k/{{.Id}}" rel="{{.Value}}">{{.Id}}</a> <a href="/{{.Root}}/{{.Id}}" rel="{{.Value}}">{{.Id}}</a>
{{end}} {{end}}
</div> </div>
{{end}} {{end}}
@ -418,3 +418,41 @@ func ListTagCloudPage(w io.Writer, ic []types.IdCount) (err error) {
} }
return return
} }
func ErrorPage(w io.Writer, e error) (err error) {
err = headTemplate.Execute(w, map[string]string{"title": "FileSrv :: ERROR"})
if err != nil {
return err
}
err = navbarTemplate.Execute(w, nil)
if err != nil {
return err
}
err = containerBeginTemplate.Execute(w, nil)
if err != nil {
return err
}
// main context of this page
err = template.Must(template.New("serverError").Parse(`
{{if .}}
<div class="span9">
<div class="hero-unit">
<h3>ERROR!</h3>
<div class="error">
{{.Error()}}
</div>
</div> {{/* hero-unit */}}
</div> {{/* span9 */}}
{{end}}
`)).Execute(w, e)
if err != nil {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer": fmt.Sprintf("Version: %s", VERSION)})
if err != nil {
return err
}
return
}

View file

@ -78,10 +78,10 @@ func initMongo() {
} }
func serverErr(w http.ResponseWriter, r *http.Request, e error) { func serverErr(w http.ResponseWriter, r *http.Request, e error) {
log.Printf("Error: %s", e)
httplog.LogRequest(r, 503) httplog.LogRequest(r, 503)
fmt.Fprintf(w, "Error: %s", e) log.Printf("Error: %s", e)
http.Error(w, "Service Unavailable", 503) w.WriteHeader(503)
//ErrorPage(w, err)
return return
} }
@ -157,7 +157,7 @@ func routeFilesGET(w http.ResponseWriter, r *http.Request) {
return return
} }
filename := strings.ToLower(uriChunks[1]) 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") {
@ -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() iter = gfs.Find(bson.M{"metadata.keywords": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).Iter()
} }
files := []types.File{} files := []types.File{}
err := iter.All(&files) err := iter.All(&files)
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
return return
} }
log.Printf("collected %d files", len(files)) log.Printf("collected %d files", len(files))
err = ListFilesPage(w, files) err = ListFilesPage(w, files)
if err != nil { if err != nil {
log.Printf("error: %s", err) log.Printf("error: %s", err)
@ -508,7 +508,7 @@ 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 {
// Path: /md5/ // Path: /md5/
kc, err := du.GetKeywords() kc, err := du.GetKeywords()
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
@ -521,7 +521,7 @@ func routeMD5s(w http.ResponseWriter, r *http.Request) {
return return
} }
files := []types.File{} files := []types.File{}
err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files) err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
@ -553,14 +553,14 @@ func routeExt(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r) http.NotFound(w, r)
return return
} 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: /ext/ // Path: /ext/
// tag cloud of extensions used // tag cloud of extensions used
ic, err := du.GetExtensions() ic, err := du.GetExtensions()
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
return return
} }
log.Printf("ext: %#v", ic) log.Printf("ext: %#v", ic)
err = ListTagCloudPage(w, ic) err = ListTagCloudPage(w, ic)
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
@ -568,15 +568,15 @@ func routeExt(w http.ResponseWriter, r *http.Request) {
return return
} }
ext := strings.ToLower(uriChunks[1]) ext := strings.ToLower(uriChunks[1])
ext_pat := fmt.Sprintf("/%s$/", ext) ext_pat := fmt.Sprintf("/%s$/", ext)
files := []types.File{} files := []types.File{}
err := gfs.Find(bson.M{"filename": ext_pat}).Sort("-metadata.timestamp").All(&files) err := gfs.Find(bson.M{"filename": ext_pat}).Sort("-metadata.timestamp").All(&files)
if err != nil { if err != nil {
serverErr(w, r, err) serverErr(w, r, err)
return return
} }
log.Printf("collected %d files, with ext %s", len(files), ext) log.Printf("collected %d files, with ext %s", len(files), ext)
err = ListFilesPage(w, files) err = ListFilesPage(w, files)
if err != nil { if err != nil {
log.Printf("error: %s", err) log.Printf("error: %s", err)
@ -750,12 +750,13 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
if exists || useRandName { if exists || useRandName {
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
str := hash.GetSmallHash() str := hash.GetSmallHash()
filename = fmt.Sprintf("%s%s", str, ext) filename = strings.ToLower(fmt.Sprintf("%s%s", str, ext))
} }
file, err := gfs.Create(filename) file, err := gfs.Create(filename)
defer file.Close() defer file.Close()
if err != nil { if err != nil {
log.Printf("Failed to create on gfs: %s", err)
serverErr(w, r, err) serverErr(w, r, err)
return return
} }
@ -763,11 +764,12 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
multiFile, err := filehdr.Open() multiFile, err := filehdr.Open()
if err != nil { if err != nil {
log.Println(err) log.Printf("Failed to open from MultipartForm: %s", err)
return return
} }
n, err := io.Copy(file, multiFile) n, err := io.Copy(file, multiFile)
if err != nil { if err != nil {
log.Printf("Failed copy from MultipartForm to gfs: %s", err)
serverErr(w, r, err) serverErr(w, r, err)
return return
} }

View file

@ -49,4 +49,5 @@ Structure used for collecting values from mongo for a tag cloud
type IdCount struct { type IdCount struct {
Id string "_id" Id string "_id"
Value int Value int
Root string
} }