|
| 1 | +package assets |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "crypto/md5" |
| 6 | + "crypto/sha1" |
| 7 | + "crypto/sha256" |
| 8 | + "crypto/sha512" |
| 9 | + "encoding/hex" |
| 10 | + "hash" |
| 11 | + "hash/crc32" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
| 14 | + "os" |
| 15 | + "path" |
| 16 | + "path/filepath" |
| 17 | + |
| 18 | + "github.com/Nightapes/go-semantic-release/pkg/config" |
| 19 | + "github.com/pkg/errors" |
| 20 | + log "github.com/sirupsen/logrus" |
| 21 | +) |
| 22 | + |
| 23 | +// Asset struct |
| 24 | +type Asset struct { |
| 25 | + name string |
| 26 | + path string |
| 27 | + algorithm string |
| 28 | + isCompressed bool |
| 29 | +} |
| 30 | + |
| 31 | +//NewAsset from a config |
| 32 | +func NewAsset(repository string, assetConfig config.Asset, algorithm string) (*Asset, error) { |
| 33 | + |
| 34 | + filePath := assetConfig.Path |
| 35 | + if assetConfig.Name != "" && assetConfig.Path == "" { |
| 36 | + filePath = assetConfig.Name |
| 37 | + log.Warn("Name is deprecated. Please update your config. See https://nightapes.github.io/go-semantic-release/") |
| 38 | + } |
| 39 | + |
| 40 | + realPath := path.Join(repository, filePath) |
| 41 | + |
| 42 | + file, err := os.Open(realPath) |
| 43 | + if err != nil { |
| 44 | + file.Close() |
| 45 | + return nil, errors.Wrapf(err, "Could not open file %s", realPath) |
| 46 | + } |
| 47 | + defer file.Close() |
| 48 | + |
| 49 | + name := assetConfig.Rename |
| 50 | + if assetConfig.Rename == "" { |
| 51 | + info, _ := file.Stat() |
| 52 | + name = info.Name() |
| 53 | + } |
| 54 | + |
| 55 | + asset := &Asset{ |
| 56 | + path: realPath, |
| 57 | + name: name, |
| 58 | + isCompressed: assetConfig.Compress, |
| 59 | + algorithm: algorithm, |
| 60 | + } |
| 61 | + |
| 62 | + return asset, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (a *Asset) getChecksum() (string, error) { |
| 66 | + log.Debugf("Calculating checksum for %s", a.path) |
| 67 | + file, err := os.Open(a.path) |
| 68 | + if err != nil { |
| 69 | + return "", errors.Wrapf(err, "Failed to open file %s to calculate checksum", a.name) |
| 70 | + } |
| 71 | + defer file.Close() // nolint: errcheck |
| 72 | + var hash hash.Hash |
| 73 | + switch a.algorithm { |
| 74 | + case "crc32": |
| 75 | + hash = crc32.NewIEEE() |
| 76 | + case "md5": |
| 77 | + hash = md5.New() |
| 78 | + case "sha1": |
| 79 | + hash = sha1.New() |
| 80 | + case "sha224": |
| 81 | + hash = sha256.New224() |
| 82 | + case "sha384": |
| 83 | + hash = sha512.New384() |
| 84 | + case "sha256": |
| 85 | + hash = sha256.New() |
| 86 | + case "sha512": |
| 87 | + hash = sha512.New() |
| 88 | + default: |
| 89 | + hash = sha256.New() |
| 90 | + } |
| 91 | + _, err = io.Copy(hash, file) |
| 92 | + if err != nil { |
| 93 | + return "", err |
| 94 | + } |
| 95 | + return hex.EncodeToString(hash.Sum(nil)), nil |
| 96 | +} |
| 97 | + |
| 98 | +// GetPath where the file is located, if zipped true, it will compress it and give you the new location |
| 99 | +func (a *Asset) GetPath() (string, error) { |
| 100 | + if a.isCompressed { |
| 101 | + return a.zipFile() |
| 102 | + } |
| 103 | + return a.path, nil |
| 104 | +} |
| 105 | + |
| 106 | +// GetName of asset |
| 107 | +func (a *Asset) GetName() string { |
| 108 | + return a.name |
| 109 | +} |
| 110 | + |
| 111 | +// IsCompressed return true if file was zipped |
| 112 | +func (a *Asset) IsCompressed() bool { |
| 113 | + return a.isCompressed |
| 114 | +} |
| 115 | + |
| 116 | +// ZipFile compress given file in zip format |
| 117 | +func (a *Asset) zipFile() (string, error) { |
| 118 | + |
| 119 | + path := a.path |
| 120 | + fileToZip, err := os.Open(path) |
| 121 | + if err != nil { |
| 122 | + return "", errors.Wrapf(err, "Could not open file %s", path) |
| 123 | + } |
| 124 | + defer fileToZip.Close() |
| 125 | + |
| 126 | + zipFile, err := ioutil.TempFile(os.TempDir(), "asset.*.zip") |
| 127 | + |
| 128 | + if err != nil { |
| 129 | + return "", errors.Wrap(err, "Could not generate tmp file") |
| 130 | + } |
| 131 | + log.Debugf("Created zipfile %s", zipFile.Name()) |
| 132 | + |
| 133 | + fileToZipInfo, err := fileToZip.Stat() |
| 134 | + if err != nil { |
| 135 | + return "", errors.Wrap(err, "Could not read file infos") |
| 136 | + } |
| 137 | + |
| 138 | + zipWriter := zip.NewWriter(zipFile) |
| 139 | + defer zipWriter.Close() |
| 140 | + |
| 141 | + fileToZipHeader, err := zip.FileInfoHeader(fileToZipInfo) |
| 142 | + if err != nil { |
| 143 | + return "", errors.Wrap(err, "Could not add file infos to zip handler") |
| 144 | + } |
| 145 | + |
| 146 | + fileToZipHeader.Name = fileToZipInfo.Name() |
| 147 | + |
| 148 | + fileToZipWriter, err := zipWriter.CreateHeader(fileToZipHeader) |
| 149 | + if err != nil { |
| 150 | + return "", errors.Wrap(err, "Could not create zip header") |
| 151 | + } |
| 152 | + |
| 153 | + if _, err = io.Copy(fileToZipWriter, fileToZip); err != nil { |
| 154 | + return "", errors.Wrap(err, "Could not zip file") |
| 155 | + } |
| 156 | + if err := zipFile.Close(); err != nil { |
| 157 | + return "", errors.Wrap(err, "Could not close file") |
| 158 | + } |
| 159 | + return filepath.Abs(fileToZipInfo.Name()) |
| 160 | +} |
0 commit comments