flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 3384eb4f1e94daa548dadcd5d074d59edc0c7eb0
parent 744622624fd556c99fd8914824f2f0a6cba849ae
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 31 Oct 2020 10:23:07 -0700

Add archive handler

Diffstat:
Mhttp.go | 18++++++++++++++++++
Mtemplates/my_site.html | 2++
Mutils.go | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/http.go b/http.go @@ -213,6 +213,23 @@ func mySiteHandler(w http.ResponseWriter, r *http.Request) { _ = t.ExecuteTemplate(w, "my_site.html", data) } +func archiveHandler(w http.ResponseWriter, r *http.Request) { + authd, authUser, _ := getAuthUser(r) + if !authd { + renderError(w, "403: Forbidden", 403) + return + } + if r.Method == "GET" { + userFolder := filepath.Join(c.FilesDirectory, filepath.Clean(authUser)) + err := zipit(userFolder, w) + if err != nil { + log.Println(err) + renderError(w, InternalServerErrorMsg, 500) + return + } + + } +} func loginHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { // show page @@ -450,6 +467,7 @@ func runHTTPServer() { serveMux.HandleFunc(hostname+"/", rootHandler) serveMux.HandleFunc(hostname+"/my_site", mySiteHandler) + serveMux.HandleFunc(hostname+"/my_site/flounder-archive.zip", archiveHandler) serveMux.HandleFunc(hostname+"/admin", adminHandler) serveMux.HandleFunc(hostname+"/edit/", editFileHandler) serveMux.HandleFunc(hostname+"/upload", uploadFilesHandler) diff --git a/templates/my_site.html b/templates/my_site.html @@ -52,4 +52,6 @@ <input type="file" id="myFile" name="file" multiple /> <input type="submit" value="Upload file" class="button" /> </form> +<br> +<p><a href="/my_site/flounder-archive.zip">Download site archive</a></p> {{template "footer" .}} diff --git a/utils.go b/utils.go @@ -1,8 +1,12 @@ package main import ( + "archive/zip" "fmt" + "io" + "os" "path" + "path/filepath" "strings" "time" ) @@ -41,3 +45,59 @@ func checkIfValidFile(filename string, fileBytes []byte) error { } return nil } + +func zipit(source string, target io.Writer) error { + archive := zip.NewWriter(target) + + info, err := os.Stat(source) + if err != nil { + return nil + } + + var baseDir string + if info.IsDir() { + baseDir = filepath.Base(source) + } + + filepath.Walk(source, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + if baseDir != "" { + header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source)) + } + + if info.IsDir() { + header.Name += "/" + } else { + header.Method = zip.Deflate + } + + writer, err := archive.CreateHeader(header) + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + _, err = io.Copy(writer, file) + return err + }) + + archive.Close() + + return err +}