fix(assets): when file is zipped, upload zipped file instead of unzipped file

This commit is contained in:
Sebastian Beisch
2020-03-25 08:45:27 +01:00
parent 322455b6d4
commit be35b743b4
5 changed files with 45 additions and 22 deletions

View File

@@ -7,6 +7,7 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"hash/crc32"
"io"
@@ -24,6 +25,7 @@ import (
type Asset struct {
name string
path string
zippedPath string
algorithm string
isCompressed bool
}
@@ -63,8 +65,12 @@ func NewAsset(repository string, assetConfig config.Asset, algorithm string) (*A
}
func (a *Asset) getChecksum() (string, error) {
log.Debugf("Calculating checksum for %s", a.path)
file, err := os.Open(a.path)
path, err := a.GetPath()
if err != nil {
return "", nil
}
log.Debugf("Calculating checksum for %s", path)
file, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "Failed to open file %s to calculate checksum", a.name)
}
@@ -98,13 +104,16 @@ func (a *Asset) getChecksum() (string, error) {
// GetPath where the file is located, if zipped true, it will compress it and give you the new location
func (a *Asset) GetPath() (string, error) {
if a.isCompressed {
return a.zipFile()
return a.ZipFile()
}
return a.path, nil
}
// GetName of asset
func (a *Asset) GetName() string {
if a.isCompressed {
return fmt.Sprintf("%s.zip", a.name)
}
return a.name
}
@@ -114,7 +123,11 @@ func (a *Asset) IsCompressed() bool {
}
// ZipFile compress given file in zip format
func (a *Asset) zipFile() (string, error) {
func (a *Asset) ZipFile() (string, error) {
if a.zippedPath != "" {
return a.zippedPath, nil
}
path := a.path
fileToZip, err := os.Open(path)
@@ -156,5 +169,6 @@ func (a *Asset) zipFile() (string, error) {
if err := zipFile.Close(); err != nil {
return "", errors.Wrap(err, "Could not close file")
}
return filepath.Abs(fileToZipInfo.Name())
a.zippedPath, err = filepath.Abs(zipFile.Name())
return a.zippedPath, err
}

View File

@@ -13,7 +13,7 @@ import (
// Set struct
type Set struct {
Assets []*Asset
assets []*Asset
repository string
algorithm string
}
@@ -21,7 +21,7 @@ type Set struct {
//New container for assets
func New(repository, algorithm string) *Set {
return &Set{
Assets: []*Asset{},
assets: []*Asset{},
repository: repository,
algorithm: algorithm,
}
@@ -34,13 +34,13 @@ func (s *Set) Add(assetConfigs ...config.Asset) error {
if err != nil {
return err
}
s.Assets = append(s.Assets, asset)
s.assets = append(s.assets, asset)
}
return nil
}
func (s *Set) All() []*Asset {
return s.Assets
return s.assets
}
func (s *Set) GenerateChecksum() error {
@@ -50,7 +50,7 @@ func (s *Set) GenerateChecksum() error {
}
defer checksumFile.Close()
lines := []string{}
for _, asset := range s.Assets {
for _, asset := range s.assets {
checksum, err := asset.getChecksum()
if err != nil {
return err
@@ -68,7 +68,7 @@ func (s *Set) GenerateChecksum() error {
return err
}
s.Assets = append(s.Assets, &Asset{
s.assets = append(s.assets, &Asset{
path: filePath,
name: "checksum.txt",
isCompressed: false,