mirror of
https://github.com/nxshock/simplefileshare.git
synced 2024-11-28 03:21:00 +05:00
Add inbrowser basic streaming
This commit is contained in:
parent
73ba82a4be
commit
233c39d51f
28
handlers.go
28
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")
|
||||
|
||||
|
1
main.go
1
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() {
|
||||
|
@ -126,6 +126,11 @@ input[type="file"] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.right {
|
||||
float: right;
|
||||
color: #545862;
|
||||
}
|
||||
|
||||
label {
|
||||
border-radius: 0.25em;
|
||||
background-color: #61afef;
|
||||
|
@ -31,7 +31,7 @@
|
||||
<th>Дата</th>
|
||||
</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>{{.Date}}</td>
|
||||
</tr>
|
||||
|
Loading…
Reference in New Issue
Block a user