Skip to content

Commit

Permalink
change hash algorithm compatible for fetchNextcloudApp
Browse files Browse the repository at this point in the history
  • Loading branch information
onny committed Feb 5, 2024
1 parent f7a363a commit 947d8b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ module git.helsinki.tools/helsinki-systems/nc4nix
go 1.17

require github.com/hashicorp/go-version v1.6.0

require (
github.com/codeclysm/extract/v3 v3.1.1 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect
github.com/klauspost/compress v1.15.13 // indirect
github.com/nix-community/go-nix v0.0.0-20231219074122-93cb24a86856 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
github.com/codeclysm/extract/v3 v3.1.1 h1:iHZtdEAwSTqPrd+1n4jfhr1qBhUWtHlMTjT90+fJVXg=
github.com/codeclysm/extract/v3 v3.1.1/go.mod h1:ZJi80UG2JtfHqJI+lgJSCACttZi++dHxfWuPaMhlOfQ=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/klauspost/compress v1.15.13 h1:NFn1Wr8cfnenSJSA46lLq4wHCcBzKTSjnBIexDMMOV0=
github.com/klauspost/compress v1.15.13/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/nix-community/go-nix v0.0.0-20231219074122-93cb24a86856 h1:CHnKW7ZH43KDkO9vDazQefi82Z0l1smKhSOpMsV1A9I=
github.com/nix-community/go-nix v0.0.0-20231219074122-93cb24a86856/go.mod h1:0FdXufC8BrrWsr65fGYC0fI6hlk4ku+JHGUiYhX/6g4=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
34 changes: 33 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import (
"net/http"
"os"
"strings"
"context"
"bytes"
"path/filepath"

"github.com/hashicorp/go-version"

"github.com/nix-community/go-nix/pkg/nar"
"github.com/nix-community/go-nix/pkg/nixbase32"
"github.com/codeclysm/extract/v3"
)

var DEBUG bool
Expand Down Expand Up @@ -57,6 +64,7 @@ type AppJson map[string]App
func major(t string) string {
return strings.Split(t, ".")[0]
}

func loadFile(t string) (AppJson, error) {
fname := major(t) + ".json"
log.Print("Loading " + fname)
Expand Down Expand Up @@ -109,6 +117,9 @@ func writeFile(t string, c AppJson) {
}

func prefetch(url string) (string, error) {
h := sha256.New()
var sha256 string

resp, err := http.Get(url)
if err != nil || resp.StatusCode != http.StatusOK {
log.Print("Prefetch failed for: ", url, err)
Expand All @@ -118,9 +129,30 @@ func prefetch(url string) (string, error) {
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print("Prefetch failed reading body for: ", url, err)
}
dname, err := os.MkdirTemp("", "")
if err != nil {
log.Fatal(err)
}
buffer := bytes.NewBuffer(contents)
extract.Archive(context.Background(), buffer, dname, nil)
entries, err := os.ReadDir(dname)
if err != nil {
return "", err
}
sha256 := fmt.Sprintf("%x", sha256.Sum256(contents))
var entryPath string
for _, entry := range entries {
if entry.IsDir() {
entryPath = filepath.Join(dname, entry.Name())
break
}
}
err = nar.DumpPath(h, entryPath)
if err != nil {
panic(err)
}
defer os.RemoveAll(dname)
sha256 = nixbase32.EncodeToString(h.Sum(nil))

return sha256, err
}
Expand Down

0 comments on commit 947d8b4

Please sign in to comment.