mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-23 04:58:47 +02:00
fix:#65 added new UID generation for ical File
This commit is contained in:
@ -1,6 +1,10 @@
|
|||||||
package functions
|
package functions
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// check if string is empty or contains only whitespaces
|
// check if string is empty or contains only whitespaces
|
||||||
func OnlyWhitespace(word string) bool {
|
func OnlyWhitespace(word string) bool {
|
||||||
@ -25,3 +29,10 @@ func ReplaceEmptyString(word string, replacement string) string {
|
|||||||
}
|
}
|
||||||
return word
|
return word
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HashString(s string) string {
|
||||||
|
hash := sha256.New()
|
||||||
|
hash.Write([]byte(s))
|
||||||
|
hashInBytes := hash.Sum(nil)
|
||||||
|
return hex.EncodeToString(hashInBytes)
|
||||||
|
}
|
||||||
|
@ -31,3 +31,24 @@ func TestOnlyWhitespace(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHashString(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"empty string", args{""}, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
||||||
|
{"non-empty string", args{"abc"}, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := HashString(tt.args.s); got != tt.want {
|
||||||
|
t.Errorf("HashString() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"htwkalender/model"
|
"htwkalender/model"
|
||||||
"htwkalender/service/functions"
|
"htwkalender/service/functions"
|
||||||
"htwkalender/service/names"
|
"htwkalender/service/names"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jordic/goics"
|
"github.com/jordic/goics"
|
||||||
@ -32,16 +31,18 @@ func (icalModel IcalModel) EmitICal() goics.Componenter {
|
|||||||
//add v time zone
|
//add v time zone
|
||||||
icalModel.vtimezone(c)
|
icalModel.vtimezone(c)
|
||||||
|
|
||||||
var timeStamp = time.Now().Local().In(europeTime).Format("20060102T150405")
|
for _, event := range icalModel.Events {
|
||||||
|
|
||||||
for i, event := range icalModel.Events {
|
|
||||||
mapEntry, mappingFound := icalModel.Mapping[event.UUID]
|
mapEntry, mappingFound := icalModel.Mapping[event.UUID]
|
||||||
|
|
||||||
s := goics.NewComponent()
|
s := goics.NewComponent()
|
||||||
s.SetType("VEVENT")
|
s.SetType("VEVENT")
|
||||||
|
|
||||||
s.AddProperty(goics.FormatDateTime("DTSTAMP", time.Now().Local().In(europeTime)))
|
s.AddProperty(goics.FormatDateTime("DTSTAMP", time.Now().Local().In(europeTime)))
|
||||||
s.AddProperty("UID", strconv.FormatInt(int64(i), 16)+"-"+timeStamp+"@htwkalender.de")
|
|
||||||
|
// create a unique id for the event by hashing the event start, end, course and name
|
||||||
|
var eventHash = functions.HashString(event.Start.String() + event.End.String() + event.Course + event.Name)
|
||||||
|
|
||||||
|
s.AddProperty("UID", eventHash+"@htwkalender.de")
|
||||||
s.AddProperty(goics.FormatDateTime("DTEND", event.End.Time().Local().In(europeTime)))
|
s.AddProperty(goics.FormatDateTime("DTEND", event.End.Time().Local().In(europeTime)))
|
||||||
s.AddProperty(goics.FormatDateTime("DTSTART", event.Start.Time().Local().In(europeTime)))
|
s.AddProperty(goics.FormatDateTime("DTSTART", event.Start.Time().Local().In(europeTime)))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user