const express = require('express'); // For web server
const bodyParser = require('body-parser'); // Receive JSON format
const querystring = require('querystring');
// Set up Express web server
var app = express();
app.use(bodyParser.json());
// this line say of server, that we run server.js he open html file from public directory
app.use(express.static(path.join(__dirname, 'public'))); // it was static(__ + 'public or www')
// let's importing config.js file where are the configuration for dotenv/env and credentials_id/secret/url/PORT and scopes
const config = require('./config');
const PORT = config.credentials.PORT; // import from bim start.js
const scopes = 'data:read data:write data:create bucket:create bucket:read';
var indRouter = require('./routes/indRouter');
indRouter(app);
app.use((err, req, res, next) => {
console.error(err);
res.status(err.statusCode).json(err);
});
// This is for web server to start listening to port 3000
app.listen(PORT, () => {
if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) {
console.log('*****************\nWARNING: Client ID & Client Secret not defined as environment variables.\n*****************');
}
console.log(`Server listening on port ${PORT}`);
});
var axiosRequest = require('./axiosRequest');
var multer = require('multer'); // To handle file upload
var upload = multer({ dest: 'tmp/' }); // Save file into local /tmp folder
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');
};
module.exports = function(app) {
app.get('/api/forge/oauth', function (req, res) {
axiosRequest.oauth(req, res);
});
app.get('/api/forge/datamanagement/bucket/create', function (req, res) {
axiosRequest.bucketCreate(req, res);
});
app.get('/api/forge/oauth/public', function (req, res) {
// Limit public token to Viewer read only
axiosRequest.publ(req, res);
});
app.get('/api/forge/datamanagement/bucket/detail', function (req, res) {
axiosRequest.detail(req, res);
});
app.post('/api/forge/datamanagement/bucket/upload', upload.single('fileToUpload'), function (req, res) {
axiosRequest.upload(req, res);
});
app.get('/api/forge/modelderivative/:urn', function (req, res) {
axiosRequest.mod(req, res);
});
};
const Axios = require('axios');
const querystring = require('querystring');
const config = require('../config');
// Prefix with your ID so the bucket key is unique across all buckets on all other accounts
const bucketKey = config.credentials.client_id.toLowerCase() + '_my_first_full_viewer_bucket';
const policyKey = 'transient'; // Expires in 24hr
function oauth(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: config.credentials.client_id,
client_secret: config.credentials.client_secret,
grant_type: 'client_credentials',
scope: config.scopes.internal
})
})
.then(function (response) {
// Success
var access_token = response.data.access_token;
Axios.defaults.headers.common['Authorization'] = 'Bearer ' + access_token;
//console.log(response);
res.redirect('/api/forge/datamanagement/bucket/create');
})
.catch(function (error) {
// Failed
console.log(error);
res.send('Failed to authenticate');
});
}
function bucketCreate(req, res) {
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');
});
}
function publ(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: config.credentials.client_id,
client_secret: config.credentials.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);
});
}
function detail(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');
});
}
function upload(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');
});
});
}
function mod(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.');
});
}
exports.oauth = oauth;
exports.bucketCreate = bucketCreate;
exports.publ = publ;
exports.detail = detail;
exports.upload = upload;
exports.mod = mod;
module.exports = () => {...
Downloading binary from https://github.com/zhangyuanwei/node-images/releases/download/v3.0.1/win32-ia32-57_binding.node
Download complete
npm install https://github.com/...