Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
"crypto": "0.0.3",
"es6-promisify": "*",
"express": "*",
"forge-model-derivative": "*",
"forge-oauth2": "*",
"forge-oss": "*",
"express-session": "^1.15.5",
"forge-apis": "^0.4.1",
"gen-uid": "*",
"path": "*",
"serve-favicon": "*"
Expand Down
2 changes: 1 addition & 1 deletion server/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var config ={
// Do not do this in your 'production' application ;)
client_id: '',
client_secret: '',
scope:'data:read data:write data:create bucket:create bucket:read',
scope: ['data:read', 'data:write', 'data:create', 'bucket:create', 'bucket:read'],
grant_type: 'client_credentials'
},
fileResumableChunk: 40, // in Mb
Expand Down
239 changes: 126 additions & 113 deletions server/lmv-projects.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// Copyright (c) Autodesk, Inc. All rights reserved
// Copyright (c) Autodesk, Inc. All rights reserved
//
// Node.js server workflow
// Node.js server workflow
// by Cyrille Fauvel - Autodesk Developer Network (ADN)
// January 2015
//
// Modified by SVNINDIA, Sep 2017
// 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
// 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
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// 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.
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
Expand All @@ -26,131 +26,144 @@ var promisify =require ('es6-promisify') ;
var stat =promisify (fs.stat) ;
var readFile =promisify (fs.readFile) ;
var crypto =require ('crypto') ;
var config =require ('./credentials') ;
var ForgeSDK = require('forge-apis');
var session = require('express-session');

var ForgeOSS =require ('forge-oss') ;
// var ForgeOSS = ForgeSDK;
// var ForgeModelDerivative = ForgeSDK;
// var ForgeOSS =require ('forge-oss') ;
var ForgeModelDerivative =require ('forge-model-derivative') ;

var config =require ('./credentials') ;

var ossBuckets =new ForgeOSS.BucketsApi () ;
var ossObjects =new ForgeOSS.ObjectsApi () ;
var md =new ForgeModelDerivative.DerivativesApi () ;
var ossBuckets =new ForgeSDK.BucketsApi () ;
var ossObjects =new ForgeSDK.ObjectsApi () ;
var md =new ForgeSDK.DerivativesApi () ;

var router =express.Router () ;
router.use (bodyParser.json ()) ;

router.get ('/translate/:urn/progress', function (req, res) {
var accessToken =req.query.accessToken ;
var urn =req.params.urn ;

//ForgeOSS.ApiClient.instance.authentications ['oauth2_application'].accessToken =accessToken ;
ForgeModelDerivative.ApiClient.instance.authentications ['oauth2_application'].accessToken =accessToken ;
md.getManifest (urn, {})
.then (function (data) {
var name =path.basename (Buffer.from (data.urn, 'base64').toString ()) ;
if ( data.derivatives !== undefined && data.derivatives.length > 0 && data.derivatives [0].hasOwnProperty ('name') )
name =data.derivatives [0].name ;
res.json ({
status: data.status,
progress: data.progress,
urn: data.urn,
name: name
}).end () ;
console.log ('Request: ' + data.status + ' (' + data.progress + ')') ;
})
.catch (function (error) {
res.status (404).end () ;
}) ;
var urn =req.params.urn ;
var credentials = req.session.credentials
var autoRefresh = true;
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(credentials.client_id, credentials.client_secret, credentials.scope, autoRefresh);
oAuth2TwoLegged.authenticate().then(function(credentials) {
md.getManifest (urn, {},oAuth2TwoLegged,credentials)
.then (function (dataBody) {
var data = dataBody.body
console.log("progrees data ",data)
var name =path.basename (new Buffer(data.urn, 'base64').toString ()) ;
if ( data.derivatives !== undefined && data.derivatives.length > 0 && data.derivatives [0].hasOwnProperty ('name') )
name =data.derivatives [0].name ;
// var percentage = data.progress.split("% complete")
res.json ({
status: data.status,
progress: data.progress,
urn: data.urn,
name: name
}).end () ;
console.log ('Request: ' + data.status + ' (' + data.progress + ')') ;
}).catch (function (error) {
console.log("error ",error)
res.status (404).end () ;
}) ;
})
}) ;

