-
Notifications
You must be signed in to change notification settings - Fork 159
/
getFileUrl.ts
83 lines (76 loc) · 2.71 KB
/
getFileUrl.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
import { Resource, SpaceResource } from '../helpers'
import { urlJoin } from '../utils'
import { GetFileContentsFactory } from './getFileContents'
import { WebDavOptions } from './types'
import { DAV } from './client'
import { ocs } from '../ocs'
export const GetFileUrlFactory = (
dav: DAV,
getFileContentsFactory: ReturnType<typeof GetFileContentsFactory>,
{ axiosClient, baseUrl }: WebDavOptions
) => {
return {
async getFileUrl(
space: SpaceResource,
resource: Resource,
{
disposition = 'attachment',
isUrlSigningEnabled = false,
signUrlTimeout = 86400,
version = null,
doHeadRequest = false,
username = ''
}: {
disposition?: 'inline' | 'attachment'
isUrlSigningEnabled?: boolean
signUrlTimeout?: number
version?: string
doHeadRequest?: boolean
username?: string
}
): Promise<string> {
const inlineDisposition = disposition === 'inline'
const { path } = resource
let { downloadURL } = resource
let signed = true
if (!downloadURL && !inlineDisposition) {
// compute unsigned url
const webDavPath = space ? urlJoin(space.webDavPath, path) : resource.webDavPath
downloadURL = version
? dav.getFileUrl(urlJoin('meta', resource.fileId, 'v', version))
: dav.getFileUrl(webDavPath)
if (username && doHeadRequest) {
await axiosClient.head(downloadURL)
}
// sign url
if (isUrlSigningEnabled && username) {
const ocsClient = ocs(baseUrl, axiosClient)
downloadURL = await ocsClient.signUrl(downloadURL, username)
} else {
signed = false
}
}
// FIXME: re-introduce query parameters
// They are not supported by getFileContents() and as we don't need them right now, I'm disabling the feature completely for now
//
// // Since the pre-signed url contains query parameters and the caller of this method
// // can also provide query parameters we have to combine them.
// const queryStr = qs.stringify(options.query || null)
// const [url, signedQuery] = downloadURL.split('?')
// const combinedQuery = [queryStr, signedQuery].filter(Boolean).join('&')
// downloadURL = [url, combinedQuery].filter(Boolean).join('?')
if (!signed || inlineDisposition) {
const response = await getFileContentsFactory.getFileContents(space, resource, {
responseType: 'blob'
})
downloadURL = URL.createObjectURL(response.body)
}
return downloadURL
},
revokeUrl: (url: string) => {
if (url && url.startsWith('blob:')) {
URL.revokeObjectURL(url)
}
}
}
}