-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathpreview.ts
147 lines (124 loc) · 3.78 KB
/
preview.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Blob, Ref } from '@hcengineering/core'
import { concatLink } from '@hcengineering/core'
import { getMetadata } from '@hcengineering/platform'
import { getFileUrl, getCurrentWorkspaceId } from './file'
import presentation from './plugin'
export interface PreviewConfig {
image: string
video: string
}
export interface VideoMeta {
status: 'ready' | 'error' | 'inprogress' | 'queued' | 'downloading' | 'pendingupload'
thumbnail: string
hls: string
}
const defaultImagePreview = (): string => `/files/${getCurrentWorkspaceId()}?file=:blobId&size=:size`
/**
*
* PREVIEW_CONFIG env variable format.
* - image - an Url with :workspace, :blobId, :downloadFile, :size placeholders.
* - video - an Url with :workspace, :blobId placeholders.
*/
export function parsePreviewConfig (config?: string): PreviewConfig | undefined {
if (config === undefined) {
return
}
const previewConfig = { image: defaultImagePreview(), video: '' }
const configs = config.split(';')
for (const c of configs) {
if (c.includes('|')) {
const [key, value] = c.split('|')
if (key === 'image') {
previewConfig.image = value
} else if (key === 'video') {
previewConfig.video = value
} else {
throw new Error(`Unknown preview config key: ${key}`)
}
} else {
// fallback to image-only config for compatibility
previewConfig.image = c
}
}
return Object.freeze(previewConfig)
}
export function getPreviewConfig (): PreviewConfig {
return (
(getMetadata(presentation.metadata.PreviewConfig) as PreviewConfig) ?? {
image: defaultImagePreview(),
video: ''
}
)
}
export async function getBlobRef (
file: Ref<Blob>,
name?: string,
width?: number
): Promise<{
src: string
srcset: string
}> {
return {
src: getFileUrl(file, name),
srcset: getSrcSet(file, width)
}
}
export async function getBlobSrcSet (file: Ref<Blob>, width?: number): Promise<string> {
return getSrcSet(file, width)
}
export function getSrcSet (_blob: Ref<Blob>, width?: number): string {
return blobToSrcSet(getPreviewConfig(), _blob, width)
}
function blobToSrcSet (cfg: PreviewConfig, blob: Ref<Blob>, width: number | undefined): string {
if (blob.includes('://')) {
return ''
}
let url = cfg.image.replaceAll(':workspace', encodeURIComponent(getCurrentWorkspaceId()))
const downloadUrl = getFileUrl(blob)
const frontUrl = getMetadata(presentation.metadata.FrontUrl) ?? window.location.origin
if (!url.includes('://')) {
url = concatLink(frontUrl ?? '', url)
}
url = url.replaceAll(':downloadFile', encodeURIComponent(downloadUrl))
url = url.replaceAll(':blobId', encodeURIComponent(blob))
let result = ''
const fu = url
if (width !== undefined) {
result +=
fu.replaceAll(':size', `${width}`) +
' 1x , ' +
fu.replaceAll(':size', `${width * 2}`) +
' 2x, ' +
fu.replaceAll(':size', `${width * 3}`) +
' 3x'
}
return result
}
/***
* @deprecated, please use Blob direct operations.
*/
export function getFileSrcSet (_blob: Ref<Blob>, width?: number): string {
return blobToSrcSet(getPreviewConfig(), _blob, width)
}
/**
* @public
*/
export async function getVideoMeta (file: string, filename?: string): Promise<VideoMeta | undefined> {
const cfg = getPreviewConfig()
let url = cfg.video
.replaceAll(':workspace', encodeURIComponent(getCurrentWorkspaceId()))
.replaceAll(':blobId', encodeURIComponent(file))
if (url === '') {
return undefined
}
const frontUrl = getMetadata(presentation.metadata.FrontUrl) ?? window.location.origin
if (!url.includes('://')) {
url = concatLink(frontUrl ?? '', url)
}
try {
const response = await fetch(url)
if (response.ok) {
return (await response.json()) as VideoMeta
}
} catch {}
}