/
server.js
422 lines (381 loc) · 14.9 KB
/
server.js
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
BIM360 Zip extractor and file transfer for BIM360 and large zip files
Transfer only the minimal set of data required, by taking advantage of the zip structure and GET RANGE network requests:
https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html
*/
const fetch = require('node-fetch');
const StreamZip = require('node-stream-zip');
const fs = require('fs');
const path = require('path');
const fastify = require('fastify')({ logger: true })
let ze = null, bm = null;
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'ui'),
prefix: '/', // optional: default '/'
})
fastify.get('/dir', async (request, reply) => {
const dirlist = fs.readdirSync('.', {withFileTypes: true})
.filter(item => !item.isDirectory())
.map(item => item.name);
return dirlist;
});
function setCORS(reply) {
reply.header("Access-Control-Allow-Origin", "*");
reply.header("Access-Control-Allow-Methods", "POST");
}
// INPUT: projectID, folderID, AccessToken
// OUTPUT: list of BIM360 files in the folder
fastify.get('/bim/list', async (request, reply) => {
if (!request.query.project) return "INPUT: project, folder, token";
setCORS(reply);
bm = new BIM360utils(request.query.project, request.query.folder, request.query.token);
const res = await bm.getFolderContents();
return res;
});
// INPUT: zipURL, size (file-size)
// OUTPUT: list of zip file contents
fastify.get('/listZipContents', async (request, reply) => {
if (!request.query.zipURL) return "INPUT: zipURL, size (file-size)";
setCORS(reply);
try {
ze = new netZipExtract(request.query.zipURL, request.query.size, bm.token);
const contents = await ze.getContents();
return contents;
} catch(err) { return err }
})
// INPUT: filename
// OUTPUT: status of result (timeout after 30seconds)
fastify.get('/transfer', async (request, reply) => {
if (!ze && !bm) return {status:`not-ready. Use 'listcontents' first`};
setCORS(reply);
if (!request.query.filename) return "INPUT: filename";
if (request.query.destFolder) bm.folder = request.query.destFolder;
if (request.query.destProject) bm.project = request.query.destProject;
try {
const filename = request.query.filename;
const resObj = await bm.createEmptyFile(filename);
const status = await ze.extractFile(filename, resObj.destURL);
let ress = null;
if (request.query.lineage)
ress = await bm.bumpVersion(resObj, request.query.lineage);
else
ress = await bm.createVersion(resObj);
} catch(err) {
return {status: err};
}
})
// INPUT: filename
// OUTPUT: status of job
fastify.get('/status', async (request, reply) => {
if (!request.query.filename) return "INPUT: filename";
setCORS(reply);
const id = request.query.filename;
if (!id) return;
return (id) ? `{ "${id}" : "${ze.session[id]}" }` : "missing filename= parameter"
})
fastify.listen(process.env.PORT || 3000, "0.0.0.0", (err, address) => {
if (err) throw err
fastify.log.info(`server listening on ${address}`)
})
/*
fastify.get('/bim/empty', async (request, reply) => {
const res = await bm.createEmptyFile(request.query.filename);
return res;
})
fastify.get('/bim/upload', async (request, reply) => {
const res = await bm.createVersion();
return res;
})
fastify.get('/extract2', async (request, reply) => {
try {
if (!ze) return {status:`not-ready. Use 'listcontents' first`};
const status = await ze.extractFile(request.query.outfile, request.query.destURL);
return {status: status};
} catch(err) {
return {status: err};
}
})
async _createTempZip2(offset, size) {
const tmpfile = fs.openSync(this.tmpFn, 'w');
const chunksize = 16 * 1024; // only need 16k bytes of data
await this._fetchWrite(tmpfile, 0, chunksize); // fetch/write header
await this._fetchWrite(tmpfile, this.fileLength - chunksize, chunksize); // fetch/write footer
fs.closeSync(tmpfile);
if (offset && size) {
// Write bytes to file
const zipHeaderOffset = 128;
const res = await fetch( this.URL, { headers: {
'range': `bytes=${offset}-${offset+size+zipHeaderOffset}`,
'Authorization': `Bearer ${this.token}`
}});
const dest = fs.createWriteStream(this.tmpFn, {flags:'a+', start:offset, highWaterMark: 2 * 1024 * 1024});
await new Promise((resolve) => {
res.body.pipe(dest);
dest.on("finish", () => resolve());
})
}
}
*/
String.prototype.hashCode = function() {
var hash = 0;
if (this.length == 0) {
return hash;
}
for (var i = 0; i < this.length; i++) {
var char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
//
// Main
//
class netZipExtract {
constructor(URN, fileLength, token) {
this.URL = `${URN}`;
this.token = token;
this.fileLength = fileLength;
this.tmpFn = `tmp-${URN.hashCode()}.zip`;
this.session = [];
}
//
// fetch a chunk of bytes from BIM360 and write to 'temp' file on fs
//
async _fetchWrite( fd, offset, length ) {
const res = await fetch( this.URL, { headers: {
'range': `bytes=${offset}-${offset+length}`,
'Authorization': `Bearer ${this.token}`
}});
if (res.status != 206)
throw(`error:${res.statusText}, bytes=${offset}-${offset+length}`)
const buff = await res.buffer();
fs.writeSync(fd, buff, 0, buff.length, offset);
return res.status;
}
async _createTempZip(offset, size) {
const tmpfile = fs.openSync(this.tmpFn, 'w');
const chunksize = 4 * 1024; // only need 16k bytes of data
await this._fetchWrite(tmpfile, 0, chunksize); // fetch/write header
await this._fetchWrite(tmpfile, this.fileLength - chunksize, chunksize); // fetch/write footer
const zipHeaderOffset = 128;
if (size)
await this._fetchWrite(tmpfile, offset, size + zipHeaderOffset); // fetch/write our filename within the zip
fs.closeSync(tmpfile);
}
//
// get directory-list inside zip (that's hosted on bim360)
//
async getContents() { return new Promise(async resolve => {
this._log(`fetch/extract Contents: ${this.URL} size ${this.fileLength}...`)
try {
await this._createTempZip();
// now, extract content directory
this.zip = new StreamZip({ file: this.tmpFn, storeEntries: true });
}
catch(err) {resolve(err);return}
this.zip.on('error', (err) => { resolve(`error:${err}`) });
this.zip.on('ready', () => {
this.entries = this.zip.entries();
this.zip.close();
resolve(this.entries);
});
})};
//
// extract a file from inside the zip file, then post it to bim360 destURL
//
async extractFile( filename, destURL ) { return new Promise(async resolve => {
// get filename's offset and byte-length, located inside the zip file
if (!this.entries) return;
const offset = this.entries[filename].offset;
const size = this.entries[filename].compressedSize;
this.filename = filename;
// now, fetch the exact bytes from bim360, and write to our temp file
const MBytes = Math.round(size / 100000) / 10;
this._log(`(downloading ${MBytes} MB) ${filename} , zip offset: ${offset}`)
await this._createTempZip(offset, size);
// now, use StreamZip to do it's magic.
this._log(`Extracting ${filename} from ${this.tmpFn}...`)
this.zip = new StreamZip({ file: this.tmpFn, storeEntries: true });
this.zip.on('error', err => { throw(`error:${err}`) });
this.zip.on('ready', async () => {
this.entries = this.zip.entries();
this.zip.extract( filename, filename, async err => {
//if (err) throw(`Zip-Extract error: ${err}`);
this._log(`Uploading (0 KB) ${filename} to ${destURL}...`)
// upload file to forge signedURL
let bytes = size;
const stream = fs.createReadStream(filename,{highWaterMark: 1024 * 1024 });
stream.on('data', b => {
this._log(`Upload progress ${96-Math.round((bytes/size)*96)}%`)
bytes-=b.length;
});
const rs2 = await fetch(destURL, {
method: 'PUT',
headers: { Authorization: `Bearer ${this.token}` },
body: stream });
const res2 = await rs2.json();
console.log(res2);
// this.zip.close(); // don't close zip before upload complete. this seems buggy for slow networks
this._log(`Upload complete: ${filename} to ${destURL}.${JSON.stringify(res2)}`)
resolve(`Upload complete: ${filename} to ${destURL}. ${res2}`)
});
});
})}
_log(m) {
console.log(m);
if (this.filename) this.session[this.filename] = m;
}
}
class BIM360utils {
constructor(project, folder, token) {
this.folder = folder;
this.project = project;
this.token = token;
this.filename = "";
this.displayName = "";
this.objid = "";
}
async getFolderContents() {
const res = await fetch( `https://developer.api.autodesk.com/data/v1/projects/${this.project}/folders/urn:adsk.wipprod:fs.folder:${this.folder}/contents`,
{ headers: { Authorization: `Bearer ${this.token}` }});
const jres = await res.json();
if (!jres.included) return jres;
let lista = jres.included.map(i => { return ({
filename: i.attributes.displayName,
size:i.attributes.storageSize,
lastModifiedTime: i.attributes.lastModifiedTime,
lineage: i.relationships.item.data.id,
version: i.attributes.versionNumber,
datetime: (new Date(i.attributes.lastModifiedTime)).toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' }).replace(/ /g, '-'),
url: `https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/${i.relationships.storage.data.id.split('/')[1]}`
})});
lista.sort((a,b)=>{return (b.lastModifiedTime < a.lastModifiedTime) ? -1:1});
return lista;
}
// token2 (optional) is a second ACCESS_TOKEN of a 2nd BIM360 HUB.
// Use this to save the resulting output file to a secondary BIM360 Hub
async createEmptyFile(filename) {
//const id = `${Math.random()}`.slice(2,5);
this.filename = filename;//`_${filename.slice(0,-4)}_${id}${filename.slice(-4)}`;
const res = await fetch( `https://developer.api.autodesk.com/data/v1/projects/${this.project}/storage`,
{
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept : 'application/vnd.api+json',
Authorization: `Bearer ${this.token}`
},
body: `{
"jsonapi": { "version": "1.0" },
"data": {
"type": "objects",
"attributes": {
"name": "${this.filename}"
},
"relationships": {
"target": {
"data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:${this.folder}" }
}
}
}
}`
});
const obj = await res.json();
this.objid = obj.data.id.split("/")[1];
return {
objid: this.objid,
filename: this.filename,
destURL: `https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/${this.objid}`
}
}
async createVersion( ob ) {
const res = await fetch( `https://developer.api.autodesk.com/data/v1/projects/${this.project}/items`,
{
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept : 'application/vnd.api+json',
Authorization: `Bearer ${this.token}`
},
body: `{
"jsonapi": { "version": "1.0" },
"data": {
"type": "items",
"attributes": {
"displayName": "${ob.filename}",
"extension": {
"type": "items:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"tip": {
"data": {
"type": "versions", "id": "1"
}
},
"parent": {
"data": {
"type": "folders",
"id": "urn:adsk.wipprod:fs.folder:${this.folder}"
}
}
}
},
"included": [
{
"type": "versions",
"id": "1",
"attributes": {
"name": "${ob.filename}",
"extension": {
"type": "versions:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"storage": {
"data": {
"type": "objects",
"id": "urn:adsk.objects:os.object:wip.dm.prod/${ob.objid}"
}
}
}
}
]
}`
});
const obj = await res.json();
return obj;
}
async getTip( ob, lineageId ) {
}
async bumpVersion( ob, lineageId ) {
const res = await fetch( `https://developer.api.autodesk.com/data/v1/projects/${this.project}/versions`,
{
method: 'POST',
headers: {
'Content-Type': 'application/vnd.api+json',
Accept : 'application/vnd.api+json',
Authorization: `Bearer ${this.token}`
},
body: `{
"jsonapi": { "version": "1.0" },
"data": {
"type": "versions",
"attributes": {
"name": "${ob.filename}",
"extension": { "type": "versions:autodesk.bim360:File", "version": "1.0"}
},
"relationships": {
"item": { "data": { "type": "items", "id": "${lineageId}" } },
"storage": { "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/${ob.objid}" } }
}
}
}`
});
const obj = await res.json();
return obj;
}
}