-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.ts
84 lines (75 loc) · 3.13 KB
/
config.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
import { EmbySite } from "@model/EmbySite";
import { ENV, EmbyConfig } from "../helper/env";
import { Map } from "../model/Map";
import { PlaybackInfo } from "@model/PlaybackInfo";
import { PictureQuality } from "@store/configSlice";
import { Image } from "@model/Image";
export type { EmbyConfig } from "../helper/env"
export const DEFAULT_EMBY_CONFIG: EmbyConfig = ENV.emby
export const config = {
emby: DEFAULT_EMBY_CONFIG,
tmdb: {
api_key: ENV.tmdb.api_key
}
}
export function makeEmbyUrl(params: Map<string, any>|null, path: string, endpoint: EmbyConfig) {
const url = new URL(`${endpoint.protocol ?? "https"}://${endpoint.host}:${endpoint.port ?? 443}${endpoint.path}${path}`)
params && Object.entries(params).forEach(([key, value]) => {
if (typeof value === "string") {
url.searchParams.append(key, value)
} else {
url.searchParams.append(key, String(value))
}
})
return url
}
export interface ImageProps {
maxHeight: number
maxWidth: number
tag: string
quality: number
}
export function getItemImage(site: EmbySite, id: string|number, quality = PictureQuality.High) {
const image: Image = {
primary: imageUrl(site, id, null, "Primary"),
backdrop: imageUrl(site, id, null, "Backdrop"),
logo: imageUrl(site, id, null, "Logo"),
}
return image
}
export function imageUrl(site: EmbySite, id: string|number, options: string|Partial<ImageProps>|null, type: "Primary"|string = "Primary") {
const endpoint = site.server!
if (typeof options === "string") {
return `${endpoint.protocol}://${endpoint.host}:${endpoint.port}${endpoint.path}emby/Items/${id}/Images/${type}?tag=${options}&quality=90`
} else {
const url = new URL(`${endpoint.protocol}://${endpoint.host}:${endpoint.port}${endpoint.path}emby/Items/${id}/Images/${type}`)
options && Object.entries(options).forEach(([key, value]) => {
url.searchParams.set(key, String(value))
})
return url.href
}
}
export function avatorUrl(id: string, options: string|Partial<ImageProps>, type: "Primary" = "Primary") {
return `${config.emby.protocol}://${config.emby.host}:${config.emby.port}${config.emby.path}emby/Users/${id}/Images/${type}?height=152&tag=${options}&quality=90`
}
export function playUrl(site: EmbySite, path: string|PlaybackInfo) {
const endpoint = site.server!
if (typeof path === "string") {
if (path?.startsWith("http")) return path
return `${endpoint.protocol}://${endpoint.host}:${endpoint.port}${endpoint.path}emby${path}`
} else {
const sources = path?.MediaSources ?? []
if (sources.length > 0) {
const source = sources[0]
if (source.Container === "strm") {
return source.Path
} else {
const streamPath = source.DirectStreamUrl ?? source.Path
if (streamPath?.startsWith("http")) return streamPath
else {
return `${endpoint.protocol}://${endpoint.host}:${endpoint.port}${endpoint.path}emby${streamPath}`
}
}
}
}
}