diff --git a/package.json b/package.json index 98139e7..69b8550 100644 --- a/package.json +++ b/package.json @@ -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": "*" diff --git a/server/credentials.js b/server/credentials.js index 8c89757..2cbaeed 100644 --- a/server/credentials.js +++ b/server/credentials.js @@ -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 diff --git a/server/lmv-projects.js b/server/lmv-projects.js index 1dd103c..83a509d 100644 --- a/server/lmv-projects.js +++ b/server/lmv-projects.js @@ -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. // @@ -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 () ; + }) + ; + }); }) ; diff --git a/server/lmv-token.js b/server/lmv-token.js index 19b8ad6..ec71914 100644 --- a/server/lmv-token.js +++ b/server/lmv-token.js @@ -1,27 +1,28 @@ // -// 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) @@ -29,6 +30,7 @@ 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) ; }) ; @@ -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") ; }) ; } ; diff --git a/server/server.js b/server/server.js index 34e44eb..6faf1b9 100644 --- a/server/server.js +++ b/server/server.js @@ -1,20 +1,21 @@ // -// Copyright (c) Autodesk, Inc. All rights reserved +// Copyright (c) Autodesk, Inc. All rights reserved // // Node.js server Host // by Cyrille Fauvel - Autodesk Developer Network (ADN) // April 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. // @@ -24,9 +25,10 @@ var favicon =require ('serve-favicon') ; var lmvToken =require ('./lmv-token') ; var lmvProjects =require ('./lmv-projects') ; var lmvFile =require ('./lmv-file') ; - +var session = require('express-session'); // http://garann.github.io/template-chooser/ var app =express () ; +app.use(session({secret: ":), its a secret!",resave: true,saveUninitialized: true})); //app.use (bodyParser.urlencoded ({ extended: false })) ; app.use (bodyParser.json ()) ; app.use (express.static (__dirname + '/../www')) ;