add metadata extension

This commit is contained in:
Acconut 2015-02-03 19:01:35 +01:00
parent 99ca34e1ce
commit 74a9a408e6
2 changed files with 48 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package tusd package tusd
import ( import (
"encoding/base64"
"errors" "errors"
"io" "io"
"log" "log"
@ -8,6 +9,7 @@ import (
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
"strings"
"github.com/bmizerany/pat" "github.com/bmizerany/pat"
) )
@ -127,7 +129,7 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
header.Set("TUS-Version", "1.0.0") header.Set("TUS-Version", "1.0.0")
header.Set("TUS-Extension", "file-creation") header.Set("TUS-Extension", "file-creation,metadata")
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return return
@ -158,8 +160,8 @@ func (handler *Handler) postFile(w http.ResponseWriter, r *http.Request) {
return return
} }
// @TODO: implement metadata extension // Parse metadata
meta := make(map[string]string) meta := parseMeta(r.Header.Get("Metadata"))
id, err := handler.dataStore.NewUpload(size, meta) id, err := handler.dataStore.NewUpload(size, meta)
if err != nil { if err != nil {
@ -285,3 +287,31 @@ func (handler *Handler) absFileUrl(r *http.Request, id string) string {
return url return url
} }
// Parse the meatadata as defined in the Metadata extension.
// e.g. Metadata: key base64value, key2 base64value
func parseMeta(header string) map[string]string {
meta := make(map[string]string)
for _, element := range strings.Split(header, ",") {
element := strings.TrimSpace(element)
parts := strings.Split(element, " ")
// Do not continue with this element if no key and value or presented
if len(parts) != 2 {
continue
}
// Ignore corrent element if the value is no valid base64
key := parts[0]
value, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
continue
}
meta[key] = string(value)
}
return meta
}

View File

@ -80,7 +80,7 @@ func TestProtocolDiscovery(t *testing.T) {
} }
headers := map[string]string{ headers := map[string]string{
"TUS-Extension": "file-creation", "TUS-Extension": "file-creation,metadata",
"TUS-Version": "1.0.0", "TUS-Version": "1.0.0",
"TUS-Resumable": "1.0.0", "TUS-Resumable": "1.0.0",
"TUS-Max-Size": "400", "TUS-Max-Size": "400",
@ -110,6 +110,19 @@ func (s postStore) NewUpload(size int64, metaData MetaData) (string, error) {
if size != 300 { if size != 300 {
s.t.Errorf("Expected size to be 300 (got %v)", size) s.t.Errorf("Expected size to be 300 (got %v)", size)
} }
if len(metaData) != 2 {
s.t.Errorf("Expected two elements in metadata")
}
if v := metaData["foo"]; v != "hello" {
s.t.Errorf("Expected foo element to be 'hello' but got %s", v)
}
if v := metaData["bar"]; v != "world" {
s.t.Errorf("Expected bar element to be 'world' but got %s", v)
}
return "foo", nil return "foo", nil
} }
@ -126,6 +139,7 @@ func TestFileCreation(t *testing.T) {
req, _ := http.NewRequest("POST", "", nil) req, _ := http.NewRequest("POST", "", nil)
req.Header.Set("TUS-Resumable", "1.0.0") req.Header.Set("TUS-Resumable", "1.0.0")
req.Header.Set("Entity-Length", "300") req.Header.Set("Entity-Length", "300")
req.Header.Set("Metadata", "foo aGVsbG8=, bar d29ybGQ=")
req.Host = "tus.io" req.Host = "tus.io"
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler.ServeHTTP(w, req) handler.ServeHTTP(w, req)