2017-03-06 20:59:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-06 16:45:48 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
2023-09-06 10:28:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-09-06 16:30:36 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-17 09:54:14 +00:00
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
2017-03-06 20:59:53 +00:00
|
|
|
)
|
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
func buildAssetsTAR(uid uuid.UUID) (io.Reader, error) {
|
2017-03-06 20:59:53 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2023-09-06 10:28:03 +00:00
|
|
|
w := tar.NewWriter(buf)
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
tarInfo, err := tar.FileInfoHeader(info, "")
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "creating tar entry")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
2023-09-06 10:28:03 +00:00
|
|
|
tarInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\")
|
|
|
|
err = w.WriteHeader(tarInfo)
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "writing tar header")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
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 {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "opening source file")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
2023-09-06 16:30:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := osFile.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing output file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2017-03-06 20:59:53 +00:00
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
if _, err := io.Copy(w, osFile); err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "copying source file")
|
2018-09-17 09:45:26 +00:00
|
|
|
}
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return nil, errors.Wrap(err, "walking source dir")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
return buf, errors.Wrap(w.Close(), "closing tar file")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
func buildAssetsZIP(uid uuid.UUID) (io.Reader, error) {
|
2017-03-06 20:59:53 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2023-09-06 10:28:03 +00:00
|
|
|
w := zip.NewWriter(buf)
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
zipInfo, err := zip.FileInfoHeader(info)
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "creating zip header")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
2023-09-06 10:28:03 +00:00
|
|
|
zipInfo.Name = strings.TrimLeft(strings.Replace(p, basePath, "", 1), "/\\")
|
|
|
|
zipFile, err := w.CreateHeader(zipInfo)
|
2017-03-06 20:59:53 +00:00
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "writing zip header")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
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 {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "opening source file")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
2023-09-06 16:30:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := osFile.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing output file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2017-03-06 20:59:53 +00:00
|
|
|
|
2023-09-06 10:28:03 +00:00
|
|
|
if _, err := io.Copy(zipFile, osFile); err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return errors.Wrap(err, "copying source file")
|
2018-09-17 09:45:26 +00:00
|
|
|
}
|
2017-03-06 20:59:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-06 12:27:36 +00:00
|
|
|
return nil, errors.Wrap(err, "walking source dir")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
return buf, errors.Wrap(w.Close(), "closing zip file")
|
2017-03-06 20:59:53 +00:00
|
|
|
}
|
2023-09-06 10:28:03 +00:00
|
|
|
|
|
|
|
func getAssetsPDF(uid uuid.UUID) (io.Reader, error) {
|
|
|
|
var (
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
found bool
|
|
|
|
)
|
|
|
|
|
|
|
|
basePath := pathFromUUID(uid, filenameOutputDir)
|
|
|
|
err := filepath.Walk(basePath, func(p string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Ext(info.Name()) != ".pdf" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
osFile, err := os.Open(p) // #nosec G304
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "opening file")
|
|
|
|
}
|
2023-09-06 16:30:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := osFile.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing output pdf file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2023-09-06 10:28:03 +00:00
|
|
|
|
|
|
|
if _, err := io.Copy(buf, osFile); err != nil {
|
|
|
|
return errors.Wrap(err, "reading file")
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
return filepath.SkipAll
|
|
|
|
})
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
// We found no file
|
|
|
|
return nil, errors.New("no pdf found")
|
|
|
|
}
|
|
|
|
|
2023-09-06 12:27:36 +00:00
|
|
|
return buf, errors.Wrap(err, "walking source dir")
|
2023-09-06 10:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func shouldPackFile(extension string) bool {
|
|
|
|
return str.StringInSlice(extension, []string{
|
|
|
|
".log",
|
|
|
|
".pdf",
|
|
|
|
})
|
|
|
|
}
|