2022-07-16 22:20:56 +01:00
|
|
|
package index
|
|
|
|
|
|
|
|
import (
|
2022-07-25 15:39:05 +01:00
|
|
|
"golang.captainalm.com/cityuni-webserver/utils/yaml"
|
2022-07-16 22:20:56 +01:00
|
|
|
"html/template"
|
2022-07-26 19:49:32 +01:00
|
|
|
"math"
|
2022-07-28 00:32:38 +01:00
|
|
|
"net/http"
|
2022-07-16 22:20:56 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-07-26 17:35:13 +01:00
|
|
|
const dateFormat = "01-2006"
|
2022-07-16 22:20:56 +01:00
|
|
|
|
|
|
|
type EntryYaml struct {
|
2022-07-27 01:16:37 +01:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Content string `yaml:"content"`
|
|
|
|
StartDate yaml.DateType `yaml:"startDate"`
|
|
|
|
EndDate yaml.DateType `yaml:"endDate"`
|
|
|
|
VideoLocation template.URL `yaml:"videoLocation"`
|
|
|
|
VideoContentType string `yaml:"videoContentType"`
|
|
|
|
ThumbnailLocations []template.URL `yaml:"thumbnailLocations"`
|
|
|
|
ImageLocations []template.URL `yaml:"imageLocations"`
|
|
|
|
ImageAltTexts []string `yaml:"imageAltTexts"`
|
2022-07-16 22:20:56 +01:00
|
|
|
}
|
|
|
|
|
2022-07-26 19:49:32 +01:00
|
|
|
type ImageReference struct {
|
2022-07-27 01:16:37 +01:00
|
|
|
ThumbnailLocation template.URL
|
|
|
|
ImageLocation template.URL
|
2022-07-26 23:51:27 +01:00
|
|
|
ImageAltText string
|
2022-07-26 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 22:20:56 +01:00
|
|
|
func (ey EntryYaml) GetStartDate() string {
|
|
|
|
return ey.StartDate.Format(dateFormat)
|
|
|
|
}
|
|
|
|
|
2022-07-28 00:32:38 +01:00
|
|
|
func (ey EntryYaml) GetStartDateHTML() string {
|
|
|
|
return ey.StartDate.Format(http.TimeFormat)
|
|
|
|
}
|
|
|
|
|
2022-07-16 22:20:56 +01:00
|
|
|
func (ey EntryYaml) GetEndDate() string {
|
|
|
|
if ey.EndDate.IsZero() {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return ey.EndDate.Format(dateFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 00:32:38 +01:00
|
|
|
func (ey EntryYaml) GetEndDateHTML() string {
|
|
|
|
return ey.GetEndTime().Format(http.TimeFormat)
|
|
|
|
}
|
|
|
|
|
2022-07-16 22:20:56 +01:00
|
|
|
func (ey EntryYaml) GetEndTime() time.Time {
|
|
|
|
if ey.EndDate.IsZero() {
|
|
|
|
return time.Now()
|
|
|
|
} else {
|
2022-07-25 15:39:05 +01:00
|
|
|
return ey.EndDate.Time
|
2022-07-16 22:20:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ey EntryYaml) GetContent() template.HTML {
|
|
|
|
return template.HTML(ey.Content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ey EntryYaml) GetDuration() time.Duration {
|
2022-07-25 15:39:05 +01:00
|
|
|
return ey.GetEndTime().Sub(ey.StartDate.Time).Truncate(time.Second)
|
2022-07-16 22:20:56 +01:00
|
|
|
}
|
2022-07-26 19:49:32 +01:00
|
|
|
|
2022-07-27 11:53:55 +01:00
|
|
|
func (ey EntryYaml) GetInt64Duration() int64 {
|
|
|
|
return int64(ey.GetDuration())
|
|
|
|
}
|
|
|
|
|
2022-07-26 19:49:32 +01:00
|
|
|
func (ey EntryYaml) GetImageCount() int {
|
2022-07-26 23:51:27 +01:00
|
|
|
return int(math.Min(math.Min(float64(len(ey.ThumbnailLocations)), float64(len(ey.ImageLocations))), float64(len(ey.ImageAltTexts))))
|
2022-07-26 19:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ey EntryYaml) GetImages() []ImageReference {
|
|
|
|
toReturn := make([]ImageReference, ey.GetImageCount())
|
2022-07-26 23:51:27 +01:00
|
|
|
for i := 0; i < len(ey.ThumbnailLocations) && i < len(ey.ImageLocations) && i < len(ey.ImageAltTexts); i++ {
|
2022-07-26 19:49:32 +01:00
|
|
|
toReturn[i] = ImageReference{
|
|
|
|
ThumbnailLocation: ey.ThumbnailLocations[i],
|
|
|
|
ImageLocation: ey.ImageLocations[i],
|
2022-07-26 23:51:27 +01:00
|
|
|
ImageAltText: ey.ImageAltTexts[i],
|
2022-07-26 19:49:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return toReturn
|
|
|
|
}
|