-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
238 lines (225 loc) · 8.68 KB
/
start.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
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
//
// 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.
/////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------
// These packages are included in package.json.
// Run `npm install` to install them.
// 'path' is part of Node.js and thus not inside package.json.
//-------------------------------------------------------------------
var express = require('express'); // For web server
var Axios = require('axios'); // A Promised base http client
var bodyParser = require('body-parser'); // Receive JSON format
var dotenv = require('dotenv').config();
// Set up Express web server
var app = express();
app.use(bodyParser.json());
app.use(express.static(__dirname + '/www'));
// This is for web server to start listening to port 3000
app.set('port', 3000);
var server = app.listen(app.get('port'), function () {
console.log('Server listening on port ' + server.address().port);
});
//-------------------------------------------------------------------
// Configuration for your Forge account
// Initialize the 2-legged OAuth2 client, and
// set specific scopes
//-------------------------------------------------------------------
var FORGE_CLIENT_ID = process.env.FORGE_CLIENT_ID;
var FORGE_CLIENT_SECRET = process.env.FORGE_CLIENT_SECRET;
var access_token = '';
var scopes = 'data:read data:write data:create bucket:create bucket:read';
const querystring = require('querystring');
// // Route /api/forge/oauth
app.get('/api/forge/oauth', function (req, res) {
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: querystring.stringify({
client_id: FORGE_CLIENT_ID,
client_secret: FORGE_CLIENT_SECRET,
grant_type: 'client_credentials',
scope: scopes
})
})
.then(function (response) {
// Success
access_token = response.data.access_token;
console.log(response);
res.redirect('/api/forge/datamanagement/bucket/create');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to authenticate');
});
});
// Route /api/forge/oauth/public
app.get('/api/forge/oauth/public', function (req, res) {
// Limit public token to Viewer read only
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
data: querystring.stringify({
client_id: FORGE_CLIENT_ID,
client_secret: FORGE_CLIENT_SECRET,
grant_type: 'client_credentials',
scope: 'viewables:read'
})
})
.then(function (response) {
// Success
console.log(response);
res.json({ access_token: response.data.access_token, expires_in: response.data.expires_in });
})
.catch(function (error) {
// Failed
console.log(error);
res.status(500).json(error);
});
});
// Buckey key and Policy Key for OSS
const bucketKey = '2f7ffcd77acc436fa2a082a6c18e1b63' + '_appro'; // Prefix with your ID so the bucket key is unique across all buckets on all other accounts
const policyKey = 'transient'; // Expires in 24hr
// Route /api/forge/datamanagement/bucket/create
app.get('/api/forge/datamanagement/bucket/create', function (req, res) {
// Create an application shared bucket using access token from previous route
// We will use this bucket for storing all files in this tutorial
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/oss/v2/buckets',
headers: {
'content-type': 'application/json',
Authorization: 'Bearer ' + access_token
},
data: JSON.stringify({
'bucketKey': bucketKey,
'policyKey': policyKey
})
})
.then(function (response) {
// Success
console.log(response);
res.redirect('/api/forge/datamanagement/bucket/detail');
})
.catch(function (error) {
if (error.response && error.response.status == 409) {
console.log('Bucket already exists, skip creation.');
res.redirect('/api/forge/datamanagement/bucket/detail');
}
// Failed
console.log(error);
res.send('Failed to create a new bucket');
});
});
// Route /api/forge/datamanagement/bucket/detail
app.get('/api/forge/datamanagement/bucket/detail', function (req, res) {
Axios({
method: 'GET',
url: 'https://developer.api.autodesk.com/oss/v2/buckets/' + encodeURIComponent(bucketKey) + '/details',
headers: {
Authorization: 'Bearer ' + access_token
}
})
.then(function (response) {
// Success
console.log(response);
res.redirect('/upload.html');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to verify the new bucket');
});
});
// For converting the source into a Base64-Encoded string
var Buffer = require('buffer').Buffer;
String.prototype.toBase64 = function () {
// Buffer is part of Node.js to enable interaction with octet streams in TCP streams,
// file system operations, and other contexts.
return new Buffer(this).toString('base64');
};
var multer = require('multer'); // To handle file upload
var upload = multer({ dest: 'tmp/' }); // Save file into local /tmp folder
// Route /api/forge/datamanagement/bucket/upload
app.post('/api/forge/datamanagement/bucket/upload', upload.single('fileToUpload'), function (req, res) {
var fs = require('fs'); // Node.js File system for reading files
fs.readFile(req.file.path, function (err, filecontent) {
Axios({
method: 'PUT',
url: 'https://developer.api.autodesk.com/oss/v2/buckets/' + encodeURIComponent(bucketKey) + '/objects/' + encodeURIComponent(req.file.originalname),
headers: {
Authorization: 'Bearer ' + access_token,
'Content-Disposition': req.file.originalname,
'Content-Length': filecontent.length
},
data: filecontent
})
.then(function (response) {
// Success
console.log(response);
var urn = response.data.objectId.toBase64();
res.redirect('/api/forge/modelderivative/' + urn);
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to create a new object in the bucket');
});
});
});
// Route /api/forge/modelderivative
app.get('/api/forge/modelderivative/:urn', function (req, res) {
var urn = req.params.urn;
var format_type = 'svf';
var format_views = ['2d', '3d'];
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/modelderivative/v2/designdata/job',
headers: {
'content-type': 'application/json',
Authorization: 'Bearer ' + access_token
},
data: JSON.stringify({
'input': {
'urn': urn
},
'output': {
'formats': [
{
'type': format_type,
'views': format_views
}
]
}
})
})
.then(function (response) {
// Success
console.log(response);
res.redirect('/viewer.html?urn=' + urn);
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Error at Model Derivative job.');
});
});