Add inbrowser basic streaming

This commit is contained in:
nxshock 2021-10-10 12:16:37 +05:00
parent 73ba82a4be
commit 233c39d51f
4 changed files with 35 additions and 1 deletions

View File

@ -130,6 +130,34 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
io.CopyBuffer(w, f, make([]byte, 4096)) 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) { func HandleIcon(w http.ResponseWriter, r *http.Request) {
ext := r.FormValue("ext") ext := r.FormValue("ext")

View File

@ -35,6 +35,7 @@ func init() {
http.HandleFunc("/icon", HandleIcon) http.HandleFunc("/icon", HandleIcon)
http.HandleFunc("/upload", HandleUpload) http.HandleFunc("/upload", HandleUpload)
http.HandleFunc("/download", HandleDownload) http.HandleFunc("/download", HandleDownload)
http.HandleFunc("/stream", HandleStream)
} }
func main() { func main() {

View File

@ -126,6 +126,11 @@ input[type="file"] {
display: none; display: none;
} }
.right {
float: right;
color: #545862;
}
label { label {
border-radius: 0.25em; border-radius: 0.25em;
background-color: #61afef; background-color: #61afef;

View File

@ -31,7 +31,7 @@
<th>Дата</th> <th>Дата</th>
</tr> </tr>
{{range .Files}} <tr> {{range .Files}} <tr>
<td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a></td> <td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a> <a class="right" href="/stream?filename={{.Name}}">просмотр</a></td>
<td><pre>{{.Size}}</pre></td> <td><pre>{{.Size}}</pre></td>
<td>{{.Date}}</td> <td>{{.Date}}</td>
</tr> </tr>