2017-03-06 20:59:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Luzifer/go_helpers/str"
|
2018-09-17 09:54:14 +00:00
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
2017-03-06 20:59:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-09-17 09:45:26 +00:00
|
|
|
osFile, err := os.Open(p) // #nosec G304
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:45:26 +00:00
|
|
|
if _, err := io.Copy(zipFile, osFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
osFile.Close() // #nosec G104
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-09-17 09:45:26 +00:00
|
|
|
osFile, err := os.Open(p) // #nosec G304
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:45:26 +00:00
|
|
|
if _, err := io.Copy(w, osFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
osFile.Close() // #nosec G104
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, w.Close()
|
|
|
|
}
|