From 81e4d4b2d79e09008b6828ca352714a5b5bc824f Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 6 Mar 2017 21:59:53 +0100 Subject: [PATCH] Move builders to own file Signed-off-by: Knut Ahlers --- .gitignore | 1 + assets.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 95 ------------------------------------------------ 3 files changed, 105 insertions(+), 95 deletions(-) create mode 100644 .gitignore create mode 100644 assets.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7840f2d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tex-api diff --git a/assets.go b/assets.go new file mode 100644 index 0000000..049ba05 --- /dev/null +++ b/assets.go @@ -0,0 +1,104 @@ +package main + +import ( + "archive/tar" + "archive/zip" + "bytes" + "io" + "os" + "path" + "path/filepath" + "strings" + + "github.com/Luzifer/go_helpers/str" + uuid "github.com/satori/go.uuid" +) + +func shouldPackFile(extension string) bool { + return str.StringInSlice(extension, []string{ + ".log", + ".pdf", + }) +} + +func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) { + buf := new(bytes.Buffer) + w := zip.NewWriter(buf) + + basePath := pathFromUUID(uid, filenameOutputDir) + err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !shouldPackFile(path.Ext(info.Name())) { + return nil + } + + zipInfo, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + zipInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") + zipFile, err := w.CreateHeader(zipInfo) + if err != nil { + return err + } + osFile, err := os.Open(p) + if err != nil { + return err + } + + io.Copy(zipFile, osFile) + osFile.Close() + + return nil + }) + + if err != nil { + return nil, err + } + + return buf, w.Close() +} + +func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) { + buf := new(bytes.Buffer) + w := tar.NewWriter(buf) + + basePath := pathFromUUID(uid, filenameOutputDir) + err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !shouldPackFile(path.Ext(info.Name())) { + return nil + } + + tarInfo, err := tar.FileInfoHeader(info, "") + if err != nil { + return err + } + tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") + err = w.WriteHeader(tarInfo) + if err != nil { + return err + } + osFile, err := os.Open(p) + if err != nil { + return err + } + + io.Copy(w, osFile) + osFile.Close() + + return nil + }) + + if err != nil { + return nil, err + } + + return buf, w.Close() +} diff --git a/main.go b/main.go index 60b7dc7..0db67cc 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main import ( - "archive/tar" - "archive/zip" - "bytes" "encoding/json" "fmt" "io" @@ -13,12 +10,9 @@ import ( "os" "os/exec" "path" - "path/filepath" "strconv" - "strings" "time" - "github.com/Luzifer/go_helpers/str" "github.com/Luzifer/rconfig" log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" @@ -234,95 +228,6 @@ func waitForJob(res http.ResponseWriter, r *http.Request) { } } -func shouldPackFile(extension string) bool { - return str.StringInSlice(extension, []string{ - ".log", - ".pdf", - }) -} - -func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) { - buf := new(bytes.Buffer) - w := zip.NewWriter(buf) - - basePath := pathFromUUID(uid, filenameOutputDir) - err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !shouldPackFile(path.Ext(info.Name())) { - return nil - } - - zipInfo, err := zip.FileInfoHeader(info) - if err != nil { - return err - } - zipInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") - zipFile, err := w.CreateHeader(zipInfo) - if err != nil { - return err - } - osFile, err := os.Open(p) - if err != nil { - return err - } - - io.Copy(zipFile, osFile) - osFile.Close() - - return nil - }) - - if err != nil { - return nil, err - } - - return buf, w.Close() -} - -func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) { - buf := new(bytes.Buffer) - w := tar.NewWriter(buf) - - basePath := pathFromUUID(uid, filenameOutputDir) - err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !shouldPackFile(path.Ext(info.Name())) { - return nil - } - - tarInfo, err := tar.FileInfoHeader(info, "") - if err != nil { - return err - } - tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\") - err = w.WriteHeader(tarInfo) - if err != nil { - return err - } - osFile, err := os.Open(p) - if err != nil { - return err - } - - io.Copy(w, osFile) - osFile.Close() - - return nil - }) - - if err != nil { - return nil, err - } - - return buf, w.Close() -} - func downloadAssets(res http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) uid, err := uuid.FromString(vars["uid"])