mirror of
https://github.com/pldubouilh/gossa.git
synced 2026-09-02 23:27:39 -04:00
archive download
This commit is contained in:
committed by
Pierre Dubouilh
parent
dd6cb90e9d
commit
c1eeb9820a
+33
-1
@@ -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)
|
||||
|
||||
+22
-1
@@ -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"]}`)
|
||||
|
||||
Reference in New Issue
Block a user