diff --git a/internal/objects/pipecopy.go b/internal/objects/pipecopy.go index 7e91853..efcbc00 100644 --- a/internal/objects/pipecopy.go +++ b/internal/objects/pipecopy.go @@ -4,14 +4,15 @@ import ( "bufio" "bytes" "context" + "crypto/sha256" + "encoding/hex" "fmt" + "github.com/spf13/viper" "io" "strings" "sync" "time" - "github.com/spf13/viper" - "github.com/gleanerio/nabu/internal/graph" log "github.com/sirupsen/logrus" @@ -23,6 +24,24 @@ func getLastElement(s string) string { return parts[len(parts)-1] } +// GenerateDateHash generates a unique hash based on the current date and time. +func generateDateHash() string { + // Get the current date and time + now := time.Now() + + // Format the date and time as a string + dateString := now.Format("2006-01-02 15:04:05") + + // Create a SHA256 hash + hash := sha256.New() + hash.Write([]byte(dateString)) + + // Convert the hash to a hex string + hashString := hex.EncodeToString(hash.Sum(nil)) + + return hashString +} + // PipeCopy writes a new object based on an prefix, this function assumes the objects are valid when concatenated // v1: viper config object // mc: minio client pointer @@ -32,7 +51,8 @@ func getLastElement(s string) string { // destprefix: destination prefix // sf: boolean to declare if single file or not. If so, skip skolimization since JSON-LD library output is enough func PipeCopy(v1 *viper.Viper, mc *minio.Client, name, bucket, prefix, destprefix string) error { - log.Printf("PipeCopy with name: %s bucket: %s prefix: %s", name, bucket, prefix) + orgname := v1.GetString("implementation_network.orgname") + log.Printf("PipeCopy with name: %s bucket: %s prefix: %s org name: %s", name, bucket, prefix, orgname) pr, pw := io.Pipe() // TeeReader of use? lwg := sync.WaitGroup{} // work group for the pipe writes... @@ -123,29 +143,23 @@ func PipeCopy(v1 *viper.Viper, mc *minio.Client, name, bucket, prefix, destprefi // Once we are done with the loop, put in the triples to associate all the graphURIs with the org. if lastProcessed { - data := `_:b0 . -_:b0 "` + time.Now().Format("2006-01-02 15:04:05") + `" . -_:b0 "GleanerIO Nabu generated catalog" . -_:b0 _:b1 . -_:b0 _:b2 . -_:b1 . -_:b1 "` + getLastElement(prefix) + `" . -_:b2 . -_:b2 "` + bucket + `" .` + data := ` . + "GleanerIO Nabu generated catalog" . + "` + time.Now().Format("2006-01-02 15:04:05") + `" . + . + . + . + "` + orgname + `" . + . + "` + getLastElement(prefix) + `" . +` for _, item := range idList { - data += `_:b0 <` + item + `> .` + "\n" + data += ` <` + item + `> .` + "\n" } - // TODO MakeURN with _:b0 Q's Will this work with a blank node? do after Skolemization? - // namedgraph, err := graph.MakeURN(v1, "resource IRI") - // sdataWithContext, err := graph.NtToNq(sdata, namedgraph) - - // TODO: Skolemize with sdataWithContext - sdata, err := graph.Skolemization(data, "release graph prov for ORG") - if err != nil { - log.Println(err) - } + namedgraph := "urn:gleaner.io:" + orgname + ":" + getLastElement(prefix) + ":datacatalog:" + generateDateHash() + sdata, err := graph.NtToNq(data, namedgraph) // Perform the final write to the pipe here // ilstr := strings.Join(idList, ",")