Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4747039a0 | ||
|
|
3c5f23d3ac | ||
|
|
c6109e23af | ||
|
|
6ae09962ad | ||
|
|
17e3e21e56 | ||
|
|
334b64ca6d | ||
|
|
9dc505af17 |
61
main.go
61
main.go
@@ -99,6 +99,7 @@ func endpoints(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
fmt.Fprintln(w, err.Error())
|
fmt.Fprintln(w, err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
setContentTypeIfMedia(w, r)
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,7 +146,7 @@ func withDel(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
match = bytes.Split(match, []byte(`href="`))[1]
|
match = bytes.Split(match, []byte(`href="`))[1]
|
||||||
match = match[:len(match)-1]
|
match = match[:len(match)-1]
|
||||||
b[i] = []byte(fmt.Sprintf(`<a href="%s/%s"><input type="button" value="❌" style="padding: .40em 1em .10em 1em; margin-right: .5em"/></a> %s`, match, ENDPOINT_DELETE, b[i]))
|
b[i] = []byte(fmt.Sprintf(`<a href="%s/%s"><input type="button" value="❌" style="padding: .40em 1em .10em 1em; margin-right: .5em" onclick='return confirm("Delete "+%q+"?");'></input></a> %s`, match, ENDPOINT_DELETE, match, b[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buff.Write(b[i])
|
buff.Write(b[i])
|
||||||
@@ -189,7 +190,7 @@ func del(w http.ResponseWriter, r *http.Request) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = os.Remove(p)
|
err = os.RemoveAll(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -201,3 +202,59 @@ func toRealPath(p string) string {
|
|||||||
d := path.Join(fs.Get("d").GetString())
|
d := path.Join(fs.Get("d").GetString())
|
||||||
return path.Join(d, p)
|
return path.Join(d, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ext := strings.ToLower(path.Ext(r.URL.Path))
|
||||||
|
if i := strings.LastIndex(ext, "."); i != -1 {
|
||||||
|
ext = ext[i:]
|
||||||
|
}
|
||||||
|
k := "Content-Type"
|
||||||
|
v := ""
|
||||||
|
switch ext {
|
||||||
|
case ".mp4":
|
||||||
|
v = "video/mp4"
|
||||||
|
case ".mkv":
|
||||||
|
v = "video/x-matroska"
|
||||||
|
case ".mp3":
|
||||||
|
v = "audio/mpeg3"
|
||||||
|
case ".epub", ".mobi":
|
||||||
|
k = "Content-Disposition"
|
||||||
|
v = "attachment"
|
||||||
|
case ".jpg", ".jpeg":
|
||||||
|
v = "image/jpeg"
|
||||||
|
case ".gif":
|
||||||
|
v = "image/gif"
|
||||||
|
case ".png":
|
||||||
|
v = "image/png"
|
||||||
|
case ".ico":
|
||||||
|
v = "image/x-icon"
|
||||||
|
case ".svg":
|
||||||
|
v = "image/svg+xml"
|
||||||
|
case ".css":
|
||||||
|
v = "text/css"
|
||||||
|
case ".js":
|
||||||
|
v = "text/javascript"
|
||||||
|
case ".json":
|
||||||
|
v = "application/json"
|
||||||
|
case ".html", ".htm":
|
||||||
|
v = "text/html"
|
||||||
|
case ".pdf":
|
||||||
|
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", path.Base(r.URL.Path)))
|
||||||
|
v = "application/pdf"
|
||||||
|
case ".webm":
|
||||||
|
v = "video/webm"
|
||||||
|
case ".weba":
|
||||||
|
v = "audio/webm"
|
||||||
|
case ".webp":
|
||||||
|
v = "image/webp"
|
||||||
|
case ".zip":
|
||||||
|
v = "application/zip"
|
||||||
|
case ".7z":
|
||||||
|
v = "application/x-7z-compressed"
|
||||||
|
case ".tar":
|
||||||
|
v = "application/x-tar"
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
|||||||
34
main_test.go
Normal file
34
main_test.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetContentType(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cases := map[string]struct {
|
||||||
|
path string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
"css with multi .": {
|
||||||
|
path: "/static/css/main.2145ce41.chunk.css",
|
||||||
|
want: "text/css",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, d := range cases {
|
||||||
|
c := d
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
r := &http.Request{URL: &url.URL{Path: c.path}}
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
setContentTypeIfMedia(w, r)
|
||||||
|
if ct := w.Header().Get("Content-Type"); ct != c.want {
|
||||||
|
t.Errorf("wrong content type: want %q, got %q", c.want, ct)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
0
public/DIR2/e.md
Normal file → Executable file
0
public/DIR2/e.md
Normal file → Executable file
@@ -1 +0,0 @@
|
|||||||
hi
|
|
||||||
0
public/b.md
Normal file → Executable file
0
public/b.md
Normal file → Executable file
Reference in New Issue
Block a user