diff --git a/main.go b/main.go index 0bb8104..1abf563 100644 --- a/main.go +++ b/main.go @@ -272,22 +272,63 @@ func updateROMCache() { } // http - GET / -// The LineageOS updater app needs a JSON array of type LineageOSROM -// Marshals romCache.ROMs to JSON to serve as the response body +// The LineageOS updater app needs a JSON array of v2 builds +// Marshals romCache.ROMs to V2 JSON format to serve as the response body func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { checkNewBuilds() romCache.RLock() - httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs} - romCache.RUnlock() + defer romCache.RUnlock() - b, err := json.Marshal(httpResponseJSON) + var builds []V2BuildResponse + + for _, rom := range romCache.ROMs { + relPath := rom.Filename + if strings.Contains(rom.URL, "/public/") { + parts := strings.SplitN(rom.URL, "/public/", 2) + if len(parts) == 2 { + relPath = parts[1] + } + } + + file := V2BuildFile{ + Date: rom.Date, + Datetime: int64(rom.Datetime), + Filename: rom.Filename, + Filepath: "/public/" + relPath, + OSPatchLevel: rom.OSPatchLevel, + OSSDKLevel: rom.OSSDKLevel, + OTAPropertyFiles: rom.OTAPropertyFiles, + SHA1: rom.SHA1, + SHA256: rom.SHA256, + Size: int64(rom.Size), + Type: strings.ToLower(rom.Romtype), + URL: rom.URL, + } + + build := V2BuildResponse{ + Date: rom.Date, + Datetime: int64(rom.Datetime), + Files: []V2BuildFile{file}, + Type: strings.ToLower(rom.Romtype), + Version: rom.Version, + } + + builds = append(builds, build) + } + + if builds == nil { + builds = []V2BuildResponse{} + } + + b, err := json.Marshal(builds) if err != nil { w.WriteHeader(http.StatusInternalServerError) - log.Printf("failed to marshal lineageOSROMs to json: %v", err) + log.Printf("failed to marshal v2 builds to json: %v", err) return } + w.Header().Set("Content-Type", "application/json") w.Write(b) }