1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-12-27 00:06:29 +00:00

ERMEHGERD: upload and from url both work, and redirect straight to the

new image
This commit is contained in:
Vincent Batts 2013-02-12 11:04:48 -05:00
parent e6d93f8541
commit e7ec9528d0
2 changed files with 131 additions and 2 deletions

View file

@ -64,10 +64,36 @@ var tailTemplateHTML = `
</html> </html>
` `
var formGetUrlTemplate = template.Must(template.New("formGetUrl").Parse(formGetUrlTemplateHTML))
var formGetUrlTemplateHTML = `
<div class="span9">
<div class="hero-unit">
<h3>Get file from URL</h3>
<form enctype="multipart/form-data" action="/urlie" method="POST">
<table>
<tr>
<td>
<input type="text" name="url" placeholder="file URL"><br/>
<input type="text" name="keywords" placeholder="keywords"><i>(comma seperatated, no spaces)</i>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Fetch File"><br/>
</td>
</tr>
</td>
</table>
</form>
</div>{{/* hero-unit */}}
</div>{{/* span9 */}}
`
var formFileUploadTemplate = template.Must(template.New("formFileUpload").Parse(formFileUploadTemplateHTML)) var formFileUploadTemplate = template.Must(template.New("formFileUpload").Parse(formFileUploadTemplateHTML))
var formFileUploadTemplateHTML = ` var formFileUploadTemplateHTML = `
<div class="span9"> <div class="span9">
<div class="hero-unit"> <div class="hero-unit">
<h3>Upload a File</h3>
<form enctype="multipart/form-data" action="/upload" method="POST"> <form enctype="multipart/form-data" action="/upload" method="POST">
<table> <table>
<tr> <tr>
@ -101,6 +127,29 @@ var listTemplateHTML = `
{{end}} {{end}}
` `
func UrliePage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: URLie"})
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
}
err = formGetUrlTemplate.Execute(w, &emptyInterface)
if (err != nil) {
return err
}
err = tailTemplate.Execute(w, map[string]string{"footer" : ""})
if (err != nil) {
return err
}
return
}
func UploadPage(w io.Writer) (err error) { func UploadPage(w io.Writer) (err error) {
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"}) err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"})
if (err != nil) { if (err != nil) {

View file

@ -8,6 +8,7 @@ import (
"log" "log"
"mime" "mime"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -424,6 +425,85 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
LogRequest(r,200) LogRequest(r,200)
} }
/*
GET /urlie
POST /urlie
*/
func routeGetFromUrl(w http.ResponseWriter, r *http.Request) {
if (r.Method == "POST") {
info := Info{
Ip: r.RemoteAddr,
Random: Rand64(),
TimeStamp: time.Now(),
}
log.Println(info)
err := r.ParseMultipartForm(1024*5)
if (err != nil) {
serverErr(w,r,err)
return
}
log.Printf("%q", r.MultipartForm.Value)
var local_filename string
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!
} else {
log.Printf("WARN: not sure what to do with param [%s = %s]", k,v)
}
}
exists, err := HasFileByFilename(local_filename)
if (err == nil && !exists) {
file, err := gfs.Create(filepath.Base(local_filename))
defer file.Close()
if (err != nil) {
serverErr(w,r,err)
return
}
local_fh, err := os.Open(local_filename)
defer local_fh.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, local_fh)
if (err != nil) {
serverErr(w,r,err)
return
}
log.Printf("Wrote [%d] bytes from %s", n, local_filename)
http.Redirect(w,r,fmt.Sprintf("/f/%s", filepath.Base(local_filename)), 302)
} else {
serverErr(w,r,err)
return
}
} else if (r.Method == "GET") {
UrliePage(w)
} else {
LogRequest(r,404)
http.NotFound(w,r)
return
}
LogRequest(r,200)
}
/* /*
GET /upload GET /upload
POST /upload POST /upload
@ -482,8 +562,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
n) n)
} }
io.WriteString(w, http.Redirect(w,r,fmt.Sprintf("/f/%s", filehdr.Filename), 302)
fmt.Sprintf("<a href=\"/f/%s\">/f/%s</a>\n", filehdr.Filename, filehdr.Filename))
} else if (exists) { } else if (exists) {
// print some message about the file already existing // print some message about the file already existing
} else { } else {
@ -540,6 +619,7 @@ func runServer(ip, port string) {
http.HandleFunc("/", routeRoot) http.HandleFunc("/", routeRoot)
http.HandleFunc("/assets/", routeAssets) http.HandleFunc("/assets/", routeAssets)
http.HandleFunc("/upload", routeUpload) http.HandleFunc("/upload", routeUpload)
http.HandleFunc("/urlie", routeGetFromUrl)
http.HandleFunc("/all", routeAll) http.HandleFunc("/all", routeAll)
http.HandleFunc("/f/", routeFiles) http.HandleFunc("/f/", routeFiles)
http.HandleFunc("/k/", routeKeywords) http.HandleFunc("/k/", routeKeywords)