Implement initial file downloading

This commit is contained in:
Felix Geisendörfer 2013-03-18 17:34:02 +01:00
parent 43bd55001c
commit 44d3e7bc14
2 changed files with 40 additions and 6 deletions

View File

@ -123,3 +123,18 @@ func getReceivedChunks(fileId string) (chunkSet, error) {
return chunks, nil
}
func getFileData(fileId string) (io.ReadCloser, int64, error) {
d := dataPath(fileId)
file, err := os.Open(d)
if err != nil {
return nil, 0, err
}
stat, err := file.Stat()
if err != nil {
return nil, 0, err
}
return file, stat.Size(), nil
}

View File

@ -2,9 +2,11 @@ package main
import (
"fmt"
"io"
"log"
"net/http"
"regexp"
"strconv"
)
var fileRoute = regexp.MustCompile("^/files/([^/]+)$")
@ -27,12 +29,11 @@ func route(w http.ResponseWriter, r *http.Request) {
postFiles(w, r)
} else if match := fileRoute.FindStringSubmatch(r.URL.Path); match != nil {
id := match[1]
// WIP
switch r.Method {
case "HEAD":
headFile(w, r, id)
case "GET":
reply(w, http.StatusNotImplemented, "File download")
getFile(w, r, id)
case "PUT":
putFile(w, r, id)
default:
@ -82,6 +83,28 @@ func postFiles(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
}
func headFile(w http.ResponseWriter, r *http.Request, fileId string) {
setFileRangeHeader(w, fileId)
}
func getFile(w http.ResponseWriter, r *http.Request, fileId string) {
data, size, err := getFileData(fileId)
if err != nil {
// @TODO: Could be a 404 as well
reply(w, http.StatusInternalServerError, err.Error())
return
}
defer data.Close()
setFileRangeHeader(w, fileId)
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
if _, err := io.CopyN(w, data, size); err != nil {
log.Printf("getFile: CopyN failed with: %s", err.Error())
return
}
}
func putFile(w http.ResponseWriter, r *http.Request, fileId string) {
contentRange, err := parseContentRange(r.Header.Get("Content-Range"))
if err != nil {
@ -101,10 +124,6 @@ func putFile(w http.ResponseWriter, r *http.Request, fileId string) {
setFileRangeHeader(w, fileId)
}
func headFile(w http.ResponseWriter, r *http.Request, fileId string) {
setFileRangeHeader(w, fileId)
}
func setFileRangeHeader(w http.ResponseWriter, fileId string) {
chunks, err := getReceivedChunks(fileId)
if err != nil {