2013-02-09 04:52:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-03-27 20:30:24 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2013-08-05 22:15:06 +00:00
|
|
|
|
|
|
|
"github.com/vbatts/go-httplog"
|
|
|
|
"github.com/vbatts/imgsrv/assets"
|
|
|
|
"github.com/vbatts/imgsrv/hash"
|
|
|
|
"github.com/vbatts/imgsrv/types"
|
|
|
|
"github.com/vbatts/imgsrv/util"
|
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
2013-02-09 04:52:40 +00:00
|
|
|
)
|
|
|
|
|
2013-02-12 15:21:25 +00:00
|
|
|
var defaultPageLimit int = 25
|
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
/* Run as the image server */
|
|
|
|
func runServer(ip, port string) {
|
|
|
|
var addr = fmt.Sprintf("%s:%s", ip, port)
|
|
|
|
|
|
|
|
initMongo()
|
|
|
|
defer mongo_session.Close()
|
|
|
|
|
|
|
|
http.HandleFunc("/", routeRoot)
|
2013-07-03 15:21:56 +00:00
|
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httplog.DefaultFavIcon.ServeHTTP(w, r)
|
|
|
|
})
|
2013-03-27 20:30:24 +00:00
|
|
|
http.HandleFunc("/assets/", routeAssets)
|
|
|
|
http.HandleFunc("/upload", routeUpload)
|
|
|
|
http.HandleFunc("/urlie", routeGetFromUrl)
|
|
|
|
http.HandleFunc("/all", routeAll)
|
|
|
|
http.HandleFunc("/f/", routeFiles)
|
2013-05-08 18:28:49 +00:00
|
|
|
http.HandleFunc("/v/", routeViews)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.HandleFunc("/k/", routeKeywords)
|
|
|
|
http.HandleFunc("/ip/", routeIPs)
|
|
|
|
http.HandleFunc("/ext/", routeExt)
|
|
|
|
http.HandleFunc("/md5/", routeMD5s)
|
|
|
|
|
|
|
|
log.Printf("Serving on %s ...", addr)
|
|
|
|
log.Fatal(http.ListenAndServe(addr, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func initMongo() {
|
|
|
|
mongo_session, err := mgo.Dial(MongoHost)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
images_db = mongo_session.DB(MongoDB)
|
|
|
|
if len(MongoUsername) > 0 && len(MongoPassword) > 0 {
|
|
|
|
err = images_db.Login(MongoUsername, MongoPassword)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfs = images_db.GridFS("fs")
|
|
|
|
}
|
|
|
|
|
2013-02-09 04:52:40 +00:00
|
|
|
func serverErr(w http.ResponseWriter, r *http.Request, e error) {
|
2013-03-27 20:30:24 +00:00
|
|
|
log.Printf("Error: %s", e)
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 503)
|
2013-03-27 20:30:24 +00:00
|
|
|
fmt.Fprintf(w, "Error: %s", e)
|
|
|
|
http.Error(w, "Service Unavailable", 503)
|
|
|
|
return
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 22:15:06 +00:00
|
|
|
/* return a <a href/> for a given filename
|
2013-02-09 04:52:40 +00:00
|
|
|
and root is the relavtive base of the explicit link.
|
|
|
|
*/
|
|
|
|
func linkToFile(root string, filename string) (html string) {
|
2013-03-27 20:30:24 +00:00
|
|
|
return fmt.Sprintf("<a href='%s/f/%s'>%s</a>",
|
|
|
|
root,
|
|
|
|
filename,
|
|
|
|
filename)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the sections of the URI Path.
|
|
|
|
This will disregard the leading '/'
|
|
|
|
*/
|
|
|
|
func chunkURI(uri string) (chunks []string) {
|
2013-03-27 20:30:24 +00:00
|
|
|
var str string
|
|
|
|
if uri[0] == '/' {
|
|
|
|
str = uri[1:]
|
|
|
|
} else {
|
|
|
|
str = uri
|
|
|
|
}
|
|
|
|
return strings.Split(str, "/")
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 18:28:49 +00:00
|
|
|
func routeViewsGET(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if len(uriChunks) > 2 {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-05-08 18:28:49 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
2013-05-09 16:13:46 +00:00
|
|
|
var file types.File
|
2013-05-09 13:36:21 +00:00
|
|
|
err := gfs.Find(bson.M{"filename": uriChunks[1]}).One(&file)
|
2013-05-08 18:28:49 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:36:21 +00:00
|
|
|
err = ImageViewPage(w, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:28:49 +00:00
|
|
|
} else {
|
|
|
|
// no filename given, show them the full listing
|
|
|
|
http.Redirect(w, r, "/all", 302)
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-05-08 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 04:52:40 +00:00
|
|
|
/*
|
|
|
|
GET /f/
|
|
|
|
GET /f/:name
|
|
|
|
*/
|
|
|
|
// Show a page of most recent images, and tags, and uploaders ...
|
|
|
|
func routeFilesGET(w http.ResponseWriter, r *http.Request) {
|
2013-06-04 20:43:16 +00:00
|
|
|
var err error
|
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if len(uriChunks) > 2 {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:43:16 +00:00
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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") {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-06-04 20:43:16 +00:00
|
|
|
routeFilesDELETE(w, r)
|
|
|
|
return
|
|
|
|
} else if len(r.Form["delete"]) > 0 && r.Form["delete"][0] == "true" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-06-04 20:43:16 +00:00
|
|
|
err = DeleteFilePage(w, uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
if len(uriChunks) == 2 && len(uriChunks[1]) > 0 {
|
|
|
|
log.Printf("Searching for [%s] ...", uriChunks[1])
|
|
|
|
query := gfs.Find(bson.M{"filename": uriChunks[1]})
|
|
|
|
|
|
|
|
c, err := query.Count()
|
|
|
|
// preliminary checks, if they've passed an image name
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Results for [%s] = %d", uriChunks[1], c)
|
|
|
|
if c == 0 {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := filepath.Ext(uriChunks[1])
|
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
|
|
|
w.Header().Set("Cache-Control", "max-age=315360000")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
file, err := gfs.Open(uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(w, file) // send the contents of the file in the body
|
|
|
|
} else {
|
2013-05-08 18:28:49 +00:00
|
|
|
// no filename given, show them the full listing
|
|
|
|
http.Redirect(w, r, "/all", 302)
|
2013-03-27 20:30:24 +00:00
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
POST /f/[:name][?k=v&k=v]
|
|
|
|
*/
|
|
|
|
// Create the file by the name in the path and/or parameter?
|
|
|
|
// add keywords from the parameters
|
|
|
|
// look for an image in the r.Body
|
|
|
|
func routeFilesPOST(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if len(uriChunks) > 2 &&
|
|
|
|
((len(uriChunks) == 2 && len(uriChunks[1]) == 0) &&
|
|
|
|
len(r.URL.RawQuery) == 0) {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 403)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.Error(w, "Not Acceptable", 403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var filename string
|
2013-05-09 16:13:46 +00:00
|
|
|
info := types.Info{
|
2013-03-27 20:30:24 +00:00
|
|
|
Ip: r.RemoteAddr,
|
2013-05-09 13:41:06 +00:00
|
|
|
Random: hash.Rand64(),
|
2013-03-27 20:30:24 +00:00
|
|
|
TimeStamp: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = r.FormValue("filename")
|
|
|
|
if len(filename) == 0 && len(uriChunks) == 2 && len(uriChunks[1]) != 0 {
|
|
|
|
filename = uriChunks[1]
|
|
|
|
}
|
|
|
|
log.Printf("%s\n", filename)
|
|
|
|
|
|
|
|
var p_ext string
|
|
|
|
p_ext = r.FormValue("ext")
|
|
|
|
if len(filename) > 0 && len(p_ext) == 0 {
|
|
|
|
p_ext = filepath.Ext(filename)
|
|
|
|
} else if len(p_ext) > 0 && strings.HasPrefix(p_ext, ".") {
|
|
|
|
p_ext = fmt.Sprintf(".%s", p_ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, word := range []string{
|
|
|
|
"k", "key", "keyword",
|
|
|
|
"keys", "keywords",
|
|
|
|
} {
|
|
|
|
v := r.FormValue(word)
|
|
|
|
if len(v) > 0 {
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
info.Keywords = append(info.Keywords, strings.Split(v, ",")...)
|
|
|
|
} else {
|
|
|
|
info.Keywords = append(info.Keywords, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filename) == 0 {
|
2013-05-09 13:41:06 +00:00
|
|
|
str := hash.GetSmallHash()
|
2013-03-27 20:30:24 +00:00
|
|
|
if len(p_ext) == 0 {
|
|
|
|
filename = fmt.Sprintf("%s.jpg", str)
|
|
|
|
} else {
|
|
|
|
filename = fmt.Sprintf("%s%s", str, p_ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := HasFileByFilename(filename)
|
|
|
|
if err == nil && !exists {
|
|
|
|
file, err := gfs.Create(filename)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file.SetMeta(&info)
|
|
|
|
|
|
|
|
// copy the request body into the gfs file
|
|
|
|
n, err := io.Copy(file, r.Body)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != r.ContentLength {
|
|
|
|
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
|
|
|
filename,
|
|
|
|
r.ContentLength,
|
|
|
|
n)
|
|
|
|
}
|
|
|
|
} else if exists {
|
|
|
|
if r.Method == "PUT" {
|
|
|
|
// TODO nothing will get here presently. Workflow needs more review
|
|
|
|
file, err := gfs.Open(filename)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:13:46 +00:00
|
|
|
var mInfo types.Info
|
2013-03-27 20:30:24 +00:00
|
|
|
err = file.GetMeta(&mInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: failed to get metadata for %s. %s\n", filename, err)
|
|
|
|
}
|
|
|
|
mInfo.Keywords = append(mInfo.Keywords, info.Keywords...)
|
|
|
|
file.SetMeta(&mInfo)
|
|
|
|
} else {
|
|
|
|
log.Printf("[%s] already exists", filename)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Accept"), "text/html") {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeFilesPUT(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
// update the file by the name in the path and/or parameter?
|
|
|
|
// update/add keywords from the parameters
|
|
|
|
// look for an image in the r.Body
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 418)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeFilesDELETE(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
2013-06-04 20:43:16 +00:00
|
|
|
if (len(uriChunks) > 2) || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 400)
|
2013-06-04 20:43:16 +00:00
|
|
|
http.Error(w, "Bad Syntax", 400)
|
2013-03-27 20:30:24 +00:00
|
|
|
return
|
|
|
|
}
|
2013-06-04 20:43:16 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
exists, err := HasFileByFilename(uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-06-04 20:43:16 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
if exists {
|
|
|
|
err = gfs.Remove(uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 302)
|
2013-06-04 20:43:16 +00:00
|
|
|
http.Redirect(w, r, "/", 302)
|
2013-03-27 20:30:24 +00:00
|
|
|
} else {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
// delete the name in the path and/or parameter?
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-05-08 18:28:49 +00:00
|
|
|
func routeViews(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
|
|
|
routeViewsGET(w, r)
|
|
|
|
default:
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-05-08 18:28:49 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 04:52:40 +00:00
|
|
|
func routeFiles(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
|
|
|
routeFilesGET(w, r)
|
|
|
|
case r.Method == "PUT":
|
|
|
|
routeFilesPUT(w, r)
|
|
|
|
case r.Method == "POST":
|
|
|
|
routeFilesPOST(w, r)
|
|
|
|
case r.Method == "DELETE":
|
|
|
|
routeFilesDELETE(w, r)
|
|
|
|
default:
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeRoot(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method != "GET" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Show a page of most recent images, and tags, and uploaders ...
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
//iter := gfs.Find(bson.M{"uploadDate": bson.M{"$gt": time.Now().Add(-time.Hour)}}).Limit(defaultPageLimit).Iter()
|
2013-05-09 16:13:46 +00:00
|
|
|
var files []types.File
|
2013-03-27 20:30:24 +00:00
|
|
|
err := gfs.Find(nil).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:36:21 +00:00
|
|
|
err = ListFilesPage(w, files)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func routeAll(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method != "GET" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
|
|
|
// Show a page of all the images
|
2013-05-09 16:13:46 +00:00
|
|
|
var files []types.File
|
2013-05-09 16:53:28 +00:00
|
|
|
err := gfs.Find(nil).Sort("-metadata.timestamp").All(&files)
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:36:21 +00:00
|
|
|
err = ListFilesPage(w, files)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
GET /k/
|
|
|
|
GET /k/:name
|
|
|
|
GET /k/:name/r
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-02-09 04:52:40 +00:00
|
|
|
Show a page of all the keyword tags, and then the images
|
|
|
|
If /k/:name/r then show a random image by keyword name
|
|
|
|
Otherwise 404
|
|
|
|
*/
|
|
|
|
func routeKeywords(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if r.Method != "GET" ||
|
|
|
|
len(uriChunks) > 3 ||
|
|
|
|
(len(uriChunks) == 3 && uriChunks[2] != "r") {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
} else if len(uriChunks) == 1 || (len(uriChunks) == 2 && len(uriChunks[1]) == 0) {
|
2013-08-06 00:29:16 +00:00
|
|
|
// Path: /k/
|
|
|
|
// show a tag cloud!
|
|
|
|
kc, err := GetKeywords()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = ListKeywordsPage(w, kc)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("K: %s (%d)", uriChunks, len(uriChunks))
|
|
|
|
|
|
|
|
var iter *mgo.Iter
|
|
|
|
if uriChunks[len(uriChunks)-1] == "r" {
|
2013-08-06 00:29:16 +00:00
|
|
|
// Path: /k/
|
2013-03-27 20:30:24 +00:00
|
|
|
// TODO determine how to show a random image by keyword ...
|
|
|
|
log.Println("random isn't built yet")
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
return
|
|
|
|
} else if len(uriChunks) == 2 {
|
2013-08-06 00:29:16 +00:00
|
|
|
// Path: /k/:name
|
2013-03-27 20:30:24 +00:00
|
|
|
log.Println(uriChunks[1])
|
|
|
|
iter = gfs.Find(bson.M{"metadata.keywords": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).Iter()
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:13:46 +00:00
|
|
|
var files []types.File
|
2013-03-27 20:30:24 +00:00
|
|
|
err := iter.All(&files)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println(len(files))
|
2013-05-09 13:36:21 +00:00
|
|
|
err = ListFilesPage(w, files)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 15:21:25 +00:00
|
|
|
func routeMD5s(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if r.Method != "GET" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
} else if len(uriChunks) != 2 {
|
|
|
|
// they didn't give an MD5, re-route
|
|
|
|
routeRoot(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-09 16:13:46 +00:00
|
|
|
var files []types.File
|
2013-03-27 20:30:24 +00:00
|
|
|
err := gfs.Find(bson.M{"md5": uriChunks[1]}).Sort("-metadata.timestamp").Limit(defaultPageLimit).All(&files)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:36:21 +00:00
|
|
|
err = ListFilesPage(w, files)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 15:21:25 +00:00
|
|
|
// Show a page of file extensions, and allow paging by ext
|
2013-02-09 04:52:40 +00:00
|
|
|
func routeExt(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method != "GET" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-02-09 04:52:40 +00:00
|
|
|
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show a page of all the uploader's IPs, and the images
|
|
|
|
func routeIPs(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method != "GET" {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-02-09 04:52:40 +00:00
|
|
|
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 16:04:48 +00:00
|
|
|
/*
|
|
|
|
GET /urlie
|
|
|
|
POST /urlie
|
|
|
|
*/
|
|
|
|
func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
|
2013-05-09 14:01:13 +00:00
|
|
|
if r.Method == "GET" {
|
|
|
|
err := UrliePage(w)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200)
|
2013-05-09 14:01:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method == "POST" {
|
2013-05-09 14:01:13 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
stored_filename string
|
|
|
|
local_filename string
|
|
|
|
useRandName bool = false
|
2013-05-09 16:13:46 +00:00
|
|
|
info types.Info
|
2013-05-09 14:01:13 +00:00
|
|
|
)
|
|
|
|
|
2013-05-09 16:13:46 +00:00
|
|
|
info = types.Info{
|
2013-03-27 20:30:24 +00:00
|
|
|
Ip: r.RemoteAddr,
|
2013-05-09 13:41:06 +00:00
|
|
|
Random: hash.Rand64(),
|
2013-03-27 20:30:24 +00:00
|
|
|
TimeStamp: time.Now(),
|
|
|
|
}
|
|
|
|
log.Println(info)
|
|
|
|
|
2013-05-09 14:01:13 +00:00
|
|
|
err = r.ParseMultipartForm(1024 * 5)
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 14:01:13 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
log.Printf("%q", r.MultipartForm.Value)
|
|
|
|
for k, v := range r.MultipartForm.Value {
|
|
|
|
if k == "keywords" {
|
|
|
|
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
|
|
|
} else if k == "url" {
|
2013-05-10 19:03:54 +00:00
|
|
|
local_filename, err = util.FetchFileFromURL(v[0])
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
} else if len(local_filename) == 0 {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Yay, hopefully we got an image!
|
2013-05-09 14:01:13 +00:00
|
|
|
} else if k == "rand" {
|
|
|
|
useRandName = true
|
2013-03-27 20:30:24 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 14:01:13 +00:00
|
|
|
exists, err := HasFileByFilename(filepath.Base(local_filename))
|
2013-05-09 13:36:21 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-05-09 14:01:13 +00:00
|
|
|
if exists || useRandName {
|
|
|
|
ext := filepath.Ext(local_filename)
|
|
|
|
str := hash.GetSmallHash()
|
|
|
|
stored_filename = fmt.Sprintf("%s%s", str, ext)
|
|
|
|
} else {
|
|
|
|
stored_filename = filepath.Base(local_filename)
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-05-09 14:01:13 +00:00
|
|
|
file, err := gfs.Create(stored_filename)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-05-09 14:01:13 +00:00
|
|
|
local_fh, err := os.Open(local_filename)
|
|
|
|
defer local_fh.Close()
|
|
|
|
if err != nil {
|
2013-03-27 20:30:24 +00:00
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 14:01:13 +00:00
|
|
|
|
|
|
|
file.SetMeta(&info)
|
|
|
|
|
|
|
|
// copy the request body into the gfs file
|
|
|
|
n, err := io.Copy(file, local_fh)
|
2013-05-09 13:36:21 +00:00
|
|
|
if err != nil {
|
2013-05-09 14:01:13 +00:00
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
2013-05-09 13:36:21 +00:00
|
|
|
}
|
2013-05-09 14:01:13 +00:00
|
|
|
log.Printf("Wrote [%d] bytes from %s to %s", n, local_filename, stored_filename)
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", stored_filename), 302)
|
2013-03-27 20:30:24 +00:00
|
|
|
} else {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
2013-02-12 16:04:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 15:21:25 +00:00
|
|
|
/*
|
|
|
|
GET /upload
|
|
|
|
POST /upload
|
|
|
|
*/
|
2013-02-11 19:27:38 +00:00
|
|
|
func routeUpload(w http.ResponseWriter, r *http.Request) {
|
2013-05-09 13:36:21 +00:00
|
|
|
if r.Method == "GET" {
|
|
|
|
// Show the upload form
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
2013-05-09 13:36:21 +00:00
|
|
|
err := UploadPage(w)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
if r.Method == "POST" {
|
2013-05-09 16:13:46 +00:00
|
|
|
info := types.Info{
|
2013-03-27 20:30:24 +00:00
|
|
|
Ip: r.RemoteAddr,
|
2013-05-09 13:41:06 +00:00
|
|
|
Random: hash.Rand64(),
|
2013-03-27 20:30:24 +00:00
|
|
|
TimeStamp: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle the form posting to this route
|
|
|
|
err := r.ParseMultipartForm(1024 * 5)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:41:06 +00:00
|
|
|
useRandName := false
|
2013-03-27 20:30:24 +00:00
|
|
|
log.Printf("%q", r.MultipartForm.Value)
|
|
|
|
for k, v := range r.MultipartForm.Value {
|
|
|
|
if k == "keywords" {
|
|
|
|
info.Keywords = append(info.Keywords, strings.Split(v[0], ",")...)
|
2013-05-09 13:36:21 +00:00
|
|
|
} else if k == "rand" {
|
2013-05-09 13:41:06 +00:00
|
|
|
useRandName = true
|
2013-03-27 20:30:24 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("WARN: not sure what to do with param [%s = %s]", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filehdr := r.MultipartForm.File["filename"][0]
|
2013-05-09 13:36:21 +00:00
|
|
|
filename := filehdr.Filename
|
|
|
|
exists, err := HasFileByFilename(filename)
|
2013-03-27 20:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
2013-05-09 13:36:21 +00:00
|
|
|
}
|
2013-05-09 13:41:06 +00:00
|
|
|
if exists || useRandName {
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
str := hash.GetSmallHash()
|
|
|
|
filename = fmt.Sprintf("%s%s", str, ext)
|
2013-05-09 13:36:21 +00:00
|
|
|
}
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-05-09 13:36:21 +00:00
|
|
|
file, err := gfs.Create(filename)
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file.SetMeta(&info)
|
2013-03-27 20:30:24 +00:00
|
|
|
|
2013-05-09 13:36:21 +00:00
|
|
|
multiFile, err := filehdr.Open()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := io.Copy(file, multiFile)
|
|
|
|
if err != nil {
|
2013-03-27 20:30:24 +00:00
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 13:36:21 +00:00
|
|
|
if n != r.ContentLength {
|
|
|
|
log.Printf("WARNING: [%s] content-length (%d), content written (%d)",
|
|
|
|
filename,
|
|
|
|
r.ContentLength,
|
|
|
|
n)
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/v/%s", filename), 302)
|
2013-03-27 20:30:24 +00:00
|
|
|
} else {
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 404)
|
2013-03-27 20:30:24 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-06-18 19:16:52 +00:00
|
|
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
2013-02-11 19:27:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 15:21:25 +00:00
|
|
|
func routeAssets(w http.ResponseWriter, r *http.Request) {
|
2013-03-27 20:30:24 +00:00
|
|
|
path, err := filepath.Rel("/assets", r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-05 23:27:07 +00:00
|
|
|
w.Header().Set("Cache-Control", "max-age=315360000, public")
|
|
|
|
w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).UTC().Format(time.RFC1123))
|
2013-08-05 22:44:38 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
switch path {
|
|
|
|
case "bootstrap.css":
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
2013-08-05 23:27:07 +00:00
|
|
|
fmt.Fprintf(w, "%s", assets.BootstrapCss())
|
2013-08-05 22:44:38 +00:00
|
|
|
case "bootstrap.js":
|
2013-08-05 23:27:07 +00:00
|
|
|
w.Header().Set("Content-Type", "text/javascript")
|
2013-08-05 22:44:38 +00:00
|
|
|
fmt.Fprintf(w, "%s", assets.BootstrapJs())
|
|
|
|
case "jquery.js":
|
2013-08-05 23:27:07 +00:00
|
|
|
w.Header().Set("Content-Type", "text/javascript")
|
2013-08-05 22:44:38 +00:00
|
|
|
fmt.Fprintf(w, "%s", assets.JqueryJs())
|
|
|
|
case "jqud.js":
|
2013-08-05 23:27:07 +00:00
|
|
|
w.Header().Set("Content-Type", "text/javascript")
|
2013-08-05 22:44:38 +00:00
|
|
|
fmt.Fprintf(w, "%s", assets.TagCloudJs())
|
2013-08-05 23:27:07 +00:00
|
|
|
default:
|
|
|
|
httplog.LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2013-03-27 20:30:24 +00:00
|
|
|
}
|
2013-08-05 23:27:07 +00:00
|
|
|
httplog.LogRequest(r, 200) // if we make it this far, then log success
|
2013-02-12 15:21:25 +00:00
|
|
|
}
|