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 = { const TimelineMediaTypes = {
UNKNOWN: 0,
IMAGE: 1, IMAGE: 1,
VIDEO: 2, VIDEO: 2,
AUDIO: 3, AUDIO: 3,

View File

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

View File

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

View File

@ -193,6 +193,25 @@ var HMNTemplateFuncs = template.FuncMap{
"noescape": func(str string) template.HTML { "noescape": func(str string) template.HTML {
return template.HTML(str) 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: // NOTE(asaf): Template specific functions:
"projectcarddata": func(project Project, classes string) ProjectCardData { "projectcarddata": func(project Project, classes string) ProjectCardData {

View File

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

View File

@ -89,6 +89,8 @@ func SnippetToTimelineItem(
item.EmbedMedia = append(item.EmbedMedia, videoMediaItem(asset)) item.EmbedMedia = append(item.EmbedMedia, videoMediaItem(asset))
} else if strings.HasPrefix(asset.MimeType, "audio/") { } else if strings.HasPrefix(asset.MimeType, "audio/") {
item.EmbedMedia = append(item.EmbedMedia, audioMediaItem(asset)) 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 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,
}
}