-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
333 lines (288 loc) · 9.41 KB
/
demo.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as fs from "fs";
import { Readable } from "stream";
import { Server } from "http";
import express from "express";
import { getOAuth2AccessToken } from "./utils";
import net from "net";
import path from "path";
type AssetResponse = {
uploadLink: string;
finishFileUploadLink: string;
cancelFileUploadLink: string;
assetId: string;
};
type BimCloudAsset = {
id: string;
operations: BimCloudOperation[];
};
type BimCloudOperation = {
id: string;
type: string;
status: "Pending" | "Started" | "Finished" | "Failed" | "Canceled";
};
// Please provide your client id and client secret for this demo
const clientId = "";
const clientSecret = "";
const identityTokenUrl = "https://identity-dev.dangl-it.com/connect/token";
const bimcloudBaseUrl = "https://bimcloud-dev.dangl-it.com";
const sourceBimFilePath = "IfcDuplexHouse.ifc";
let accessToken: string;
async function launchDemo(): Promise<void> {
if (!clientId || !clientSecret) {
throw new Error(
"Please provide your client id and client secret for this demo"
);
}
// First we're getting a token
accessToken = await getOAuth2AccessToken(
clientId,
clientSecret,
identityTokenUrl
);
// Then we're creating an asset
const assetResponse = await createAsset();
// Then we upload the source IFC file
await uploadIfcFile(assetResponse);
// And announce that we've finished the file upload
await announceFileUploadFinished(assetResponse);
// Then we get the operations
const assetData = await getBimCloudAssetData(assetResponse.assetId);
// We'll wait until each operation is finished simultaneously
// and download the results
let geometryFileName;
let structureFileName;
await Promise.all(
assetData.operations.map(async (operation) => {
const fileName = await waitUntilOperationFinishedAndDownloadAsset(
assetResponse.assetId,
operation
);
if (operation.type === "WexbimGeometryConversion") {
geometryFileName = fileName;
} else if (operation.type === "StructureConversion") {
structureFileName = fileName;
}
})
);
// Then we'll launch a small web server that shows a viewer
await launchServer(geometryFileName || "", structureFileName || "");
}
async function createAsset(): Promise<AssetResponse> {
// We're sending an object like this:
// {
// "fileName": "string"
// "sizeInBytes": 0
// }
// to https://bimcloud-dev.dangl-it.com/api/assets
// and we're taking the information from the file 'IfcDuplexHouse.ifc'
// In the result, BIMCloud will create an asset for us and will wait
// until we've uploaded the file to the storage. After we've done that,
// BIMCloud will be able to start processing the model.
var fileInfo = fs.statSync(sourceBimFilePath);
const assetResponse = await fetch(bimcloudBaseUrl + "/api/assets", {
method: "POST",
headers: {
Authorization: "Bearer " + accessToken,
"Content-Type": "application/json",
},
body: JSON.stringify({
fileName: sourceBimFilePath,
// We're announcing the file size in advance to BIMCloud
sizeInBytes: fileInfo.size,
}),
});
const assetResponseJson = await assetResponse.json();
return assetResponseJson;
}
async function uploadIfcFile(assetResponse: AssetResponse): Promise<void> {
// We're using the 'uploadLink' property from the assetResponse
// to upload the file 'IfcDuplexHouse.ifc' to an Azure blob storage container
// Unfortunately, we need to specify the header 'x-ms-blob-type' with the value 'BlockBlob'
// since that's requireds by Azure, see https://stackoverflow.com/questions/37824136/put-on-sas-blob-url-without-specifying-x-ms-blob-type-header
const fileInfo = fs.statSync(sourceBimFilePath);
const fileBuffer = fs.readFileSync(sourceBimFilePath);
const blob = new Blob([fileBuffer]);
const response = await fetch(assetResponse.uploadLink, {
method: "PUT",
body: blob,
headers: {
"Content-Length": fileInfo.size.toString(),
"x-ms-blob-type": "BlockBlob",
},
});
if (!response.ok) {
throw new Error(`Failed to upload file: ${response.statusText}`);
}
console.log("File uploaded successfully");
}
async function announceFileUploadFinished(
assetResponse: AssetResponse
): Promise<void> {
// We're sending a PUT request to the 'finishFileUploadLink' property
// of the assetResponse
await fetch(assetResponse.finishFileUploadLink, {
method: "PUT",
headers: {
Authorization: "Bearer " + accessToken,
},
});
}
async function getBimCloudAssetData(assetId: string): Promise<BimCloudAsset> {
const response = await fetch(bimcloudBaseUrl + "/api/assets/" + assetId, {
method: "GET",
headers: {
Authorization: "Bearer " + accessToken,
},
});
return await response.json();
}
async function waitUntilOperationFinishedAndDownloadAsset(
assetId: string,
operation: BimCloudOperation
): Promise<string> {
// We're sending a GET request to https://bimcloud-dev.dangl-it.com/api/assets/{assetId}/operations/{operationId}
// and checking the 'status' property
// If it's 'Finished', we're sending a GET request to the 'downloadLink' property
// and downloading the file
// If it's 'Failed', we're logging an error to the console
// We're also waiting for a few seconds between each poll
let latestStatus = operation.status;
let hasFinished = false;
console.log(
"Initial status for operation type " +
operation.type +
" is " +
operation.status
);
let fileName;
while (!hasFinished) {
const response = await fetch(
bimcloudBaseUrl +
"/api/assets/" +
assetId +
"/operations/" +
operation.id,
{
method: "GET",
headers: {
Authorization: "Bearer " + accessToken,
},
}
);
const operationData = (await response.json()) as BimCloudOperation;
// If the status has changed, we're logging to the console
if (latestStatus !== operationData.status) {
console.log(
"Operation status changed to " +
operationData.status +
" for operation type " +
operationData.type
);
latestStatus = operationData.status;
}
// If the operation is finished, we're downloading the asset
if (operationData.status === "Finished") {
hasFinished = true;
console.log(
"Operation finished for type " + operation.type + " , downloading asset"
);
const downloadInstructionsResponse = await fetch(
bimcloudBaseUrl +
"/api/assets/" +
assetId +
"/operations/" +
operation.id +
"/content",
{
method: "GET",
headers: {
Authorization: "Bearer " + accessToken,
},
}
);
const downloadLink = (await downloadInstructionsResponse.json())
.downloadLink;
// We're downloading the asset
// This is a simple GET request to the 'downloadLink' property
// of the operation response
// We're saving the file to the current directory
const downloadResponse = await fetch(downloadLink);
fileName = downloadResponse.headers
.get("Content-Disposition")
?.split(";")
.find((part) => part.includes("filename="))
?.split("=")[1]
.replace(/"/g, "");
if (downloadResponse.ok && downloadResponse.body) {
const streamWriter = fs.createWriteStream(
fileName || "downloadedAsset_" + operation.type
);
Readable.fromWeb(downloadResponse.body as any).pipe(streamWriter);
}
} else if (operationData.status === "Failed") {
hasFinished = true;
console.log("Operation failed for type " + operation.type);
} else {
// If the operation is still pending, we're waiting for a few seconds
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
return fileName || "";
}
async function launchServer(
geometryFileName: string,
structureFileName: string
): Promise<void> {
const app = express();
const port = await getEmptyPort();
let server: Server;
// Serve the static HTML file
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "demo.html"));
});
app.get("/dependencies.js", (req, res) => {
res.sendFile(path.join(__dirname, "node_modules/@xbim/viewer/index.js"));
});
app.get("/model.wexbim", (req, res) => {
res.sendFile(path.join(__dirname, geometryFileName));
});
app.get("/model.json", (req, res) => {
res.sendFile(path.join(__dirname, structureFileName));
});
// Route to close the server
app.post("/stop-server", (req, res) => {
res.send("Server is closing...");
server.close(() => {
console.log("Server closed");
});
});
// Start the server
server = app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
// Return a promise that resolves when the server is closed
return new Promise<void>((resolve) => {
server.on("close", () => {
resolve();
});
});
}
async function getEmptyPort(): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.listen(0, () => {
const port = (server.address() as net.AddressInfo).port;
server.close((err) => {
if (err) {
reject(err);
} else {
resolve(port);
}
});
});
server.on("error", (err) => {
reject(err);
});
});
}
launchDemo();