-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseStargateHandlers.ts
More file actions
212 lines (207 loc) · 5.94 KB
/
Copy pathuseStargateHandlers.ts
File metadata and controls
212 lines (207 loc) · 5.94 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import {
ExportApi,
FileApi,
ResComputationStatus,
ResExport,
ResExportDefinitionType,
UtilsApi,
} from "@shapediver/sdk.geometry-api-sdk-v2";
import {
ISdStargateBakeDataCommandDto,
ISdStargateBakeDataReplyDto,
ISdStargateExportFileCommandDto,
ISdStargateExportFileReplyDto,
ISdStargateExportFileResultEnum,
ISdStargateGetDataCommandDto,
ISdStargateGetDataReplyDto,
ISdStargateGetDataResultEnum,
ISdStargateGetSupportedDataReplyDto,
} from "@shapediver/sdk.stargate-sdk-v1";
import {fetchFileWithToken} from "@shapediver/viewer.utils.mime-type";
import {useCallback} from "react";
import {SessionData} from "./useShapeDiverStargate";
/**
* Example files that can be used by the handlers.
*/
const exampleFiles: {filename: string; contentType: string; href: string}[] = [
{
filename: "test.json",
contentType: "application/json",
href: window.location.origin + window.location.pathname + "test.json",
},
{
filename: "test.3dm",
contentType: "model/vnd.3dm",
href: window.location.origin + window.location.pathname + "test.3dm",
},
{
filename: "test.dwg",
contentType: "application/dwg",
href: window.location.origin + window.location.pathname + "test.dwg",
},
];
/**
* Which content types, file extensions, and parameter types are supported by the handlers.
*/
const supportedData: Partial<ISdStargateGetSupportedDataReplyDto> = {
contentTypes: ["application/json", "application/dwg", "model/vnd.3dm"],
fileExtensions: ["json", "3dm", "dwg"],
parameterTypes: ["File"],
};
/**
* Hook providing example Stargate handlers.
* @returns
*/
export default function useStargateHandlers(): {
/**
* Types of parameters, content types, and file endings supported by the handlers.
*/
supportedData: Partial<ISdStargateGetSupportedDataReplyDto>;
/**
* Handler for the GET DATA command.
* @param data
* @param sessionData
* @returns
*/
getDataCommandHandler?: (
data: ISdStargateGetDataCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateGetDataReplyDto>;
/**
* Handler for the BAKE DATA command.
* @param data
* @param sessionData
* @returns
*/
bakeDataCommandHandler?: (
data: ISdStargateBakeDataCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateBakeDataReplyDto>;
/**
* Handler for the EXPORT FILE command.
* @param data
* @param sessionData
* @returns
*/
exportFileCommandHandler?: (
data: ISdStargateExportFileCommandDto,
sessionData: SessionData,
) => Promise<ISdStargateExportFileReplyDto>;
} {
// example handler for the GET DATA command
const getDataCommandHandler = useCallback(
async (
{parameter: {id: parameterId}}: ISdStargateGetDataCommandDto,
{config, session}: SessionData,
): Promise<ISdStargateGetDataReplyDto> => {
// get the definition of the parameter for which data is requested
const paramDef = session.parameters![parameterId];
// in this example we handle "File" parameters with "application/json" format
if (paramDef.type === "File") {
// try to find a suitable test file
const testFile = exampleFiles.find((f) =>
paramDef.format?.includes(f.contentType),
);
if (testFile) {
// download the file
const downloadedFile = await new UtilsApi(config).download(
testFile.href,
{responseType: "arraybuffer"},
);
// request a file upload URL from the Geometry API
const response = await new FileApi(config).uploadFile(
session.sessionId,
{
[parameterId]: {
size: (
downloadedFile.data as unknown as ArrayBuffer
).byteLength,
filename: testFile.filename,
format: testFile.contentType,
},
},
);
// extract the data for the file to be uploaded
const fileData = response.data.asset.file[parameterId];
// upload the file to the URL provided by the Geometry API
await new UtilsApi(config).upload(
fileData.href,
downloadedFile.data,
testFile.contentType,
testFile.filename,
);
// return the id of the uploaded file
return {
info: {
message: "File uploaded successfully.",
result: ISdStargateGetDataResultEnum.SUCCESS,
count: 1,
},
asset: {
id: fileData.id,
},
};
}
}
// if we cannot handle the request, return a "nothing" response
return {
info: {
message: "No data available.",
result: ISdStargateGetDataResultEnum.NOTHING,
count: 0,
},
};
},
[],
);
// example handler for the EXPORT FILE command
const exportFileCommandHandler = useCallback(
async (
{parameters, export: {id, index}}: ISdStargateExportFileCommandDto,
{config, session}: SessionData,
): Promise<ISdStargateExportFileReplyDto> => {
// get the definition of the export which should be downloaded
const exportDef = session.exports![id];
if (exportDef.type !== ResExportDefinitionType.DOWNLOAD)
return {
info: {
message: "Export is not of type DOWNLOAD.",
result: ISdStargateExportFileResultEnum.NOTHING,
},
};
// request the export
const {
data: {exports: exportResults},
} = await new ExportApi(config).computeExports(session.sessionId, {
parameters,
exports: [id],
});
const exportResult = exportResults![id] as ResExport;
if (
exportResult.status_collect !== ResComputationStatus.SUCCESS ||
exportResult.status_computation !== ResComputationStatus.SUCCESS
)
return {
info: {
message: "Export computation was not successful.",
result: ISdStargateExportFileResultEnum.NOTHING,
},
};
// fetch the exported file
const {href, size} = exportResult.content![index];
await fetchFileWithToken(
href,
exportResult.filename!,
config.accessToken as string,
);
return {
info: {
message: `File ${exportResult.filename} downloaded successfully (${size} bytes).`,
result: ISdStargateExportFileResultEnum.SUCCESS,
},
};
},
[],
);
return {getDataCommandHandler, exportFileCommandHandler, supportedData};
}