3 Commits
v0.1 ... v0.4

Author SHA1 Message Date
Bel LaPointe
9dc505af17 Confirm delete 2020-03-23 15:55:36 -06:00
Bel LaPointe
0c36391bb2 Create test files and add delete button 2020-03-23 15:50:49 -06:00
bel
7c67df0f1d Add uploadability 2020-03-23 03:20:02 +00:00
4 changed files with 160 additions and 5 deletions

164
main.go
View File

@@ -9,15 +9,32 @@ listing file.
package main package main
import ( import (
"bytes"
"errors"
"fmt"
"io"
"local/args" "local/args"
"local/gziphttp" "local/gziphttp"
"log" "log"
"net/http" "net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"strings" "strings"
) )
const (
ENDPOINT_UPLOAD = "__upload__"
ENDPOINT_DELETE = "__delete__"
)
var (
fs *args.ArgSet
)
func main() { func main() {
fs := args.NewArgSet() fs = args.NewArgSet()
fs.Append(args.STRING, "p", "port to serve", "8100") fs.Append(args.STRING, "p", "port to serve", "8100")
fs.Append(args.STRING, "d", "static path to serve", "./public") fs.Append(args.STRING, "d", "static path to serve", "./public")
if err := fs.Parse(); err != nil { if err := fs.Parse(); err != nil {
@@ -34,14 +51,153 @@ func main() {
log.Fatal(http.ListenAndServe(":"+p, nil)) log.Fatal(http.ListenAndServe(":"+p, nil))
} }
func handler(d string) func(http.ResponseWriter, *http.Request) { func handler(d string) http.HandlerFunc {
h := http.FileServer(http.Dir(d)) return gzip(endpoints(withDel(fserve(d))))
}
func writeMeta(w http.ResponseWriter) {
fmt.Fprintf(w, `
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
`)
}
func writeForm(w http.ResponseWriter) {
fmt.Fprintf(w, `
<form enctype="multipart/form-data" action="./%s" method="post">
<input type="file" name="file" required/>
<input type="submit" value="upload"/>
</form>
`, ENDPOINT_UPLOAD)
}
func gzip(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if gziphttp.Can(r) { if gziphttp.Can(r) {
gz := gziphttp.New(w) gz := gziphttp.New(w)
defer gz.Close() defer gz.Close()
w = gz w = gz
} }
h.ServeHTTP(w, r) foo(w, r)
} }
} }
func endpoints(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if isDir(r) {
writeMeta(w)
writeForm(w)
}
if isUploaded(r) {
if err := upload(w, r); err != nil {
fmt.Fprintln(w, err.Error())
}
} else if isDeleted(r) {
if err := del(w, r); err != nil {
fmt.Fprintln(w, err.Error())
}
} else {
foo(w, r)
}
}
}
func isUploaded(r *http.Request) bool {
return path.Base(r.URL.Path) == ENDPOINT_UPLOAD
}
func isDeleted(r *http.Request) bool {
return path.Base(r.URL.Path) == ENDPOINT_DELETE
}
func isDir(r *http.Request) bool {
d := toRealPath(r.URL.Path)
fi, err := os.Stat(d)
if err != nil {
return false
}
if !fi.IsDir() {
return false
}
if _, err := os.Stat(path.Join(d, "index.html")); err == nil {
return false
}
return true
}
func withDel(foo http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !isDir(r) {
foo(w, r)
return
}
fmt.Fprintln(w, `<a href=".."><input type="button" style="padding: .15em 4em .35em 4em" value=".."/></a>`+"\n")
w2 := httptest.NewRecorder()
foo(w2, r)
b := bytes.Split(w2.Body.Bytes(), []byte("\n"))
buff := bytes.NewBuffer(nil)
for i := range b {
if bytes.Contains(b[i], []byte("<a href=")) {
re := regexp.MustCompile(`href="[^"]*"`)
match := re.Find(b[i])
if len(match) > 0 {
match = bytes.Split(match, []byte(`href="`))[1]
match = match[:len(match)-1]
b[i] = []byte(fmt.Sprintf(`<a href="%s/%s"><input type="button" value="&#10060;" 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([]byte("\n"))
}
io.Copy(w, buff)
}
}
func fserve(d string) http.HandlerFunc {
h := http.FileServer(http.Dir(d))
return h.ServeHTTP
}
func upload(w http.ResponseWriter, r *http.Request) error {
r.ParseMultipartForm(100 << 20)
file, handler, err := r.FormFile("file")
if err != nil {
return err
}
defer file.Close()
p := toRealPath(path.Join(path.Dir(r.URL.Path), handler.Filename))
if fi, err := os.Stat(p); err == nil && !fi.IsDir() {
return errors.New("already exists")
}
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, file); err != nil {
return err
}
http.Redirect(w, r, path.Dir(r.URL.Path)+"/", http.StatusSeeOther)
return nil
}
func del(w http.ResponseWriter, r *http.Request) error {
p := toRealPath(path.Dir(r.URL.Path))
_, err := os.Stat(p)
if err != nil {
return err
}
err = os.Remove(p)
if err != nil {
return err
}
http.Redirect(w, r, path.Dir(path.Dir(r.URL.Path))+"/", http.StatusSeeOther)
return nil
}
func toRealPath(p string) string {
d := path.Join(fs.Get("d").GetString())
return path.Join(d, p)
}

0
public/DIR2/e.md Normal file
View File

View File

@@ -1 +0,0 @@
hi

0
public/b.md Normal file
View File