diff --git a/main.go b/main.go index 706c4ad..054c0c0 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,10 @@ package main import ( + "crypto/sha256" "encoding/json" "fmt" + "io" "io/fs" "log" "net/http" @@ -72,14 +74,19 @@ func getLineageOSROMs(romDirectory string) ([]LineageOSROM, error) { return nil } + fileHash, err := hashFile(d.Name()) + if err != nil { + log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", d.Name(), err) + } + lineageOSROM := LineageOSROM{ Datetime: int(fInfo.ModTime().Unix()), Filename: d.Name(), - ID: "TBD", + ID: fileHash, Romtype: "nightly", Size: int(fInfo.Size()), URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", d.Name()), - Version: "TBD", + Version: splitName[1], } lineageOSROMs = append(lineageOSROMs, lineageOSROM) @@ -114,3 +121,18 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) { w.Write(b) } + +func hashFile(filename string) (string, error) { + f, err := os.Open(filename) + if err != nil { + return "", fmt.Errorf("failed to open file '%s': %v: ", filename, err) + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + return "", fmt.Errorf("failed to copy data from file to hash function: %v", err) + } + + return string(h.Sum(nil)), nil +}