diff --git a/.gitignore b/.gitignore index c2ad1db..50d8c4b 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,8 @@ node_modules credentials.js # webstorm project files -.idea \ No newline at end of file +.idea + +.DS_store +Thumbs.db +*.bak diff --git a/README-stg.md b/README-stg.md index 56bacc1..15cc34d 100644 --- a/README-stg.md +++ b/README-stg.md @@ -43,11 +43,11 @@ get model URNs - as explained in the Setup/Usage Instructions. ``` cp credentials_.js credentials.js ``` -* Replace the placeholder with your own keys in credentials.js, line #23 and #24
+* Replace the placeholders with your own keys in credentials.js, line #23 and #24
``` - credentials.ClientId = ''; + client_id: process.env.CONSUMERKEY || ''; - credentials.ClientSecret = ''; + client_secret: process.env.CONSUMERSECRET || ''; ``` * In file credentials.js line #26, replace the BaseUrl address by the staging server address
``` @@ -56,7 +56,7 @@ get model URNs - as explained in the Setup/Usage Instructions. * Upload one of your models to your account and get its URN using another workflow sample, for example, - [this workflow sample in .Net WPF application](https://github.com/Developer-Autodesk/workflow-wpf-view.and.data.api) if you are using windows - or [this workflow sample in Mac OS Swift](https://github.com/Developer-Autodesk/workflow-macos-swift-view.and.data.api) if you are using Mac - - or this [WEB page](http://javalmvwalkthrough-vq2mmximxb.elasticbeanstalk.com/) + - or this [WEB page](http://models.autodesk.io/) or this [one](http://javalmvwalkthrough-vq2mmximxb.elasticbeanstalk.com/) * Copy the URN which was generated in the previous step in file /www/index.js at line #18
``` var defaultUrn = ''; diff --git a/README.md b/README.md index 890eed8..5afcb32 100644 --- a/README.md +++ b/README.md @@ -34,16 +34,16 @@ get model URNs - as explained in the Setup/Usage Instructions. ``` cp credentials_.js credentials.js ``` -* Replace the placeholder with your own keys in credentials.js, line #23 and #24
+* Replace the placeholders with your own keys in credentials.js, line #23 and #24
``` - credentials.ClientId = ''; + client_id: process.env.CONSUMERKEY || '', - credentials.ClientSecret = ''; + client_secret: process.env.CONSUMERSECRET || '', ``` * Upload one of your models to your account and get its URN using another workflow sample, for example, - [this workflow sample in .Net WPF application](https://github.com/Developer-Autodesk/workflow-wpf-view.and.data.api) if you are using windows - or [this workflow sample in Mac OS Swift](https://github.com/Developer-Autodesk/workflow-macos-swift-view.and.data.api) if you are using Mac - - or this [WEB page](http://javalmvwalkthrough-vq2mmximxb.elasticbeanstalk.com/) + - or this [WEB page](http://models.autodesk.io/) or this [one](http://javalmvwalkthrough-vq2mmximxb.elasticbeanstalk.com/) * Copy the URN which was generated in the previous step in file /www/index.js at line #18
``` var defaultUrn = ''; diff --git a/credentials_.js b/credentials_.js index 7faa047..ea24448 100644 --- a/credentials_.js +++ b/credentials_.js @@ -15,15 +15,21 @@ // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. ///////////////////////////////////////////////////////////////////////////////// +var credentials ={ -var credentials ={} ; + credentials: { + // Replace placeholder below by the Consumer Key and Consumer Secret you got from + // http://developer.autodesk.com/ for the production server + client_id: process.env.CONSUMERKEY || '', + client_secret: process.env.CONSUMERSECRET || '', + grant_type: 'client_credentials' + }, + + // If you which to use the Autodesk View & Data API on the staging server, change this url + BaseUrl: 'https://developer.api.autodesk.com', + Version: 'v1' +} ; -// Replace placeholder below by the Consumer Key and Consumer Secret you got from -// http://developer.autodesk.com/ for the production server -credentials.ClientId ='' ; -credentials.ClientSecret ='' ; +credentials.Authentication =credentials.BaseUrl + '/authentication/' + credentials.Version + '/authenticate' -// If you which to use the Autodesk View & Data API on the staging server, change this url -credentials.BaseUrl = 'https://developer.api.autodesk.com' ; - -module.exports =credentials ; +module.exports =credentials ; \ No newline at end of file diff --git a/package.json b/package.json index 01fb0f0..cc7ec2c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,29 @@ { - "name": "AdnViewerBasic", - - "version": "0.0.0", - - "dependencies": { - "express": "*", - "request": "*", - "serve-favicon": "*" - } + "name": "AdnViewerBasic", + "description": "A basic node.js server sample", + "version": "1.0.0", + "dependencies": { + "serve-favicon": ">= 2.2.0", + "express": ">= 4.12.3", + "request": ">= 2.55.0" + }, + "files": [ + "LICENSE", + "README.md" + ], + "engines": { + "node": ">= 0.10.0" + }, + "contributors": [ + "Philippe Leefsma ", + "Cyrille Fauvel " + ], + "license": "MIT", + "scripts": { + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/Developer-Autodesk/workflow-node.js-view.and.data.api.git" + } } \ No newline at end of file diff --git a/routes/api.js b/routes/api.js index 408f063..7aaa419 100644 --- a/routes/api.js +++ b/routes/api.js @@ -15,32 +15,25 @@ // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. ///////////////////////////////////////////////////////////////////////////////// -var credentials = require('../credentials'); +var credentials =(require ('fs').existsSync ('credentials.js') ? + require('../credentials') + : (console.log ('No credentials.js file present, assuming using CONSUMERKEY & CONSUMERSECRET system variables.'), require('../credentials_'))) ; +var express =require ('express') ; +var request =require ('request') ; -var express = require('express'); -var request = require('request'); - -var router = express.Router(); +var router =express.Router () ; /////////////////////////////////////////////////////////////////////////////// // Generates access token /////////////////////////////////////////////////////////////////////////////// -router.get('/token', function (req, res) { - var params = { - client_id: credentials.ClientId, - client_secret: credentials.ClientSecret, - grant_type: 'client_credentials' - } - - request.post( - credentials.BaseUrl + '/authentication/v1/authenticate', - { form: params }, - +router.get ('/token', function (req, res) { + request.post ( + credentials.Authentication, + { form: credentials.credentials }, function (error, response, body) { - if (!error && response.statusCode == 200) { - res.send(body); - } - }); -}); + if ( !error && response.statusCode == 200 ) + res.send (body) ; + }) ; +}) ; -module.exports = router; +module.exports =router ; diff --git a/www/images/Thumbs.db b/www/images/Thumbs.db deleted file mode 100644 index 02d42c8..0000000 Binary files a/www/images/Thumbs.db and /dev/null differ diff --git a/www/upload.html b/www/upload.html new file mode 100644 index 0000000..9744f2c --- /dev/null +++ b/www/upload.html @@ -0,0 +1,78 @@ + + + + ADN Viewer Demo (client upload) + + + + + + + + + + + + + + + +
+
+
+

Upload and translate a file

+
+
+ +
+ + +
+
+
+
+
+ +
+
+
+

My URNs

+
+
+
+
+ +
+ +
+ +
+ My URN list +
Click on a urn below to launch the viewer
+
+
+
+
+ + + diff --git a/www/upload.js b/www/upload.js new file mode 100644 index 0000000..b83822d --- /dev/null +++ b/www/upload.js @@ -0,0 +1,155 @@ +///////////////////////////////////////////////////////////////////////////////// +// Copyright (c) Autodesk, Inc. All rights reserved +// Written by Cyrille Fauvel, 2015 - ADN/Developer Technical Services +// +// 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 oViewDataClient =null ; + +$(document).ready (function () { + oViewDataClient =new Autodesk.ADN.Toolkit.ViewData.AdnViewDataClient ( + 'https://developer.api.autodesk.com', + 'http://' + window.location.host + '/api/token' + ) ; + + $('#btnTranslateThisOne').click (function (evt) { + var files =document.getElementById('files').files ; + if ( files.length == 0 ) + return ; + var bucket = + 'model' + + new Date ().toISOString ().replace (/T/, '-').replace (/:+/g, '-').replace (/\..+/, '') + + '-' + ''.toLowerCase ().replace (/\W+/g, '') ; + + createBucket (bucket, files) + }) ; + + $('#btnAddThisOne').click (function (evt) { + var urn =$('#urn').val ().trim () ; + if ( urn == '' ) + return ; + AddThisOne (urn) ; + }) ; +}) ; + +function AddThisOne (urn) { + var id =urn.replace (/=+/g, '') ; + $('#list').append ('
' + + '' + + '
' + ) ; + $('#' + id).click (function (evt) { + window.open ('/?urn=' + $(this).text (), '_blank') ; + }) ; +} + +function createBucket (bucket, files) { + var bucketData ={ + bucketKey: bucket, + servicesAllowed: {}, + policy: 'transient' + } ; + oViewDataClient.createBucketAsync ( + bucketData, + //onSuccess + function (response) { + console.log ('Bucket creation successful:') ; + console.log (response) ; + $('#msg').text ('Bucket creation successful') ; + uploadFiles (response.key, files) ; + }, + //onError + function (error) { + console.log ('Bucket creation failed:'); + console.log (error) ; + $('#msg').text ('Bucket creation failed!') ; + } + ) ; +} + +function uploadFiles (bucket, files) { + for ( var i =0 ; i < files.length ; i++ ) { + var file =files [i] ; + //var filename =file.replace (/^.*[\\\/]/, '') ; + console.log ('Uploading file: ' + file.name + ' ...') ; + $('#msg').text ('Uploading file: ' + file.name + ' ...') ; + oViewDataClient.uploadFileAsync ( + file, + bucket, + file.name, + //onSuccess + function (response) { + console.log ('File was uploaded successfully:') ; + console.log (response) ; + $('#msg').text ('File was uploaded successfully') ; + var fileId =response.objects [0].id ; + var registerResponse =oViewDataClient.register (fileId) ; + if ( registerResponse.Result === "Success" + || registerResponse.Result === "Created" + ) { + console.log ("Registration result: " + registerResponse.Result) ; + console.log ('Starting translation: ' + fileId) ; + $('#msg').text ('Your model was uploaded successfully. Translation starting...') ; + checkTranslationStatus ( + fileId, + 1000 * 60 * 5, // 5 mins timeout + //onSuccess + function (viewable) { + console.log ("Translation was successful: " + response.file.name) ; + console.log ("Viewable: ") ; + console.log (viewable) ; + $('#msg').text ('Translation was successful: ' + response.file.name + '.') ; + //var fileId =oViewDataClient.fromBase64 (viewable.urn) ; + AddThisOne (viewable.urn) ; + } + ) ; + } + }, + //onError + function (error) { + console.log ('File upload failed:') ; + console.log (error) ; + $('#msg').text ('File upload failed!') ; + } + ) ; + } +} + +function checkTranslationStatus (fileId, timeout, onSuccess) { + var startTime =new Date ().getTime () ; + var timer =setInterval (function () { + var dt =(new Date ().getTime () - startTime) / timeout ; + if ( dt >= 1.0 ) { + clearInterval (timer) ; + } else { + oViewDataClient.getViewableAsync ( + fileId, + function (response) { + var msg ='Translation Progress ' + fileId + ': ' + response.progress ; + console.log (msg) ; + $('#msg').text (msg) ; + if ( response.progress === 'complete' ) { + clearInterval (timer) ; + onSuccess (response) ; + } + }, + function (error) { + } + ) ; + } + }, + 2000 + ) ; +}