json response + imported old drag and drop uploader

This commit is contained in:
andreimarcu 2015-09-25 00:58:38 -04:00
parent 350338baa4
commit 8caae56b39
5 changed files with 1946 additions and 17 deletions

View File

@ -15,15 +15,18 @@ var Config struct {
bind string bind string
filesDir string filesDir string
siteName string siteName string
siteURL string
} }
func main() { func main() {
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080", flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
"host to bind to (default: 127.0.0.1:8080)") "host to bind to (default: 127.0.0.1:8080)")
flag.StringVar(&Config.filesDir, "d", "files/", flag.StringVar(&Config.filesDir, "filespath", "files/",
"path to files directory (default: files/)") "path to files directory (default: files/)")
flag.StringVar(&Config.siteName, "n", "linx", flag.StringVar(&Config.siteName, "sitename", "linx",
"name of the site") "name of the site")
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
"site base url (including trailing slash)")
flag.Parse() flag.Parse()
// Disable template caching -- keep until out of pre-alpha // Disable template caching -- keep until out of pre-alpha

View File

@ -352,7 +352,7 @@ body {
} }
.qq-upload-file {} .qq-upload-file {}
.qq-upload-spinner {display:inline-block; background: url("/images/loading.gif"); width:15px; height:15px; vertical-align:text-bottom;} .qq-upload-spinner {display:inline-block; width:15px; height:15px; vertical-align:text-bottom;}
.qq-upload-size,.qq-upload-cancel {font-size:11px;} .qq-upload-size,.qq-upload-cancel {font-size:11px;}
.qq-upload-failed-text {display:none;} .qq-upload-failed-text {display:none;}

1894
static/js/cat.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -8,12 +8,11 @@
<div class="clear"></div> <div class="clear"></div>
<form action="/upload" method="POST" enctype="multipart/form-data"> <form action="/upload" method="POST" enctype="multipart/form-data">
<div id="file-uploader" style="min-width: 400px;"> <div id="file-uploader" style="min-width: 400px;">
<br /> <br />
<input type="file" name="file" id="file_upload" name="file"><br/ ><br/ > <input type="file" name="file" id="file_upload" name="file"><br/ ><br/ >
<input id ="upload_btn" type="submit" value="Upload"> <input id ="upload_btn" type="submit" value="Upload">
<br /><br /> <br /><br />
</div> </div>
<div id="choices"> <div id="choices">
@ -42,4 +41,18 @@
</div> </div>
</div> </div>
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "/static/js/cat.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>
{% endblock %} {% endblock %}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -30,15 +31,21 @@ type Upload struct {
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) { func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq := UploadRequest{} upReq := UploadRequest{}
file, headers, err := r.FormFile("file") if r.Header.Get("Content-Type") == "application/octet-stream" {
if err != nil { defer r.Body.Close()
fmt.Fprintf(w, err.Error()) upReq.src = r.Body
return upReq.filename = r.URL.Query().Get("qqfile")
}
defer file.Close()
upReq.src = file } else {
upReq.filename = headers.Filename file, headers, err := r.FormFile("file")
if err != nil {
return
}
defer file.Close()
upReq.src = file
upReq.filename = headers.Filename
}
upload, err := processUpload(upReq) upload, err := processUpload(upReq)
if err != nil { if err != nil {
@ -46,7 +53,19 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
return return
} }
http.Redirect(w, r, "/"+upload.Filename, 301) if strings.EqualFold("application/json", r.Header.Get("Accept")) {
js, _ := json.Marshal(map[string]string{
"filename": upload.Filename,
"url": Config.siteURL + upload.Filename,
})
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(js)
} else {
http.Redirect(w, r, "/"+upload.Filename, 301)
}
} }
func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) { func uploadPutHandler(c web.C, w http.ResponseWriter, r *http.Request) {