Basic file init

This commit is contained in:
Felix Geisendörfer 2013-03-18 11:06:33 +01:00
parent 17a8d892bf
commit 33478ede43
2 changed files with 21 additions and 8 deletions

View File

@ -29,5 +29,17 @@ func logPath(fileId string) string {
return dataPath(fileId)+".log" return dataPath(fileId)+".log"
} }
func initDataFile(id string, size int) { func initFile(fileId string, size int64, contentType string) error {
d := dataPath(fileId)
file, err := os.OpenFile(d, os.O_CREATE | os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
if err := file.Truncate(size); err != nil {
return err
}
return nil
} }

View File

@ -7,12 +7,6 @@ import (
"regexp" "regexp"
) )
// temporary, to provide a mock server for the HTML5 client while the actual
// storage is still under development here.
const (
exampleId = "0bc88096498be52d7909bcec815d37b3"
)
var fileRoute = regexp.MustCompile("^/files/([^/]+)$") var fileRoute = regexp.MustCompile("^/files/([^/]+)$")
func serveHttp() error { func serveHttp() error {
@ -32,6 +26,7 @@ func route(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" && r.URL.Path == "/files" { if r.Method == "POST" && r.URL.Path == "/files" {
createFile(w, r) createFile(w, r)
} else if match := fileRoute.FindStringSubmatch(r.URL.Path); match != nil { } else if match := fileRoute.FindStringSubmatch(r.URL.Path); match != nil {
// WIP
switch r.Method { switch r.Method {
case "HEAD": case "HEAD":
w.Header().Set("X-Resume", "bytes=0-99") w.Header().Set("X-Resume", "bytes=0-99")
@ -74,6 +69,12 @@ func createFile(w http.ResponseWriter, r *http.Request) {
contentType = "application/octet-stream" contentType = "application/octet-stream"
} }
w.Header().Set("Location", "/files/"+exampleId) id := uid()
if err := initFile(id, contentRange.Size, contentType); err != nil {
reply(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Location", "/files/"+id)
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
} }