mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-02-20 08:40:01 +00:00
layouts: more formating, and working towards a better look
assets: embedding stylesheets for formating
This commit is contained in:
parent
8c7adeb56c
commit
7e970f5dfd
2 changed files with 166 additions and 53 deletions
109
layouts.go
109
layouts.go
File diff suppressed because one or more lines are too long
110
server.go
110
server.go
|
@ -13,6 +13,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var defaultPageLimit int = 25
|
||||||
|
|
||||||
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)
|
log.Printf("Error: %s", e)
|
||||||
LogRequest(r,503)
|
LogRequest(r,503)
|
||||||
|
@ -44,19 +46,6 @@ func chunkURI(uri string) (chunks []string) {
|
||||||
return strings.Split(str, "/")
|
return strings.Split(str, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* given an url.URL.RawQuery, get a dictionary in return */
|
|
||||||
func parseRawQuery(qry string) (params map[string]string) {
|
|
||||||
qryChunks := strings.Split(qry, "&")
|
|
||||||
params = make(map[string]string, len(qryChunks))
|
|
||||||
for _, chunk := range qryChunks {
|
|
||||||
p := strings.SplitN(chunk, "=", 2)
|
|
||||||
if (len(p) == 2) {
|
|
||||||
params[p[0]] = p[1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return params
|
|
||||||
}
|
|
||||||
|
|
||||||
/* kindof a common log type output */
|
/* kindof a common log type output */
|
||||||
func LogRequest(r *http.Request, statusCode int) {
|
func LogRequest(r *http.Request, statusCode int) {
|
||||||
var addr string
|
var addr string
|
||||||
|
@ -192,8 +181,6 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
filename = fmt.Sprintf("%s%s", str, p_ext)
|
filename = fmt.Sprintf("%s%s", str, p_ext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Printf("%q\n", filename)
|
|
||||||
log.Printf("%t\n", p_ext)
|
|
||||||
|
|
||||||
exists, err := HasFileByFilename(filename)
|
exists, err := HasFileByFilename(filename)
|
||||||
if (err == nil && !exists) {
|
if (err == nil && !exists) {
|
||||||
|
@ -244,8 +231,12 @@ func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
io.WriteString(w,
|
if (strings.Contains(r.Header.Get("Accept"), "text/html")) {
|
||||||
fmt.Sprintf("<a href=\"/f/%s\">/f/%s</a>\n", filename, filename))
|
io.WriteString(w,
|
||||||
|
fmt.Sprintf("<a href=\"/f/%s\">/f/%s</a>\n", filename, filename))
|
||||||
|
} else {
|
||||||
|
io.WriteString(w, fmt.Sprintf("/f/%s\n", filename))
|
||||||
|
}
|
||||||
|
|
||||||
LogRequest(r,200)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
@ -310,9 +301,9 @@ func routeRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
// Show a page of most recent images, and tags, and uploaders ...
|
// Show a page of most recent images, and tags, and uploaders ...
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
//iter := gfs.Find(bson.M{"uploadDate": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(10).Iter()
|
//iter := gfs.Find(bson.M{"uploadDate": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(defaultPageLimit).Iter()
|
||||||
var files []File
|
var files []File
|
||||||
err := gfs.Find(nil).Sort("-metadata.timestamp").Limit(40).All(&files)
|
err := gfs.Find(nil).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
|
||||||
if (err != nil) {
|
if (err != nil) {
|
||||||
serverErr(w,r,err)
|
serverErr(w,r,err)
|
||||||
return
|
return
|
||||||
|
@ -358,23 +349,22 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
LogRequest(r,404)
|
LogRequest(r,404)
|
||||||
http.NotFound(w,r)
|
http.NotFound(w,r)
|
||||||
return
|
return
|
||||||
|
} else if (len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0)) {
|
||||||
|
routeRoot(w,r)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("K: %s (%d)", uriChunks, len(uriChunks))
|
log.Printf("K: %s (%d)", uriChunks, len(uriChunks))
|
||||||
params := parseRawQuery(r.URL.RawQuery)
|
|
||||||
log.Printf("K: params: %s", params)
|
|
||||||
|
|
||||||
var iter *mgo.Iter
|
var iter *mgo.Iter
|
||||||
if (len(uriChunks) == 1) {
|
if (uriChunks[len(uriChunks)-1] == "r") {
|
||||||
// show a sorted list of tag name links
|
|
||||||
iter = gfs.Find(bson.M{"metadata": bson.M{"keywords": uriChunks[1] } }).Sort("$natural").Limit(100).Iter()
|
|
||||||
} else if (len(uriChunks) == 2) {
|
|
||||||
iter = gfs.Find(bson.M{"metadata": bson.M{"keywords": uriChunks[1] } }).Limit(10).Iter()
|
|
||||||
} else if (uriChunks[2] == "r") {
|
|
||||||
// TODO determine how to show a random image by keyword ...
|
// TODO determine how to show a random image by keyword ...
|
||||||
log.Println("random isn't built yet")
|
log.Println("random isn't built yet")
|
||||||
LogRequest(r,404)
|
LogRequest(r,404)
|
||||||
return
|
return
|
||||||
|
} else if (len(uriChunks) == 2) {
|
||||||
|
log.Println(uriChunks[1])
|
||||||
|
iter = gfs.Find(bson.M{"metadata.keywords": uriChunks[1] }).Sort("-metadata.timestamp").Limit(defaultPageLimit).Iter()
|
||||||
}
|
}
|
||||||
|
|
||||||
var files []File
|
var files []File
|
||||||
|
@ -383,24 +373,36 @@ func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
||||||
serverErr(w,r,err)
|
serverErr(w,r,err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Println(len(files))
|
||||||
|
ListFilesPage(w, files)
|
||||||
|
|
||||||
|
LogRequest(r,200)
|
||||||
|
}
|
||||||
|
|
||||||
|
func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
||||||
|
uriChunks := chunkURI(r.URL.Path)
|
||||||
|
if (r.Method != "GET") {
|
||||||
|
LogRequest(r,404)
|
||||||
|
http.NotFound(w,r)
|
||||||
|
return
|
||||||
|
} else if (len(uriChunks) != 2) {
|
||||||
|
// they didn't give an MD5, re-route
|
||||||
|
routeRoot(w,r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var files []File
|
||||||
|
err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
|
||||||
|
if (err != nil) {
|
||||||
|
serverErr(w,r,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
ListFilesPage(w, files)
|
ListFilesPage(w, files)
|
||||||
|
|
||||||
LogRequest(r,200)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeList(w http.ResponseWriter, iter *mgo.Iter) {
|
// Show a page of file extensions, and allow paging by ext
|
||||||
var this_file File
|
|
||||||
fmt.Fprintf(w, "<ul>\n")
|
|
||||||
for iter.Next(&this_file) {
|
|
||||||
log.Println(this_file.Filename)
|
|
||||||
fmt.Fprintf(w, "<li>%s - %d</li>\n",
|
|
||||||
linkToFile("", this_file.Filename),
|
|
||||||
this_file.UploadDate.Year())
|
|
||||||
}
|
|
||||||
fmt.Fprintf(w, "</ul>\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show a page of all the uploader's IPs, and the images
|
|
||||||
func routeExt(w http.ResponseWriter, r *http.Request) {
|
func routeExt(w http.ResponseWriter, r *http.Request) {
|
||||||
if (r.Method != "GET") {
|
if (r.Method != "GET") {
|
||||||
LogRequest(r,404)
|
LogRequest(r,404)
|
||||||
|
@ -422,15 +424,24 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
||||||
LogRequest(r,200)
|
LogRequest(r,200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
GET /upload
|
||||||
|
POST /upload
|
||||||
|
*/
|
||||||
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
if (r.Method == "POST") {
|
if (r.Method == "POST") {
|
||||||
info := Info{
|
info := Info{
|
||||||
Ip: r.RemoteAddr,
|
Ip: r.RemoteAddr,
|
||||||
Random: Rand64(),
|
Random: Rand64(),
|
||||||
|
TimeStamp: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle the form posting to this route
|
// handle the form posting to this route
|
||||||
r.ParseMultipartForm(1024*5)
|
err := r.ParseMultipartForm(1024*5)
|
||||||
|
if (err != nil) {
|
||||||
|
serverErr(w,r,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Printf("%q", r.MultipartForm.Value)
|
log.Printf("%q", r.MultipartForm.Value)
|
||||||
for k, v := range r.MultipartForm.Value {
|
for k, v := range r.MultipartForm.Value {
|
||||||
if (k == "keywords") {
|
if (k == "keywords") {
|
||||||
|
@ -490,6 +501,20 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
LogRequest(r,200) // if we make it this far, then log success
|
LogRequest(r,200) // if we make it this far, then log success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func routeAssets(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path, err := filepath.Rel("/assets", r.URL.Path)
|
||||||
|
if (err != nil) {
|
||||||
|
serverErr(w,r,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch path {
|
||||||
|
case "bootstrap.css":
|
||||||
|
fmt.Fprint(w,bootstrapCSS)
|
||||||
|
w.Header().Set("Content-Type", "text/css")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initMongo() {
|
func initMongo() {
|
||||||
mongo_session, err := mgo.Dial(MongoHost)
|
mongo_session, err := mgo.Dial(MongoHost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -513,13 +538,14 @@ func runServer(ip, port string) {
|
||||||
defer mongo_session.Close()
|
defer mongo_session.Close()
|
||||||
|
|
||||||
http.HandleFunc("/", routeRoot)
|
http.HandleFunc("/", routeRoot)
|
||||||
|
http.HandleFunc("/assets/", routeAssets)
|
||||||
http.HandleFunc("/upload", routeUpload)
|
http.HandleFunc("/upload", routeUpload)
|
||||||
http.HandleFunc("/all", routeAll)
|
http.HandleFunc("/all", routeAll)
|
||||||
http.HandleFunc("/f/", routeFiles)
|
http.HandleFunc("/f/", routeFiles)
|
||||||
http.HandleFunc("/k/", routeKeywords)
|
http.HandleFunc("/k/", routeKeywords)
|
||||||
http.HandleFunc("/ip/", routeIPs)
|
http.HandleFunc("/ip/", routeIPs)
|
||||||
http.HandleFunc("/ext/", routeExt)
|
http.HandleFunc("/ext/", routeExt)
|
||||||
//http.HandleFunc("/md5/", routeMD5s)
|
http.HandleFunc("/md5/", routeMD5s)
|
||||||
|
|
||||||
log.Printf("Serving on %s ...", addr)
|
log.Printf("Serving on %s ...", addr)
|
||||||
log.Fatal(http.ListenAndServe(addr, nil))
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
|
|
Loading…
Add table
Reference in a new issue