-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationContext.js
More file actions
164 lines (147 loc) · 4.85 KB
/
ConfigurationContext.js
File metadata and controls
164 lines (147 loc) · 4.85 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* eslint-disable no-unused-vars */
import React from 'react';
import { useQuery } from 'react-query';
import { storage as StorageApi, resource as ResourceApi } from '@vidispine/vdt-api';
import filenameScript from '../pages/Settings/filenameScript';
const ConfigurationContext = React.createContext();
const parseStorages = ({ storage: storageList }) => {
return storageList.reduce((acc, curr) => {
const { method: methodList = [], metadata = {}, id: storageId } = curr;
const output = {};
let storageType;
const { field = [] } = metadata;
if (field.length) {
if (field.some(({ key }) => key === 'transcodeThemeSourceStorage')) storageType = 'input';
if (field.some(({ key }) => key === 'transcodeThemeOutputStorage')) storageType = 'output';
if (!storageType) return acc;
const { value = '-' } = field.find(({ key }) => key === 'description') || {};
output.description = value;
}
const [defaultMethod] = methodList;
if (defaultMethod) {
const { uri: uriString, ...params } = defaultMethod;
const methodUri = new URL(uriString);
let { protocol, pathname, search } = methodUri;
if (pathname.startsWith('//')) pathname = pathname.slice(2);
if (protocol.endsWith(':')) protocol = protocol.slice(0, -1);
if (search.startsWith('?')) search = search.slice(1);
const [access, bucket] = pathname.split('@');
const [accessKey] = access.split(':_VSENC__');
const [name, ...folderPath] = bucket?.split('/') || [];
const path = folderPath.join('/');
const queries = search.split('&').reduce((a, c) => {
const [key, value] = c.split('=');
return { ...a, [key]: value };
}, {});
Object.assign(output, {
accessKey,
name,
path,
protocol,
storageId,
...queries,
...params,
});
}
return { ...acc, [storageType]: output };
}, {});
};
const parseResources = ({ resource: resourceList }) =>
resourceList.map(({ id, vidinet }) => ({ id, ...vidinet }));
export function useGetStorages() {
return useQuery(
['storages'],
() => {
return StorageApi.listStorage({}).then(({ data = {} }) => {
return parseStorages(data);
});
},
{
refetchOnWindowFocus: false,
staleTime: Infinity,
},
);
}
export function useGetResources() {
return useQuery(
['resources'],
() =>
ResourceApi.listResourceType({ resourceType: 'vidinet' }).then(({ data = {} }) =>
parseResources(data),
),
{
refetchOnWindowFocus: false,
staleTime: Infinity,
},
);
}
export function useConfiguration() {
const context = React.useContext(ConfigurationContext);
return context;
}
export function ConfigurationProvider({ children }) {
const {
data: storages,
isLoading: isLoadingStorages,
isError: isErrorStorages,
} = useGetStorages();
const {
data: resources,
isLoading: isLoadingResources,
isError: isErrorResources,
} = useGetResources();
const isLoading = isLoadingStorages || isLoadingResources;
const isError = isErrorStorages || isErrorResources;
const onUpdateStorage = ({ input, output }) => {
const {
protocol,
accessKey,
secretKey,
name,
path,
// region,
id: storageMethodId,
storageId,
} = {
...input,
...output,
};
let uri = `${protocol}://${accessKey}:${secretKey}@${name}/${path}`;
if (uri.charAt(uri.length - 1) !== '/') uri = uri.concat('/');
const encodedAccessKey = encodeURIComponent(accessKey);
const encodedSecretKey = encodeURIComponent(secretKey);
const encodedUri = `${protocol}://${encodedAccessKey}:${encodedSecretKey}@${name}/${path}`;
const encodedUrl = encodeURIComponent(uri);
// if (region && region !== 'auto') uri = uri.concat(`?region=${region}`);
if (storageId) {
return StorageApi.modifyStorageMethod({
storageMethodId,
storageId,
queryParams: { url: encodedUrl },
});
}
const storageDocument = {
type: 'LOCAL',
capacity: 800000000000,
method: [{ uri: encodedUri, read: true, write: true, browse: true }],
metadata: { field: [] },
};
if (input) {
storageDocument.metadata.field = [{ key: 'transcodeThemeSourceStorage', value: true }];
} else if (output) {
storageDocument.metadata.field = [
{ key: 'transcodeThemeOutputStorage', value: true },
{ key: 'filenameScript', value: filenameScript },
];
}
return StorageApi.createStorage({ storageDocument });
};
const contextValue = React.useMemo(
() => ({ onUpdateStorage, storages, resources, isLoading, isError }),
[isError, isLoading, resources, storages],
);
return (
<ConfigurationContext.Provider value={contextValue}>{children}</ConfigurationContext.Provider>
);
}
export default ConfigurationContext;