diff --git a/handlers.go b/handlers.go index 5e3f47f..f606413 100644 --- a/handlers.go +++ b/handlers.go @@ -130,6 +130,34 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { io.CopyBuffer(w, f, make([]byte, 4096)) } +func HandleStream(w http.ResponseWriter, r *http.Request) { + filename := filepath.Base(r.FormValue("filename")) + + if filename == "" { + http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest) + return + } + + f, err := os.Open(filepath.Join(config.StoragePath, filename)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + defer f.Close() + + fileStat, err := f.Stat() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(filename))) + w.Header().Set("Accept-Ranges", "none") + w.Header().Set("Content-Length", strconv.Itoa(int(fileStat.Size()))) + + io.CopyBuffer(w, f, make([]byte, 4096)) +} + func HandleIcon(w http.ResponseWriter, r *http.Request) { ext := r.FormValue("ext") diff --git a/main.go b/main.go index 3cc5831..536ae2f 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ func init() { http.HandleFunc("/icon", HandleIcon) http.HandleFunc("/upload", HandleUpload) http.HandleFunc("/download", HandleDownload) + http.HandleFunc("/stream", HandleStream) } func main() { diff --git a/site/style.css b/site/style.css index 48a84bf..3fdfc13 100644 --- a/site/style.css +++ b/site/style.css @@ -126,6 +126,11 @@ input[type="file"] { display: none; } +.right { + float: right; + color: #545862; +} + label { border-radius: 0.25em; background-color: #61afef; diff --git a/templates/index.htm b/templates/index.htm index 0717478..e1c18ef 100644 --- a/templates/index.htm +++ b/templates/index.htm @@ -31,7 +31,7 @@
{{.Size}}