forked from Autodesk-Forge/recap-walkthrough-photo.to.3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
270 lines (257 loc) · 11.1 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
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
/////////////////////////////////////////////////////////////////////
// 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
// 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';
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.send('<p>Authentication success!</p><a href="/api/forge/recap/photoscene/add">Add a photoscene</a>');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to authenticate');
});
});
// Route /api/forge/recap/photoscene/add
// Creates and initializes a photoscene for reconstruction.
app.get('/api/forge/recap/photoscene/add', function (req, res) {
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
data: querystring.stringify({
scenename: 'myscenename',
format: 'rcm'
})
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
var photosceneId = response.data.Photoscene.photosceneid;
var nextLink = '/api/forge/recap/photoscene/upload?photosceneid=' + photosceneId;
res.send('<p>Photoscene added!</p><a href="' + nextLink + '">Upload files to photoscene</a>');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to create a photoscene');
});
});
// Route /api/forge/recap/photoscene/upload
// Adds one or more files to a photoscene.
app.get('/api/forge/recap/photoscene/upload', function (req, res) {
var photosceneId = req.query.photosceneid;
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/file',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + access_token
},
data: querystring.stringify({
photosceneid: photosceneId,
type: 'image',
'file[0]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1158.JPG',
'file[1]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1159.JPG',
'file[2]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1160.JPG',
'file[3]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1162.JPG',
'file[4]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1163.JPG',
'file[5]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1164.JPG',
'file[6]': 'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1165.JPG'
})
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
console.log(JSON.stringify(response.data.Files));
var nextLink = '/api/forge/recap/photoscene/process?photosceneid=' + photosceneId;
res.send('<p>Files added to photoscene!</p><a href="' + nextLink + '">Begin processing photoscene</a>');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to upload files to photoscene');
});
});
// Route /api/forge/recap/photoscene/process
// Starts photoscene processing.
app.get('/api/forge/recap/photoscene/process', function (req, res) {
var photosceneId = req.query.photosceneid;
Axios({
method: 'POST',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/' + photosceneId,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + access_token
}
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
var nextLink = '/api/forge/recap/photoscene/checkprogress?photosceneid=' + photosceneId;
res.send('<p>Photoscene is being processed!</p><a href="' + nextLink + '">Check progress of photoscene</a>');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to process files in photoscene');
});
});
// Route /api/forge/recap/photoscene/checkprogress
// Returns the processing progress and status of a photoscene.
app.get('/api/forge/recap/photoscene/checkprogress', function (req, res) {
var photosceneId = req.query.photosceneid;
Axios({
method: 'GET',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/' + photosceneId + '/progress',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + access_token
}
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
if (response.data.Photoscene && response.data.Photoscene.progressmsg == 'DONE') {
var nextLink = '/api/forge/recap/photoscene/result?photosceneid=' + photosceneId;
res.send('<p>Photoscene process is complete!</p><a href="' + nextLink + '">View result of photoscene</a>');
} else {
var nextLink = '/api/forge/recap/photoscene/delete?photosceneid=' + photosceneId;
res.send('<p>Photoscene is not ready, this may take a while. Try refreshing <a href="/api/forge/recap/photoscene/checkprogress?photosceneid=' + photosceneId + '">this page</a>. Progress: ' + response.data.Photoscene.progress + '%...</p>');
}
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to check progress of photoscene');
});
});
// Route /api/forge/recap/photoscene/result
// Returns a time-limited HTTPS link to an output file of the specified format.
app.get('/api/forge/recap/photoscene/result', function (req, res) {
var photosceneId = req.query.photosceneid;
Axios({
method: 'GET',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/' + photosceneId + '?format=rcm',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + access_token
}
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
if (response.data.Photoscene && response.data.Photoscene.progressmsg == 'DONE') {
var nextLink = '/api/forge/recap/photoscene/delete?photosceneid=' + photosceneId;
res.send('<p>Success! This is the scene link:</p><p>' + response.data.Photoscene.scenelink + '</p>'
+ 'Would you like to <a href="' + nextLink + '">delete photoscene</a>?');
} else {
res.send('Photoscene is not ready. Try refreshing <a href="/api/forge/recap/photoscene/checkprogress?photosceneid=' + photosceneId + '">this page</a>. Progress: ' + response.data.Photoscene.progress + '%...');
}
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to get result of photoscene');
});
});
// Route /api/forge/recap/photoscene/delete
// Deletes a photoscene and its associated assets (images, output files, ...).
app.get('/api/forge/recap/photoscene/delete', function (req, res) {
var photosceneId = req.query.photosceneid;
Axios({
method: 'DELETE',
url: 'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/' + photosceneId,
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + access_token
}
})
.then(function (response) {
// Success
console.log(response);
if (response.data.Error) {
res.send(response.data.Error.msg);
}
res.send('<p>Photoscene deleted!</p>');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to delete photoscene');
});
});