-
Notifications
You must be signed in to change notification settings - Fork 24
/
integration.js
198 lines (179 loc) · 5.76 KB
/
integration.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
'use strict'; // http://www.w3schools.com/js/js_strict.asp
// token handling in session
var Credentials = require('./../credentials');
// forge config information, such as client ID and secret
var config = require('./../config');
// web framework
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var request = require('request');
var Encoder = require('node-html-encoder').Encoder;
var encoder = new Encoder('entity');
// forge oAuth package
var forgeSDK = require('forge-apis');
router.post('/api/forge/createFolder', jsonParser, function (req, res) {
var token = new Credentials(req.session);
var forge3legged = new forgeSDK.AuthClientThreeLegged(
config.forge.credentials.client_id,
config.forge.credentials.client_secret,
config.forge.callbackURL,
config.forge.scope,
true);
if (token.getForgeCredentials() === undefined) {
res.status(401).end();
return;
}
var parentFolderHref = req.body.parentFolder;
var folderName = encoder.htmlDecode(req.body.folderName);
var params = parentFolderHref.split('/');
var projectId = params[params.length - 3];
var parentFolderId = params[params.length - 1];
var folders = new forgeSDK.FoldersApi();
folders.getFolderContents(projectId, parentFolderId, {}, forge3legged, token.getForgeCredentials())
.then(function (folderContents) {
for (var f in folderContents.body.data) {
var item = folderContents.body.data[f];
if (item.type === 'folders' && item.attributes.displayName === folderName) {
res.json({folderId: item.links.self.href});
return;
}
}
if (projectId.indexOf('b.') > -1) {
// BIM 360 Docs project
var createFolder = {
url: 'https://developer.api.autodesk.com/data/v1/projects/' + projectId + '/commands',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + token.getForgeCredentials().access_token,
'Content-Type': 'application/vnd.api+json'
},
body: JSON.stringify(createBIM360FolderSpecData(folderName, parentFolderId))
};
request(createFolder, function (error, response) {
if (error) {
if (process.env.CONSOLELOG) console.log(error);
res.status(500).end();
return;
}
if (response.statusCode >= 400) {
res.status(response.statusCode).end();
return;
}
var body = JSON.parse(response.body);
if ((body.errors && body.errors.length > 0) || (body.data && body.data.attributes && body.data.attributes.errors)) {
if (process.env.CONSOLELOG) console.log(body.errors);
res.status(500).end();
return;
}
// create folder for BIM 360 Docs returns ONLY the folder ID, but our UI uses the full Href
res.json({folderId: 'https://developer.api.autodesk.com/data/v1/projects/' + projectId + '/folders/' + body.data.relationships.created.data[0].id});
});
}
else {
// other project type (e.g. BIM 360 Team)
folders.postFolder(projectId, createFolderSpecData(folderName, parentFolderId), forge3legged, token.getForgeCredentials())
.then(function (folder) {
res.json({folderId: folder.body.data.links.self.href});
})
.catch(function (error) {
console.log(error);
res.status(500).end();
})
}
})
.catch(function (error) {
console.log(error);
res.status(500).end();
});
});
function createFolderSpecData(folderName, parentFolderId) {
return {
jsonapi: {
version: "1.0"
},
data: {
type: "folders",
attributes: {
name: folderName,
extension: {
type: "folders:autodesk.core:Folder",
version: "1.0"
}
},
relationships: {
parent: {
data: {
type: "folders",
id: parentFolderId
}
}
}
}
}
}
function createBIM360FolderSpecData(folderName, parentFolderId) {
return {
jsonapi: {
version: "1.0"
},
data: {
type: "commands",
attributes: {
extension: {
type: "commands:autodesk.core:CreateFolder",
version: "1.0.0",
}
},
relationships: {
resources: {
data: [
{
type: "folders",
id: "1"
}
]
}
}
},
included: [{
type: "folders",
id: "1",
attributes: {
name: folderName,
extension: {
type: "folders:autodesk.bim360:Folder",
version: "1.0"
}
},
relationships: {
parent: {
data: {
type: "folders",
id: parentFolderId
}
}
}
}
]
}
}
module.exports = router;