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
filesDir string
siteName string
siteURL string
}
func main() {
flag.StringVar(&Config.bind, "b", "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/)")
flag.StringVar(&Config.siteName, "n", "linx",
flag.StringVar(&Config.siteName, "sitename", "linx",
"name of the site")
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
"site base url (including trailing slash)")
flag.Parse()
// Disable template caching -- keep until out of pre-alpha

View File

@ -352,7 +352,7 @@ body {
}
.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-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>
<form action="/upload" method="POST" enctype="multipart/form-data">
<div id="file-uploader" style="min-width: 400px;">
<br />
<input type="file" name="file" id="file_upload" name="file"><br/ ><br/ >
<input id ="upload_btn" type="submit" value="Upload">
<br /><br />
<br />
<input type="file" name="file" id="file_upload" name="file"><br/ ><br/ >
<input id ="upload_btn" type="submit" value="Upload">
<br /><br />
</div>
<div id="choices">
@ -42,4 +41,18 @@
</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 %}

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
@ -30,15 +31,21 @@ type Upload struct {
func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
upReq := UploadRequest{}
file, headers, err := r.FormFile("file")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
defer file.Close()
if r.Header.Get("Content-Type") == "application/octet-stream" {
defer r.Body.Close()
upReq.src = r.Body
upReq.filename = r.URL.Query().Get("qqfile")
upReq.src = file
upReq.filename = headers.Filename
} else {
file, headers, err := r.FormFile("file")
if err != nil {
return
}
defer file.Close()
upReq.src = file
upReq.filename = headers.Filename
}
upload, err := processUpload(upReq)
if err != nil {
@ -46,7 +53,19 @@ func uploadPostHandler(c web.C, w http.ResponseWriter, r *http.Request) {
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) {