Compare commits

...

3 Commits

Author SHA1 Message Date
238dab70fe more appropriate function name
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-30 20:14:34 -06:00
75668ad531 use simple type conversion instead fmt package 2023-06-30 20:12:44 -06:00
f308e4351a update comments 2023-06-30 20:11:49 -06:00
2 changed files with 12 additions and 10 deletions

12
main.go
View File

@ -36,7 +36,7 @@ type ROMCache struct {
const ( const (
romDirectory = "public" // directory where ROMs are available for download romDirectory = "public" // directory where ROMs are available for download
buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory
cacheFile = "public/romcache.json" cacheFile = "public/romcache.json" // persistence between server restarts so we don't have to rehash all the ROM files each time the program starts
) )
var ( // evil global variable var ( // evil global variable
@ -67,7 +67,7 @@ func init() {
log.Printf("loaded cached file: %s", rom.Filename) log.Printf("loaded cached file: %s", rom.Filename)
} }
// Check if any new build artifacts and preload the romCache // Check if any new build artifacts and load any new files into the romCache
moveBuildArtifacts() moveBuildArtifacts()
go updateROMCache() go updateROMCache()
} }
@ -106,7 +106,7 @@ func updateROMCache() {
for i, v := range files { for i, v := range files {
isLineageROM, splitName := isLineageROMZip(v) isLineageROM, splitName := parseROMFileName(v)
if !isLineageROM { if !isLineageROM {
continue continue
} }
@ -179,9 +179,11 @@ func updateROMCache() {
} }
// http - GET / // http - GET /
// Writes JSON response for the updater app to know what versions are available to download // The LineageOS updater app needs a JSON array of type LineageOSROM
// Marshal's romCache.ROMs to JSON to serve as the response body
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
go func() { go func() { // Also checks for new builds - TBD need a better method as the first request will return no new updates. inotify?
newBuilds := moveBuildArtifacts() newBuilds := moveBuildArtifacts()
if newBuilds { if newBuilds {
updateROMCache() updateROMCache()

View File

@ -30,7 +30,7 @@ func moveBuildArtifacts() bool {
var newROMs bool var newROMs bool
for _, v := range files { for _, v := range files {
if isLineageROM, _ := isLineageROMZip(v); !isLineageROM { // skip files that aren't LineageOS ROMs if isLineageROM, _ := parseROMFileName(v); !isLineageROM { // skip files that aren't LineageOS ROMs
continue continue
} }
@ -61,14 +61,14 @@ func hashFile(filename string) (string, error) {
return "", fmt.Errorf("failed to copy data from file to hash function: %v", err) return "", fmt.Errorf("failed to copy data from file to hash function: %v", err)
} }
return fmt.Sprintf("%x", h.Sum(nil)), nil return string(h.Sum(nil)), nil
} }
// false if a file is not a LineageOS ROM .zip file // false if a file is not a LineageOS ROM .zip file
// no formal validation is performed - only file naming convention is checked // no formal validation is performed - only file naming convention is checked
// also returns a lineage ROM's filename sliced and delimited by -'s // also returns a lineage ROM's filename sliced and delimited by -'s
// Example filename: lineage-20.0-20230604-UNOFFICIAL-sunfish.zip // Example filename: lineage-20.0-20230604-UNOFFICIAL-sunfish.zip
func isLineageROMZip(v fs.DirEntry) (bool, []string) { func parseROMFileName(v fs.DirEntry) (bool, []string) {
// skip directories, non .zip files and files that don't begin with lineage- // 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-") { if v.Type().IsDir() || !strings.HasSuffix(v.Name(), ".zip") || !strings.HasPrefix(v.Name(), "lineage-") {