2013-02-09 04:52:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-03-27 20:30:24 +00:00
|
|
|
"fmt"
|
2013-05-09 13:41:06 +00:00
|
|
|
"github.com/vbatts/imgsrv/hash"
|
2013-05-09 16:13:46 +00:00
|
|
|
"github.com/vbatts/imgsrv/types"
|
2013-03-27 20:30:24 +00:00
|
|
|
"io"
|
|
|
|
"labix.org/v2/mgo"
|
|
|
|
"labix.org/v2/mgo/bson"
|
|
|
|
"log"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
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)
|
|
|
|
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)
|
|
|
|
LogRequest(r, 503)
|
|
|
|
fmt.Fprintf(w, "Error: %s", e)
|
|
|
|
http.Error(w, "Service Unavailable", 503)
|
|
|
|
return
|
2013-02-09 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return a <a href/> for a given filename
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* kindof a common log type output */
|
|
|
|
func LogRequest(r *http.Request, statusCode int) {
|
2013-03-27 20:30:24 +00:00
|
|
|
var addr string
|
|
|
|
var user_agent string
|
|
|
|
|
|
|
|
user_agent = ""
|
|
|
|
addr = r.RemoteAddr
|
|
|
|
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if k == "User-Agent" {
|
|
|
|
user_agent = strings.Join(v, " ")
|
|
|
|
}
|
|
|
|
if k == "X-Forwarded-For" {
|
|
|
|
addr = strings.Join(v, " ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s - - [%s] \"%s %s\" \"%s\" %d %d\n",
|
|
|
|
addr,
|
|
|
|
time.Now(),
|
|
|
|
r.Method,
|
|
|
|
r.URL.Path,
|
|
|
|
user_agent,
|
|
|
|
statusCode,
|
|
|
|
r.ContentLength)
|
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 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
LogRequest(r, 200)
|
|
|
|
}
|
|
|
|
|
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-03-27 20:30:24 +00:00
|
|
|
uriChunks := chunkURI(r.URL.Path)
|
|
|
|
if len(uriChunks) > 2 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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
|
|
|
}
|
|
|
|
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) {
|
|
|
|
LogRequest(r, 403)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
LogRequest(r, 200)
|
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)
|
|
|
|
if len(uriChunks) > 2 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
} else if len(uriChunks) == 2 && len(uriChunks[1]) == 0 {
|
|
|
|
}
|
|
|
|
exists, err := HasFileByFilename(uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
err = gfs.Remove(uriChunks[1])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
LogRequest(r, 200)
|
|
|
|
} else {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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:
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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:
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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" {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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-03-27 20:30:24 +00:00
|
|
|
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" {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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-03-27 20:30:24 +00:00
|
|
|
err := gfs.Find(nil).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
|
|
|
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") {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
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))
|
|
|
|
|
|
|
|
var iter *mgo.Iter
|
|
|
|
if uriChunks[len(uriChunks)-1] == "r" {
|
|
|
|
// TODO determine how to show a random image by keyword ...
|
|
|
|
log.Println("random isn't built yet")
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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" {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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" {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-02-09 04:52:40 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
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" {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-02-09 04:52:40 +00:00
|
|
|
|
2013-03-27 20:30:24 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
LogRequest(r, 200)
|
|
|
|
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" {
|
|
|
|
local_filename, err = FetchFileFromURL(v[0])
|
|
|
|
if err != nil {
|
|
|
|
serverErr(w, r, err)
|
|
|
|
return
|
|
|
|
} else if len(local_filename) == 0 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
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 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2013-05-09 14:01:13 +00:00
|
|
|
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
|
|
|
|
LogRequest(r, 200) // if we make it this far, then log success
|
|
|
|
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 {
|
|
|
|
LogRequest(r, 404)
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch path {
|
|
|
|
case "bootstrap.css":
|
|
|
|
fmt.Fprint(w, bootstrapCSS)
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
}
|
2013-02-12 15:21:25 +00:00
|
|
|
}
|