added preload status and cancel

This commit is contained in:
ston1th 2022-03-16 01:58:10 +01:00
commit a069baf48d
10 changed files with 153 additions and 35 deletions

View file

@ -8,6 +8,8 @@ import (
"net/http"
"sort"
"strings"
"cachefs/pkg/fs"
)
type statusInterceptor struct {
@ -44,6 +46,7 @@ type dirContents struct {
type file struct {
Name string
Anchor string
Status int
}
type files []file
@ -77,16 +80,21 @@ func (r *responseInterceptor) Status() int {
return r.status
}
func (r *responseInterceptor) GetPaths() (dir dirContents, err error) {
func (r *responseInterceptor) GetPaths(path string, fs *fs.FS) (dir dirContents, err error) {
err = xml.Unmarshal(r.buf.Bytes(), &dir)
if err != nil {
return
}
path = strings.TrimSuffix(path, "/")
for _, p := range dir.AllPaths {
if p[len(p)-1] == '/' {
dir.Dirs = append(dir.Dirs, p)
} else {
dir.Files = append(dir.Files, file{Name: p, Anchor: anchor(p)})
dir.Files = append(dir.Files, file{
Name: p,
Anchor: anchor(p),
Status: fs.CacheStatus(path + "/" + p),
})
}
}
sort.Strings(dir.Dirs)

View file

@ -57,7 +57,8 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, msg, code)
return
}
if r.FormValue("v") == "t" {
option := r.FormValue("o")
if option == "v" {
err = video.Execute(w, r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -66,8 +67,12 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if !d.IsDir() {
if r.FormValue("p") == "t" {
fs.fs.Preload(p)
if option == "p" || option == "sp" {
if option == "p" {
fs.fs.Preload(p)
} else {
fs.fs.CancelPreload(p)
}
rdir := "/"
if i := strings.LastIndex(p, "/"); i > 0 {
rdir = p[:i]
@ -75,7 +80,7 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, rdir+"#"+anchor(p), http.StatusFound)
return
}
if r.FormValue("n") == "t" {
if option == "n" {
fs.fs.NoCache.ServeHTTP(w, r)
return
}
@ -94,7 +99,7 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if offline {
w.Header().Del("Last-Modified")
}
paths, err := i.GetPaths()
paths, err := i.GetPaths(p, fs.fs)
if err == io.EOF {
w.WriteHeader(i.Status())
return

View file

@ -9,9 +9,9 @@ var (
<html>
<head>
<style>
body { text-align: center; padding: 0 150px 0 150px; font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
table { text-align: left; }
body { padding: 0 150px 0 150px; font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; margin: 0 auto; }
table { text-align: left; width: 100%; }
</style>
</head>
<body>
@ -20,16 +20,17 @@ var (
[v]: show video
[n]: skip file caching
[p]: preload file
[s]: stop preloading
</pre>
<table>
<tr><th>Path</th><th>Options</th></tr>
<tr><th style="width:100%;">Path</th><th>Options</th></tr>
<tr><td><a href="../">../</a></td><td></td></tr>
{{range $s := .Dirs -}}
<tr><td><a href="{{$s}}">{{$s}}</a></td><td></td></tr>
{{end -}}
{{range $s := .Files -}}
<tr><td><a id="{{$s.Anchor}}" href="{{$s.Name}}">{{$s.Name}}</a></td>
<td><a href="{{$s.Name}}?v=t">[v]</a>&nbsp;<a href="{{$s.Name}}?n=t">[n]</a>&nbsp;<a href="{{$s.Name}}?p=t">[p]</a></td></tr>
<td><a href="{{$s.Name}}?o=v">[v]</a>&nbsp;<a href="{{$s.Name}}?o=n">[n]</a>&nbsp;<a href="{{$s.Name}}?o=p">[p]</a>&nbsp;<a href="{{$s.Name}}?o=sp">[sp]</a>&nbsp;{{if ge $s.Status 0}}{{$s.Status}}%{{end}}</td></tr>
{{end -}}
</table>
</article>