mirror of
https://github.com/vbatts/imgsrv.git
synced 2024-12-25 23:36:30 +00:00
ERMEHGERD: upload and from url both work, and redirect straight to the
new image
This commit is contained in:
parent
e6d93f8541
commit
e7ec9528d0
2 changed files with 131 additions and 2 deletions
49
layouts.go
49
layouts.go
|
@ -64,10 +64,36 @@ var tailTemplateHTML = `
|
|||
</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 formFileUploadTemplateHTML = `
|
||||
<div class="span9">
|
||||
<div class="hero-unit">
|
||||
<h3>Upload a File</h3>
|
||||
<form enctype="multipart/form-data" action="/upload" method="POST">
|
||||
<table>
|
||||
<tr>
|
||||
|
@ -101,6 +127,29 @@ var listTemplateHTML = `
|
|||
{{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) {
|
||||
err = headTemplate.Execute(w, map[string]string{"title" : "FileSrv :: Upload"})
|
||||
if (err != nil) {
|
||||
|
|
84
server.go
84
server.go
|
@ -8,6 +8,7 @@ import (
|
|||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -424,6 +425,85 @@ func routeIPs(w http.ResponseWriter, r *http.Request) {
|
|||
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
|
||||
POST /upload
|
||||
|
@ -482,8 +562,7 @@ func routeUpload(w http.ResponseWriter, r *http.Request) {
|
|||
n)
|
||||
}
|
||||
|
||||
io.WriteString(w,
|
||||
fmt.Sprintf("<a href=\"/f/%s\">/f/%s</a>\n", filehdr.Filename, filehdr.Filename))
|
||||
http.Redirect(w,r,fmt.Sprintf("/f/%s", filehdr.Filename), 302)
|
||||
} else if (exists) {
|
||||
// print some message about the file already existing
|
||||
} else {
|
||||
|
@ -540,6 +619,7 @@ func runServer(ip, port string) {
|
|||
http.HandleFunc("/", routeRoot)
|
||||
http.HandleFunc("/assets/", routeAssets)
|
||||
http.HandleFunc("/upload", routeUpload)
|
||||
http.HandleFunc("/urlie", routeGetFromUrl)
|
||||
http.HandleFunc("/all", routeAll)
|
||||
http.HandleFunc("/f/", routeFiles)
|
||||
http.HandleFunc("/k/", routeKeywords)
|
||||
|
|
Loading…
Reference in a new issue