add version and ID (filehash)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 09:32:23 -06:00
parent 62722871e7
commit f9a0a1df8f

26
main.go
View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"crypto/sha256"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"log" "log"
"net/http" "net/http"
@ -72,14 +74,19 @@ func getLineageOSROMs(romDirectory string) ([]LineageOSROM, error) {
return nil 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{ lineageOSROM := LineageOSROM{
Datetime: int(fInfo.ModTime().Unix()), Datetime: int(fInfo.ModTime().Unix()),
Filename: d.Name(), Filename: d.Name(),
ID: "TBD", ID: fileHash,
Romtype: "nightly", Romtype: "nightly",
Size: int(fInfo.Size()), Size: int(fInfo.Size()),
URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", d.Name()), URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", d.Name()),
Version: "TBD", Version: splitName[1],
} }
lineageOSROMs = append(lineageOSROMs, lineageOSROM) lineageOSROMs = append(lineageOSROMs, lineageOSROM)
@ -114,3 +121,18 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
w.Write(b) 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
}