Added support for unknown filetypes in snippets

This commit is contained in:
Asaf Gartner 2022-04-07 08:19:48 +03:00
parent 6063a7dd71
commit 38e93d5208
6 changed files with 48 additions and 2 deletions

View File

@ -1,4 +1,5 @@
const TimelineMediaTypes = {
UNKNOWN: 0,
IMAGE: 1,
VIDEO: 2,
AUDIO: 3,

View File

@ -56,6 +56,10 @@
<audio src="{{ .AssetUrl }}" controls>
{{ else if eq .Type mediaembed }}
{{ .EmbedHTML }}
{{ else }}
<div class="project-card br2 pv1 ph2">
<a href="{{ .AssetUrl }}" target="_blank">{{ .Filename }} ({{ filesize .FileSize }})</a>
</div>
{{ end }}
</div>
{{ end }}

View File

@ -21,6 +21,10 @@
<div class="mb3 aspect-ratio aspect-ratio--16x9">
{{ .EmbedHTML }}
</div>
{{ else }}
<div class="project-card br2 pv1 ph2">
<a href="{{ .AssetUrl }}" target="_blank">{{ .Filename }} ({{ filesize .FileSize }})</a>
</div>
{{ end }}
{{ end }}
</div>

View File

@ -193,6 +193,25 @@ var HMNTemplateFuncs = template.FuncMap{
"noescape": func(str string) template.HTML {
return template.HTML(str)
},
"filesize": func(numBytes int) string {
scales := []string{
" bytes",
"kb",
"mb",
"gb",
}
num := float64(numBytes)
scale := 0
for num > 1024 && scale < len(scales)-1 {
num /= 1024
scale += 1
}
precision := 0
if scale > 0 {
precision = 2
}
return fmt.Sprintf("%.*f%s", precision, num, scales[scale])
},
// NOTE(asaf): Template specific functions:
"projectcarddata": func(project Project, classes string) ProjectCardData {

View File

@ -305,7 +305,8 @@ type TimelineItem struct {
type TimelineItemMediaType int
const (
TimelineItemMediaTypeImage TimelineItemMediaType = iota + 1
TimelineItemMediaTypeUnknown TimelineItemMediaType = iota
TimelineItemMediaTypeImage
TimelineItemMediaTypeVideo
TimelineItemMediaTypeAudio
TimelineItemMediaTypeEmbed
@ -318,6 +319,8 @@ type TimelineItemMedia struct {
ThumbnailUrl string
MimeType string
Width, Height int
Filename string
FileSize int
ExtraOpenGraphItems []OpenGraphItem
}

View File

@ -89,6 +89,8 @@ func SnippetToTimelineItem(
item.EmbedMedia = append(item.EmbedMedia, videoMediaItem(asset))
} else if strings.HasPrefix(asset.MimeType, "audio/") {
item.EmbedMedia = append(item.EmbedMedia, audioMediaItem(asset))
} else {
item.EmbedMedia = append(item.EmbedMedia, unknownMediaItem(asset))
}
}
@ -100,7 +102,8 @@ func SnippetToTimelineItem(
}
}
if len(item.EmbedMedia) > 0 && (item.EmbedMedia[0].Width == 0 || item.EmbedMedia[0].Height == 0) {
if len(item.EmbedMedia) == 0 ||
(len(item.EmbedMedia) > 0 && (item.EmbedMedia[0].Width == 0 || item.EmbedMedia[0].Height == 0)) {
item.CanShowcase = false
}
@ -185,3 +188,15 @@ func youtubeMediaItem(videoId string) templates.TimelineItemMedia {
},
}
}
func unknownMediaItem(asset *models.Asset) templates.TimelineItemMedia {
assetUrl := hmnurl.BuildS3Asset(asset.S3Key)
return templates.TimelineItemMedia{
Type: templates.TimelineItemMediaTypeUnknown,
AssetUrl: assetUrl,
MimeType: asset.MimeType,
Filename: asset.Filename,
FileSize: asset.Size,
}
}