function makeKey (file) {
var filename =path.basename (file) ;
return (filename) ;
var filename =path.basename (file) ;
return (filename) ;
}

function singleUpload (bucketKey, filename, total) {
var objectKey =makeKey (filename) ;
var readStream =fs.createReadStream (filename) ;
return (ossObjects.uploadObject (bucketKey, objectKey, total, readStream, {})) ;
function singleUpload (bucketKey, filename, total,oAuth2TwoLegged,credentials) {
var objectKey =makeKey (filename) ;
var readStream =fs.createReadStream (filename) ;
return (ossObjects.uploadObject (bucketKey, objectKey, total, readStream, {},oAuth2TwoLegged,credentials)) ;
}

function ExistOrCreate (bucketKey) {
console.log ('Check Bucket if bucket exists...') ;
return (ossBuckets.getBucketDetails (bucketKey)
.then (function (results) {
return (results) ;
})
.catch (function (error) {
console.log ('Create Bucket...') ;
var opts ={
"bucketKey": bucketKey,
"policyKey": 'transient'
} ;
var headers ={
'xAdsRegion': 'US'
} ;
return (ossBuckets.createBucket (opts, headers)) ;
})
) ;
function ExistOrCreate (bucketKey,oAuth2TwoLegged,credentials) {
console.log ('Check Bucket if bucket exists...') ;
return (ossBuckets.getBucketDetails (bucketKey,oAuth2TwoLegged,credentials)
.then (function (results) {
return (results) ;
})
.catch (function (error) {
console.log ('Create Bucket...') ;
var opts ={
"bucketKey": bucketKey,
"policyKey": 'transient'
} ;
var headers ={
'xAdsRegion': 'US'
} ;
return (ossBuckets.createBucket (opts, headers,oAuth2TwoLegged,credentials)) ;
})
) ;
}

router.post ('/translate', function (req, res) {
var accessToken =req.body.accessToken ;
var filename =path.normalize (__dirname + '/../' + req.body.file) ;
var hash =crypto.createHash ('md5').update (config.credentials.client_id).digest ('hex').replace (/\W+/g, '') ;
var bucketKey =
'model'
+ new Date ().toISOString ().replace (/T/, '-').replace (/:+/g, '-').replace (/\..+/, '')
+ '-' + hash ;

ForgeOSS.ApiClient.instance.authentications ['oauth2_application'].accessToken =accessToken ;
ForgeModelDerivative.ApiClient.instance.authentications ['oauth2_application'].accessToken =accessToken ;

var stats ;
stat (filename)
.then (function (results) {
stats =results ;
return (ExistOrCreate (bucketKey)) ;
})
.then (function (bucket) {
console.log ('async upload') ;
var total =stats.size ;
var chunkSize =config.fileResumableChunk * 1024 * 1024 ;
//if ( total <= chunkSize )
return (singleUpload (bucketKey, filename, total)) ;
//else
// return (uploadFileAsChunks (bucketKey, filename, total, chunkSize)) ;
})
.then (function (data) {
console.log ('Launching translation') ;
var job ={
"input": {
"urn": new Buffer (data.objectId).toString ('base64'),
"compressedUrn": false,
"rootFilename": data.objectKey
},
"output": {
"formats": [
{
"type": "svf",
"views": [
"2d",
"3d"
]
}
]
}
} ;
return (md.translate (job, { 'xAdsForce': true })) ;
})
.then (function (results) {
res.json (results) ;
})
.catch (function (error) {
console.log (JSON.stringify (error)) ;
res.status (500).end () ;
})
;
var credentials = req.session.credentials
var autoRefresh = true;
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(credentials.client_id, credentials.client_secret, credentials.scope, autoRefresh);
oAuth2TwoLegged.authenticate().then(function(credentials) {
console.log(" oAuth2TwoLegged credentials ", credentials)
var filename =path.normalize (__dirname + '/../' + req.body.file) ;
var hash =crypto.createHash ('md5').update (config.credentials.client_id).digest ('hex').replace (/\W+/g, '') ;
var bucketKey =
'model'
+ new Date ().toISOString ().replace (/T/, '-').replace (/:+/g, '-').replace (/\..+/, '')
+ '-' + hash
+ '-' + "manufact" ;
// ForgeModelDerivative.ApiClient.instance.authentications ['oauth2_application'].accessToken =credentials.access_token ;
var stats ;
stat (filename)
.then (function (results) {
stats =results ;
return (ExistOrCreate (bucketKey,oAuth2TwoLegged,credentials)) ;
})
.then (function (bucket) {
console.log ('async upload') ;
var total =stats.size ;
var chunkSize =config.fileResumableChunk * 1024 * 1024 ;
//if ( total <= chunkSize )
return (singleUpload (bucketKey, filename, total,oAuth2TwoLegged,credentials)) ;
//else
// return (uploadFileAsChunks (bucketKey, filename, total, chunkSize)) ;
})
.then (function (data) {
console.log ('Launching translation',data) ;
var job ={
"input": {
"urn": new Buffer (data.body.objectId).toString ('base64'),
"compressedUrn": false,
"rootFilename": data.body.objectKey
},
"output": {
"formats": [
{
"type": "svf",
"views": [
"2d",
"3d"
]
}
]
}
} ;
return (md.translate (job, { 'xAdsForce': true },oAuth2TwoLegged,credentials)) ;
})
.then (function (results) {
console.log("results ",results)
res.json (results.body) ;
})
.catch (function (error) {
console.log ("error ",JSON.stringify (error)) ;
res.status (500).end () ;
})
;
});
}) ;


Expand Down
34 changes: 18 additions & 16 deletions server/lmv-token.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
//
// Copyright (c) Autodesk, Inc. All rights reserved
// Copyright (c) Autodesk, Inc. All rights reserved
//
// Node.js server workflow
// Node.js server workflow
// by Cyrille Fauvel - Autodesk Developer Network (ADN)
// January 2015
// Modified by SVNINDIA, Sep 2017
//
// 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
// 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
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// 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.
// 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 ForgeOauth2 =require ('forge-oauth2') ;
var ForgeSDK =require ('forge-apis') ;
var config =require ('./credentials') ;

var session = require('express-session');
var router =express.Router () ;

// This is the downgraded access_token for the viewer (should be read-only)
router.get ('/token', function (req, res) {
var credentials =config.clone ('data:read') ;
credentials.client_id =req.query.key ;
credentials.client_secret =req.query.secret ;
console.log("credentials ",credentials)
refreshToken (credentials, res) ;
}) ;

Expand All @@ -37,19 +39,19 @@ router.post ('/token', function (req, res) {
var credentials =config.clone () ;
credentials.client_id =req.body.key ;
credentials.client_secret =req.body.secret ;
req.session.credentials = credentials
refreshToken (credentials, res) ;
}) ;

var refreshToken =function (credentials, res) {
var apiInstance =new ForgeOauth2.TwoLeggedApi () ;
apiInstance.authenticate (credentials.client_id, credentials.client_secret, credentials.grant_type, credentials)
.then (function (response) {
res.json (response) ;
})
.catch (function (error) {
var autoRefresh = true;
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(credentials.client_id, credentials.client_secret, credentials.scope, autoRefresh);
oAuth2TwoLegged.authenticate().then(function(credentials) {
res.json (credentials) ;
}).catch (function (error) {
if ( error.statusCode )
return (res.status (error.statusCode).end (error.statusMessage)) ;
res.status (500).end () ;
res.status (500).end ("Enter valid credentials") ;
})
;
} ;
Expand Down
Loading