diff --git a/main.go b/main.go index 4ca069d..1df03dd 100644 --- a/main.go +++ b/main.go @@ -81,13 +81,8 @@ func updateROMCache(romDirectory string) { for i, v := range files { - // skip directories - if v.Type().IsDir() { - continue - } - - // skip non .zip files - if !strings.HasSuffix(v.Name(), ".zip") { + isLineageROM, splitName := isLineageROMZip(v) + if !isLineageROM { continue } @@ -99,11 +94,6 @@ func updateROMCache(romDirectory string) { } romCache.Unlock() - // Parse filename and skip files that can't be parsed - splitName := strings.Split(v.Name(), "-") - if len(splitName) != 5 { - continue - } go func(v fs.DirEntry) { fInfo, err := v.Info() if err != nil { @@ -157,19 +147,10 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool { var newROMs bool for _, v := range files { - // skip directories - if v.Type().IsDir() { - continue - } - // skip non .zip files - if !strings.HasSuffix(v.Name(), ".zip") { - continue - } - // Parse filename and skip files that can't be parsed - splitName := strings.Split(v.Name(), "-") - if len(splitName) != 5 { + if isLineageROM, _ := isLineageROMZip(v); !isLineageROM { // skip files that aren't LineageOS ROMs continue } + newROMs = true log.Printf("new build found - moving file %s", v.Name()) err := moveBuildFile(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name())) @@ -182,6 +163,22 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool { return newROMs } +// false if a file is not a LineageOS ROM .zip file +// no formal validation is performed - only file naming convention is checked +// also returns a lineage ROM's filename sliced and delimited by -'s +// Example filename: lineage-20.0-20230604-UNOFFICIAL-sunfish.zip +func isLineageROMZip(v fs.DirEntry) (bool, []string) { + + // skip directories, non .zip files and files that don't begin with lineage- + if v.Type().IsDir() || !strings.HasSuffix(v.Name(), ".zip") || !strings.HasPrefix(v.Name(), "lineage-") { + return false, nil + } + + splitName := strings.Split(v.Name(), "-") + // expect 5 dashes + return len(splitName) == 5, splitName +} + // http - GET / // Writes JSON response for the updater app to know what versions are available to download func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { @@ -208,6 +205,7 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { }() } +// Returns a sha256 hash of a file located at the path provided func hashFile(filename string) (string, error) { f, err := os.Open(filename) if err != nil {