Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Myview dataclient #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
console.log('Server listening on port ' + server.address().port);
});
});
2 changes: 1 addition & 1 deletion www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////////////////
var defaultUrn = '<replace with your encoded urn>';
var defaultUrn = 'dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6YmFsbGpvaW50L3B1bXAxQS5pcHQ';

$(document).ready(function () {
var tokenurl = 'http://' + window.location.host + '/api/token';
Expand Down
59 changes: 59 additions & 0 deletions www/upload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<html>
<head>
<title>ADN Viewer Demo (client upload)</title>
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />

<!-- jquery -->
<script src="https://code.jquery.com/jquery-2.1.2.min.js"></script>

<!-- Bootstrap CSS -->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />

<!-- Autodesk.ADN.Toolkit.Viewer -->
<script src=" https://rawgit.com/Developer-Autodesk/library-javascript-view.and.data.api/master/js/Autodesk.ADN.Toolkit.ViewData.js"></script>
<script src="/upload.js"></script>
</head>

<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Upload and translate a file</h3>
</div>
<div class="panel-body">
<input class="form-control" type="file" id="files" name="files" multiple />
<br />
<div style="text-align: center;">
<a class="btn btn-primary" id="btnTranslateThisOne">Translate this one for me</a>
</div>

<br />
<div id="msg"></div>
</div>
</div>
</div>

<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">My URNs</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-8">
<input class="form-control" type="text" id="urn" name="urn" value="" />
</div>
<div class="col-md-4">
<a class="btn btn-primary" id="btnAddThisOne">Add to the list</a>
</div>
</div>

<br />
<legend>My URN list</legend>
<div>Click on a urn below to launch the viewer</div>
<div id="list"></div>
</div>
</div>
</div>
</body>
</html>
145 changes: 145 additions & 0 deletions www/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
var oViewDataClient =null ;

$(document).ready (function () {
oViewDataClient =new Autodesk.ADN.Toolkit.ViewData.AdnViewDataClient (
'https://developer.api.autodesk.com',
'http://' + window.location.host + '/api/token'
) ;
}) ;
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 (/\..+/, '')
+ '-' + 'abAXdBGtQCUdptvG6bbC5AKCX9QFZmVY'.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 ('<div class="list-group-item row">'
+ '<button id="' + id + '" type="text" class="form-control">' + urn + '</button>'
+ '</div>'
) ;
$('#' + 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
) ;
}