-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
220 lines (195 loc) · 6.44 KB
/
index.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
/////////////////////////////////////////////////////////////////////
// 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.
/////////////////////////////////////////////////////////////////////
var express = require('express');
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var async = require('async');
var unirest = require('unirest');
var ejs = require('ejs');
var app = express();
app.use(bodyParser.json ());
app.use(express.static (__dirname + '/www'));
app.use(favicon (__dirname + '/www/images/favicon.ico'));
app.set('view engine', 'ejs');
var client_id = 'QQU4ihGdLoZEfLJWj9cbZaQkGy8Ha2zx';
var client_secret = 'dVS1qAC5px2FPwbv';
var access_token = '';
var redirect_uri = 'http://localhost:3000/api/forge/callback/oauth';
var scope = ['data:read', 'data:write'];
var BASE_ENDPOINT = 'https://developer.api.autodesk.com/photo-to-3d/v1';
var ClientOAuth2 = require('client-oauth2');
var recapAuth = new ClientOAuth2 ({
clientId : client_id,
clientSecret : client_secret,
accessTokenUri : 'https://developer.api.autodesk.com/authentication/v1/gettoken',
authorizationUri : 'https://developer.api.autodesk.com/authentication/v1/authorize',
authorizationGrants : [ 'code' ],
redirectUri : redirect_uri,
scopes : scope
});
app.get('/auth', function (req, res) {
var uri = recapAuth.code.getUri ();
console.log ('redirection: ' + uri);
res.redirect(uri);
});
app.get('/api/forge/callback/oauth', function (req, res) {
recapAuth.code.getToken (req.url)
.then (function (token) {
console.log ('token: ');
console.log (token);
access_token = token.accessToken;
res.redirect ('/app');
});
});
app.get ('/app', function (req, res) {
var self = this;
var endpoint = BASE_ENDPOINT + '/service/date';
unirest.get (endpoint)
.header ('Accept', 'application/json')
.header ('Content-Type', 'application/json')
.header ('Authorization', 'Bearer ' + access_token)
.end (function (response) {
try {
if ( response.statusCode != 200 )
throw response;
var json = response.body;
var obj = {
'dt': json.date
};
res.render ('explore', obj);
} catch ( err ) {
console.log(err);
console.log (response.code + ' - ' + response.body);
res.status (500).end ();
}
});
});
app.post ('/app/createscene', function (req, res) {
var self = this;
var sceneName = req.body.name;
var endpoint = BASE_ENDPOINT + '/photoscene';
unirest.post (endpoint)
.header ('Accept', 'application/json')
.header ('Authorization', 'Bearer ' + access_token)
.send ({
'scenename': sceneName,
'format': 'rcm'
})
.end (function (response) {
try {
if ( response.statusCode != 200 )
throw response;
var json = response.body;
var obj = {
'id': json.Photoscene.photosceneid
};
res.json (obj);
} catch ( err ) {
console.log (response.code + ' - ' + response.body);
res.status (500).end ();
}
});
});
app.post ('/app/post', function (req, res) {
var self = this;
var sceneId = req.body.id;
var urlArray = [
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1158.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1159.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1160.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1162.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1163.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1164.JPG',
'https://s3.amazonaws.com/adsk-recap-public/forge/lion/DSC_1165.JPG'
];
console.log("Posting photos...")
var endpoint = BASE_ENDPOINT + '/file';
unirest.post (endpoint)
.header ('Accept', 'application/json')
.header ('Authorization', 'Bearer ' + access_token)
.send ({
'photosceneid': sceneId,
'type': 'image',
'file[0]': urlArray [0],
'file[1]': urlArray [1],
'file[2]': urlArray [2],
'file[3]': urlArray [3],
'file[4]': urlArray [4],
'file[5]': urlArray [5],
'file[6]': urlArray [6]
})
.end (function (response) {
try {
if ( response.statusCode != 200 )
throw response;
var json = response.body;
var obj = {
'count': json.Files.file.length
};
res.json (obj);
} catch ( err ) {
console.log (response.code + ' - ' + response.body);
res.status (500).end ();
}
});
});
app.post ('/app/launch', function (req, res) {
var self = this;
var sceneId = req.body.id;
var endpoint = BASE_ENDPOINT + '/photoscene/' + sceneId;
unirest.post (endpoint)
.header ('Accept', 'application/json')
.header ('Authorization', 'Bearer ' + access_token)
.send ()
.end (function (response) {
try {
if ( response.statusCode != 200 )
throw response;
var obj = {
'ok': 'launched'
};
res.json (obj);
} catch ( err ) {
console.log (response.code + ' - ' + response.body);
res.status (500).end ();
}
});
});
app.post ('/app/results', function (req, res) {
var self = this;
var sceneId = req.body.id;
var endpoint = BASE_ENDPOINT + '/photoscene/' + sceneId + '?format=rcm';
unirest.get (endpoint)
.header ('Accept', 'application/json')
.header ('Authorization', 'Bearer ' + access_token)
.send ()
.end (function (response) {
try {
if ( response.statusCode != 200 )
throw response;
var json = response.body;
res.json (json);
} catch ( err ) {
console.log (response.code + ' - ' + response.body);
res.status(500).end();
}
});
});
app.set ('port', process.env.PORT || 3000);
var server = app.listen (app.get ('port'), function () {
console.log ('Server listening on port ' + server.address ().port);
});