Browse Source

json response + imported old drag and drop uploader

pull/4/head
andreimarcu 9 years ago
parent
commit
8caae56b39
  1. 7
      server.go
  2. 2
      static/css/linx.css
  3. 1894
      static/js/cat.js
  4. 15
      templates/index.html
  5. 21
      upload.go

7
server.go

@ -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

2
static/css/linx.css

@ -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
File diff suppressed because it is too large
View File

15
templates/index.html

@ -8,7 +8,6 @@
<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/ >
@ -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 %}

21
upload.go

@ -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{}
if r.Header.Get("Content-Type") == "application/octet-stream" {
defer r.Body.Close()
upReq.src = r.Body
upReq.filename = r.URL.Query().Get("qqfile")
} else {
file, headers, err := r.FormFile("file") file, headers, err := r.FormFile("file")
if err != nil { if err != nil {
fmt.Fprintf(w, err.Error())
return return
} }
defer file.Close() defer file.Close()
upReq.src = file upReq.src = file
upReq.filename = headers.Filename 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
} }
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) 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) {

Loading…
Cancel
Save