add function for moving new builds
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 13:28:36 -06:00
parent 8ef8b3d1be
commit 655020a73f

52
main.go
View File

@ -43,6 +43,8 @@ var (
func init() { func init() {
romCache = ROMCache{} romCache = ROMCache{}
romCache.Cached = make(map[string]bool) romCache.Cached = make(map[string]bool)
moveBuildArtifacts("out", "public")
go updateROMCache("public") go updateROMCache("public")
} }
@ -68,6 +70,7 @@ func updateROMCache(romDirectory string) {
log.Printf("failed to open ROM directory: %v", err) log.Printf("failed to open ROM directory: %v", err)
return return
} }
defer f.Close()
files, err := f.ReadDir(0) files, err := f.ReadDir(0)
if err != nil { if err != nil {
log.Printf("failed to read files in directory: %v", err) log.Printf("failed to read files in directory: %v", err)
@ -134,6 +137,48 @@ func updateROMCache(romDirectory string) {
} }
} }
// returns true if new builds were moved
func moveBuildArtifacts(outDirectory, romDirectory string) bool {
f, err := os.Open(romDirectory)
if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return false
}
defer f.Close()
files, err := f.ReadDir(0)
if err != nil {
log.Printf("failed to read files in directory: %v", err)
return false
}
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 {
continue
}
newROMs = true
err := os.Rename(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name()))
if err != nil {
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
continue
}
}
return newROMs
}
// 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) {
@ -152,7 +197,12 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
w.Write(b) w.Write(b)
go updateROMCache("public") go func() {
newBuilds := moveBuildArtifacts("out", "public")
if newBuilds {
updateROMCache("public")
}
}()
} }
func hashFile(filename string) (string, error) { func hashFile(filename string) (string, error) {