4535992

Upload with SketchFabdataApi

Jan 31st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 5.06 KB | None | 0 0
  1. <html>
  2. <form id="the-form" action="" enctype="multipart/form-data" method="POST"> <!--style="display:none"-->
  3.     <input id="myfile" name="modelFile" type="file" class="form-control">
  4.     <input name="token" type="text" ng-model="authorizationUrlToken.value" class="form-control">
  5.     <input name="name" type="text" ng-model="itemToEdit.fields.name.value" class="form-control">
  6.     <input name="description" type="text" value="" class="form-control">
  7.     <input name="tags" type="text" class="form-control">
  8.     <input id="myprivate" name="private" type="checkbox" value="0" class="form-control">
  9.     <input id="mypassword" name="password" type="password" class="form-control">
  10.     <input id="mysubmit" name="Submit" type="submit" value="Submit Upload to SketchFab" class="form-control btn btn-default" tooltip="Upload" title="Upload" />
  11. </form>          
  12.  
  13. <script>
  14. ///////////////////////////////////////////////////////////
  15. //TEST 3 upload via javascript of sketchfab with SketchfabDataApi
  16. ////////////////////////////////////////////////////////////
  17.         $scope.UploadModelBySketchfabdataApi = function() {
  18.             var token = $scope.authorizationUrlToken.value;
  19.  
  20.             var file = jQuery("#myfile")[0].files[0];//.attr("files")[0];
  21.             if (file) {
  22.                 var fileName = file.name;
  23.             }
  24.             var fullPathFile = window.location.protocol + "//" + window.location.host;      
  25.             fullPathFile = fullPathFile + '/private/' + fileName;
  26.             console.info('START:' + fullPathFile);
  27.            
  28.             var Swagger = require('swagger-client');
  29.             var fs = require('fs');
  30.             var path = require('path');
  31.             var api = new SketchfabDataApi();
  32.             api.then(function (client) {
  33.  
  34.                 // This is how you authenticate your requests with the "Token" scheme
  35.                 client.clientAuthorizations.add("Token",
  36.                     new Swagger.ApiKeyAuthorization(
  37.                         "Authorization",
  38.                         "Token " + token ,
  39.                         "header"
  40.                     )
  41.                 );
  42.  
  43.                 // This is how you upload a file
  44.                 client.apis.models.post_v3_models({
  45.                     isPublished: 'false',
  46.                     modelFile: fs.createReadStream(path.resolve(fullPathFile))
  47.                 }).then(function (response) {
  48.  
  49.                     if (response.status === 201) {
  50.                         // The model URI is immediately returned, even if processing hasn't finished yet
  51.                         console.log('After processing, model will be available at: ' +
  52.                             response.headers.location);
  53.                         var uid = response.headers.location
  54.                             .replace('https://api.sketchfab.com/v3/models/', '');
  55.  
  56.                         // You can poll the processing status to know when the model is ready
  57.                         // See how `pollStatus` is implemented below
  58.                         pollStatus(client,
  59.                             uid,
  60.                             function (err, res) {
  61.                                 console.log(err, res);
  62.                             });
  63.                         //Redirect to my uri
  64.                         window.location.href = window.location.protocol + "//" + window.location.host + "/stealth/#/stealth/models3d/models3d";
  65.                     }
  66.  
  67.  
  68.                 }).catch(function (error) {
  69.                     console.error("ERROR ON UPLAOD:" + error);
  70.                 });
  71.  
  72.             }).catch(function (error) {
  73.                 console.log("ERROR ON AUTHENTICATION:" + error);
  74.             });
  75.  
  76.         }
  77.  
  78.         /**
  79.          * Poll processing status
  80.          * @param {object} client Swagger client
  81.          * @param {string} uid Model uid
  82.          * @param {function} callback will receive (err, result)
  83.          */
  84.         function pollStatus(client, uid, callback) {
  85.             client.apis.models.get_v3_models_uid({
  86.                 uid: uid
  87.             }).then(function (response) {
  88.                 if (response.obj.status.processing === 'SUCCEEDED') {
  89.                     callback(null, response.obj.status);
  90.                 } else if (response.obj.status.processing === 'FAILED') {
  91.                     callback(response.obj.status.processing, null);
  92.                 } else {
  93.                     setTimeout(function () {
  94.                         console.log(response.obj.status);
  95.                         pollStatus(client, uid, callback);
  96.                     }, 1000);
  97.                 }
  98.             });
  99.         }
  100.  
  101.         //Funzioni da chima re al submit della form
  102.         //data.onsubmit = function() {
  103.         //    //$scope.uploadModel(data);
  104.         //    $scope.UploadModelCSharp();
  105.         //    return false; // Prevent redirect
  106.         //}
  107.  
  108.         //jQuery('#the-form').on('submit', uploadModel);
  109.  
  110.         jQuery('#the-form').submit(function () {
  111.             //TEST 3
  112.             $scope.UploadModelBySketchfabdataApi();      
  113.             return false; // Prevent redirect
  114.         });
  115. </script>
  116. </html>
Add Comment
Please, Sign In to add comment