Implement initial file downloading
This commit is contained in:
parent
43bd55001c
commit
44d3e7bc14
|
@ -123,3 +123,18 @@ func getReceivedChunks(fileId string) (chunkSet, error) {
|
||||||
|
|
||||||
return chunks, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -2,9 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fileRoute = regexp.MustCompile("^/files/([^/]+)$")
|
var fileRoute = regexp.MustCompile("^/files/([^/]+)$")
|
||||||
|
@ -27,12 +29,11 @@ func route(w http.ResponseWriter, r *http.Request) {
|
||||||
postFiles(w, r)
|
postFiles(w, r)
|
||||||
} 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]
|
||||||
// WIP
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "HEAD":
|
case "HEAD":
|
||||||
headFile(w, r, id)
|
headFile(w, r, id)
|
||||||
case "GET":
|
case "GET":
|
||||||
reply(w, http.StatusNotImplemented, "File download")
|
getFile(w, r, id)
|
||||||
case "PUT":
|
case "PUT":
|
||||||
putFile(w, r, id)
|
putFile(w, r, id)
|
||||||
default:
|
default:
|
||||||
|
@ -82,6 +83,28 @@ func postFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusCreated)
|
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) {
|
func putFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||||
contentRange, err := parseContentRange(r.Header.Get("Content-Range"))
|
contentRange, err := parseContentRange(r.Header.Get("Content-Range"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -101,10 +124,6 @@ func putFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
||||||
setFileRangeHeader(w, fileId)
|
setFileRangeHeader(w, fileId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func headFile(w http.ResponseWriter, r *http.Request, fileId string) {
|
|
||||||
setFileRangeHeader(w, fileId)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setFileRangeHeader(w http.ResponseWriter, fileId string) {
|
func setFileRangeHeader(w http.ResponseWriter, fileId string) {
|
||||||
chunks, err := getReceivedChunks(fileId)
|
chunks, err := getReceivedChunks(fileId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue