-
Notifications
You must be signed in to change notification settings - Fork 202
/
metadata.ts
113 lines (103 loc) · 3.12 KB
/
metadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { capitalCase } from "~/utils/case"
import type { AudioDetail, ImageDetail, Metadata } from "~/types/media"
import { AUDIO, IMAGE } from "~/constants/media"
import { useProviderStore } from "~/stores/provider"
import type { NuxtI18nInstance } from "@nuxtjs/i18n"
const getImageType = (
imageType: string | undefined,
i18n: NuxtI18nInstance
) => {
if (imageType) {
if (imageType.split("/").length > 1) {
return imageType.split("/")[1]
}
return imageType
}
return i18n.t("mediaDetails.information.unknown")
}
const getAudioType = (audio: AudioDetail, i18n: NuxtI18nInstance) => {
if (!audio.alt_files) {
return audio.filetype ?? i18n.t("mediaDetails.information.unknown")
}
const altFormats = audio.alt_files
.map((altFile) => altFile.filetype)
.filter((filetype) => filetype !== audio.filetype)
const uniqueFormats = Array.from(new Set(altFormats))
if (audio.filetype) {
uniqueFormats.unshift(audio.filetype)
}
return [...uniqueFormats].join(", ")
}
export const getMediaMetadata = (
media: AudioDetail | ImageDetail,
i18n: NuxtI18nInstance,
imageInfo?: { width?: number; height?: number; type?: string }
) => {
const metadata: Metadata[] = []
if (media.source && media.providerName !== media.sourceName) {
metadata.push({
name: "provider",
label: "mediaDetails.providerLabel",
value: media.providerName || media.provider,
})
}
const sourceUrl = useProviderStore().getSourceUrl(
media.source ?? media.provider,
media.frontendMediaType
)
const sourceName = media.sourceName ?? media.providerName ?? media.provider
metadata.push({
name: "source",
label: "mediaDetails.sourceLabel",
source: media.source ?? media.provider,
url: sourceUrl,
value: sourceName,
})
if (media.category) {
metadata.push({
label: "mediaDetails.information.category",
value: i18n
.t(`filters.${media.frontendMediaType}Categories.${media.category}`)
.toString(),
})
}
const mediaTypeString =
media.frontendMediaType === IMAGE
? getImageType(imageInfo?.type, i18n)
: getAudioType(media, i18n)
metadata.push({
label: "mediaDetails.information.type",
value: mediaTypeString.toString().toUpperCase(),
})
if (media.frontendMediaType === IMAGE) {
metadata.push({
label: "imageDetails.information.dimensions",
value: `${i18n.t("imageDetails.information.sizeInPixels", {
width: imageInfo?.width,
height: imageInfo?.height,
})}`,
})
}
if (media.frontendMediaType === AUDIO) {
if (media.audio_set) {
metadata.unshift({
label: "audioDetails.table.album",
value: media.audio_set.title,
url: media.audio_set.foreign_landing_url,
})
}
if (media.genres && media.genres.length > 0) {
metadata.push({
label: "audioDetails.table.genre",
value: media.genres.map((genre) => capitalCase(genre)).join(", "),
})
}
if (media.sample_rate) {
metadata.push({
label: "audioDetails.table.sampleRate",
value: media.sample_rate.toString(),
})
}
}
return metadata
}