-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathprivatePreviewBlob.ts
60 lines (54 loc) · 1.49 KB
/
privatePreviewBlob.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
import { encodePath } from 'web-pkg/src/utils'
import { clientService, cacheService } from '../../services'
import { buildQueryString } from './common'
import isEqual from 'lodash-es/isEqual'
interface PrivatePreviewBlobOptions {
server: string
userId: string
resource: {
id: string
path: string
etag?: string
}
token: string
dimensions?: [number, number]
}
export const privatePreviewBlob = async (
options: PrivatePreviewBlobOptions,
cached = false
): Promise<string> => {
if (cached) {
return cacheFactory(options)
}
const url = [
options.server,
'remote.php/dav/files/',
options.userId,
encodePath(options.resource.path),
'?',
buildQueryString({
etag: options.resource.etag,
dimensions: options.dimensions
})
].join('')
try {
const { data } = await clientService.httpAuthenticated(options.token).get(url, {
responseType: 'blob'
})
return window.URL.createObjectURL(data)
} catch (ignored) {}
}
const cacheFactory = async (options: PrivatePreviewBlobOptions): Promise<string> => {
const hit = cacheService.filePreview.get(options.resource.id)
if (hit && hit.etag === options.resource.etag && isEqual(options.dimensions, hit.dimensions)) {
return hit.src
}
try {
const src = await privatePreviewBlob(options)
return cacheService.filePreview.set(
options.resource.id,
{ src, etag: options.resource.etag, dimensions: options.dimensions },
0
).src
} catch (ignored) {}
}