create isLineageROMZip function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 15:41:01 -06:00
parent 623b8f74f1
commit 464f4d09e7

44
main.go
View File

@ -81,13 +81,8 @@ func updateROMCache(romDirectory string) {
for i, v := range files { for i, v := range files {
// skip directories isLineageROM, splitName := isLineageROMZip(v)
if v.Type().IsDir() { if !isLineageROM {
continue
}
// skip non .zip files
if !strings.HasSuffix(v.Name(), ".zip") {
continue continue
} }
@ -99,11 +94,6 @@ func updateROMCache(romDirectory string) {
} }
romCache.Unlock() 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) { go func(v fs.DirEntry) {
fInfo, err := v.Info() fInfo, err := v.Info()
if err != nil { if err != nil {
@ -157,19 +147,10 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool {
var newROMs bool var newROMs bool
for _, v := range files { for _, v := range files {
// skip directories if isLineageROM, _ := isLineageROMZip(v); !isLineageROM { // skip files that aren't LineageOS ROMs
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 {
continue continue
} }
newROMs = true newROMs = true
log.Printf("new build found - moving file %s", v.Name()) 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())) 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 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 / // http - GET /
// Writes JSON response for the updater app to know what versions are available to download // Writes JSON response for the updater app to know what versions are available to download
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { 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) { func hashFile(filename string) (string, error) {
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {