add more headers, routes and also allow POSTING to /files/<id> to add compatibility with jQuery File Upload

This commit is contained in:
tim-kos 2013-03-19 16:36:48 +01:00
parent dab46b60c9
commit 83d367fd11
1 changed files with 11 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import (
) )
var fileRoute = regexp.MustCompile("^/files/([^/]+)$") var fileRoute = regexp.MustCompile("^/files/([^/]+)$")
var filesRoute = regexp.MustCompile("^/files/?$")
var dataStore *DataStore var dataStore *DataStore
func init() { func init() {
@ -41,11 +42,16 @@ func route(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "tusd") w.Header().Set("Server", "tusd")
w.Header().Add("Access-Control-Allow-Origin", "*") w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Origin, x-requested-with, content-type, accept, Content-Range, Content-Disposition")
w.Header().Add("Access-Control-Expose-Headers", "Location, Range")
if r.Method == "POST" && r.URL.Path == "/files" { if r.Method == "OPTIONS" {
reply(w, http.StatusOK, "")
return
}
if r.Method == "POST" && filesRoute.Match([]byte(r.URL.Path)) {
postFiles(w, r) postFiles(w, r)
} else if r.Method == "OPTIONS" && r.URL.Path == "/files" {
reply(w, http.StatusOK, "Cool")
} else if match := fileRoute.FindStringSubmatch(r.URL.Path); match != nil { } else if match := fileRoute.FindStringSubmatch(r.URL.Path); match != nil {
id := match[1] id := match[1]
switch r.Method { switch r.Method {
@ -53,6 +59,8 @@ func route(w http.ResponseWriter, r *http.Request) {
headFile(w, r, id) headFile(w, r, id)
case "GET": case "GET":
getFile(w, r, id) getFile(w, r, id)
case "POST":
putFile(w, r, id)
case "PUT": case "PUT":
putFile(w, r, id) putFile(w, r, id)
default: default: