diff --git a/gossa-ui b/gossa-ui index a88fc12..26b93fd 160000 --- a/gossa-ui +++ b/gossa-ui @@ -1 +1 @@ -Subproject commit a88fc12cf53249238cf45dc382be0851cf7261df +Subproject commit 26b93fd4bd969143105c23bc1bc644eeba000e13 diff --git a/readme.md b/readme.md index 21d8240..18bc0df 100644 --- a/readme.md +++ b/readme.md @@ -8,24 +8,26 @@ gossa [![docker pulls](https://img.shields.io/docker/pulls/pldubouilh/gossa.svg?logo=docker)](https://hub.docker.com/r/pldubouilh/gossa) [![github downloads](https://img.shields.io/github/downloads/pldubouilh/gossa/total.svg?logo=github)](https://github.com/pldubouilh/gossa/releases) -a fast and simple webserver for your files, that's dependency-free and with under 200 lines of code, easy to review. +a fast and simple webserver for your files, that's dependency-free and with under 250 lines of code, easy to review. a [simple UI](https://github.com/pldubouilh/gossa-ui) comes as default, featuring : - * πŸ” files/directories browser - * πŸ“© drag-and-drop file/directory uploader - * πŸš€ lightweight, default ui weights 110kB and prints in ms - * πŸ—ΊοΈ files handling - move/rename/delete + * πŸ” files/directories browser & handler + * πŸ“© drag-and-drop uploader + * πŸš€ lightweight and dependency free + * πŸ’Ύ 90s web UI that prints in ms * πŸ“Έ picture browser * πŸ“½οΈ video streaming * ✍️ simple text editor - * ⌨️ keyboard shortcuts - * πŸ₯‚ fast golang static server, easily fills available bandwidth - * πŸ”’ easy/secure multi account setup + * ⌨️ keyboard navigation + * πŸ₯‚ fast golang static server + * πŸ”’ easy/secure multi account setup, read-only mode ### build built blobs are available on the [release page](https://github.com/pldubouilh/gossa/releases) - or simply `make build` this repo. +arch linux users can also install through the [user repos](https://aur.archlinux.org/packages/gossa/) - e.g. `yay -S gossa` + ### usage ```sh % ./gossa --help @@ -33,6 +35,9 @@ built blobs are available on the [release page](https://github.com/pldubouilh/go % ./gossa -h 192.168.100.33 ~/storage ``` +### shortcuts +press `Ctrl/Cmd + h` to see all the UI/keyboard shortcuts. + ### fancier setups release images are pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gossa), e.g. : @@ -43,6 +48,4 @@ release images are pushed to [dockerhub](https://hub.docker.com/r/pldubouilh/gos in a do-one-thing-well mindset, HTTPS and authentication has been left to middlewares and proxies. [sample caddy configs](https://github.com/pldubouilh/gossa/blob/master/support/) are available to quickly setup multi users setups along with https. -### shortcuts -the default UI is fully usable by through keyboard/UI shortcuts - press `Ctrl/Cmd + h` to see them all. diff --git a/src/gossa.go b/src/gossa.go index 0d8e379..a4d35fb 100755 --- a/src/gossa.go +++ b/src/gossa.go @@ -1,6 +1,7 @@ package main import ( + "archive/zip" "encoding/json" "errors" "flag" @@ -147,12 +148,41 @@ func upload(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok")) } +func walkZip(wz *zip.Writer, fp, baseInZip string) { + files, err := ioutil.ReadDir(fp) + check(err) + + for _, file := range files { + if !file.IsDir() { + data, err := ioutil.ReadFile(fp + file.Name()) + check(err) + f, err := wz.Create(baseInZip + file.Name()) + check(err) + _, err = f.Write(data) + check(err) + } else if file.IsDir() { + newBase := fp + file.Name() + "/" + walkZip(wz, newBase, baseInZip+file.Name()+"/") + } + } +} + +func zipRPC(w http.ResponseWriter, r *http.Request) { + zipPath := r.URL.Query().Get("zipPath") + zipName := r.URL.Query().Get("zipName") + defer exitPath(w, "zip", zipPath) + wz := zip.NewWriter(w) + w.Header().Add("Content-Disposition", "attachment; filename=\""+zipName+".zip\"") + walkZip(wz, checkPath(zipPath)+"/", "") + wz.Close() +} + func rpc(w http.ResponseWriter, r *http.Request) { var err error var rpc rpcCall + defer exitPath(w, "rpc", rpc) bodyBytes, _ := ioutil.ReadAll(r.Body) json.Unmarshal(bodyBytes, &rpc) - defer exitPath(w, "rpc", rpc) if rpc.Call == "mkdirp" { err = os.MkdirAll(checkPath(rpc.Args[0]), os.ModePerm) @@ -197,6 +227,8 @@ func main() { http.HandleFunc(*extraPath+"rpc", rpc) http.HandleFunc(*extraPath+"post", upload) } + + http.HandleFunc(*extraPath+"zip", zipRPC) http.HandleFunc("/", doContent) fs = http.StripPrefix(*extraPath, http.FileServer(http.Dir(initPath))) fmt.Printf("Gossa startig on directory %s\nListening on http://%s:%s%s\n", initPath, *host, *port, *extraPath) diff --git a/src/gossa_test.go b/src/gossa_test.go index d3e8e0b..f5c23a9 100644 --- a/src/gossa_test.go +++ b/src/gossa_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "crypto/sha256" "fmt" "io/ioutil" "net/http" @@ -24,11 +25,16 @@ func trimSpaces(str string) string { return space.ReplaceAllString(str, " ") } -func get(t *testing.T, url string) string { +func getRaw(t *testing.T, url string) []byte { resp, err := http.Get(url) dieMaybe(t, err) body, err := ioutil.ReadAll(resp.Body) dieMaybe(t, err) + return body +} + +func get(t *testing.T, url string) string { + body := getRaw(t, url) return trimSpaces(string(body)) } @@ -115,6 +121,21 @@ func doTestRegular(t *testing.T, url string, testExtra bool) { fetchAndTestDefault(t, url+path) // extra path will just redirect to root dir } + // ~~~~~~~~~~~~~~~~~ + fmt.Println("\r\n~~~~~~~~~~ test zip") + bodyRaw := getRaw(t, url+"zip?zipPath=%2F%E4%B8%AD%E6%96%87%2F&zipName=%E4%B8%AD%E6%96%87") + hashStr := fmt.Sprintf("%x", sha256.Sum256(bodyRaw)) + if hashStr != "b02436a76b149e6c4458bbbe622ab7c5e789bb0d26b87f604cf0f989cfaf669f" { + t.Fatal("invalid zip checksum", hashStr) + } + + // ~~~~~~~~~~~~~~~~~ + fmt.Println("\r\n~~~~~~~~~~ test zip invalid path") + body0 = get(t, url+"zip?zipPath=%2Ftmp&zipName=subdir") + if body0 == `ok` { + t.Fatal("zip passed for invalid path") + } + // ~~~~~~~~~~~~~~~~~ fmt.Println("\r\n~~~~~~~~~~ test mkdir rpc") body0 = postJSON(t, url+"rpc", `{"call":"mkdirp","args":["/AAA"]}`)