Fix scanning files for resources

This commit is contained in:
Daniel 2020-11-24 16:10:26 +01:00
parent 889bc1c389
commit 08fda000bb
2 changed files with 36 additions and 18 deletions

View file

@ -1,7 +1,7 @@
package updater package updater
import ( import (
"fmt" "path"
"regexp" "regexp"
"strings" "strings"
) )
@ -13,34 +13,45 @@ var (
// GetIdentifierAndVersion splits the given file path into its identifier and version. // GetIdentifierAndVersion splits the given file path into its identifier and version.
func GetIdentifierAndVersion(versionedPath string) (identifier, version string, ok bool) { func GetIdentifierAndVersion(versionedPath string) (identifier, version string, ok bool) {
// extract version dirPath, filename := path.Split(versionedPath)
rawVersion := fileVersionRegex.FindString(versionedPath)
// Extract version from filename.
rawVersion := fileVersionRegex.FindString(filename)
if rawVersion == "" { if rawVersion == "" {
// No version present in file, making it invalid.
return "", "", false return "", "", false
} }
// replace - with . and trim _ // Trim the `_v` that gets caught by the regex and
// replace `-` with `.` to get the version string.
version = strings.Replace(strings.TrimLeft(rawVersion, "_v"), "-", ".", -1) version = strings.Replace(strings.TrimLeft(rawVersion, "_v"), "-", ".", -1)
// put together without version // Put the filename back together without version.
i := strings.Index(versionedPath, rawVersion) i := strings.Index(filename, rawVersion)
if i < 0 { if i < 0 {
// extracted version not in string (impossible) // extracted version not in string (impossible)
return "", "", false return "", "", false
} }
return versionedPath[:i] + versionedPath[i+len(rawVersion):], version, true filename = filename[:i] + filename[i+len(rawVersion):]
// Put the full path back together and return it.
// `dirPath + filename` is guaranteed by path.Split()
return dirPath + filename, version, true
} }
// GetVersionedPath combines the identifier and version and returns it as a file path. // GetVersionedPath combines the identifier and version and returns it as a file path.
func GetVersionedPath(identifier, version string) (versionedPath string) { func GetVersionedPath(identifier, version string) (versionedPath string) {
// split in half identifierPath, filename := path.Split(identifier)
splittedFilePath := strings.SplitN(identifier, ".", 2)
// replace . with - // Split the filename where the version should go.
splittedFilename := strings.SplitN(filename, ".", 2)
// Replace `.` with `-` for the filename format.
transformedVersion := strings.Replace(version, ".", "-", -1) transformedVersion := strings.Replace(version, ".", "-", -1)
// put together // Put everything back together and return it.
if len(splittedFilePath) == 1 { versionedPath = identifierPath + splittedFilename[0] + "_v" + transformedVersion
return fmt.Sprintf("%s_v%s", splittedFilePath[0], transformedVersion) if len(splittedFilename) > 1 {
versionedPath += "." + splittedFilename[1]
} }
return fmt.Sprintf("%s_v%s.%s", splittedFilePath[0], transformedVersion, splittedFilePath[1]) return versionedPath
} }

View file

@ -39,12 +39,23 @@ func (reg *ResourceRegistry) ScanStorage(root string) error {
// walk fs // walk fs
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { _ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
// skip tmp dir (including errors trying to read it)
if strings.HasPrefix(path, reg.tmpDir.Path) {
return filepath.SkipDir
}
// handle walker error
if err != nil { if err != nil {
lastError = fmt.Errorf("%s: could not read %s: %w", reg.Name, path, err) lastError = fmt.Errorf("%s: could not read %s: %w", reg.Name, path, err)
log.Warning(lastError.Error()) log.Warning(lastError.Error())
return nil return nil
} }
// ignore directories
if info.IsDir() {
return nil
}
// get relative path to storage // get relative path to storage
relativePath, err := filepath.Rel(reg.storageDir.Path, path) relativePath, err := filepath.Rel(reg.storageDir.Path, path)
if err != nil { if err != nil {
@ -52,10 +63,6 @@ func (reg *ResourceRegistry) ScanStorage(root string) error {
log.Warning(lastError.Error()) log.Warning(lastError.Error())
return nil return nil
} }
// ignore files in tmp dir
if strings.HasPrefix(relativePath, reg.tmpDir.Path) {
return nil
}
// convert to identifier and version // convert to identifier and version
relativePath = filepath.ToSlash(relativePath) relativePath = filepath.ToSlash(relativePath)