加入forge會使用到的api
This commit is contained in:
		
							parent
							
								
									e84e6b04ff
								
							
						
					
					
						commit
						d3f199cd00
					
				
							
								
								
									
										55
									
								
								FrontendWebApi/ApiControllers/ModelDerivativeController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								FrontendWebApi/ApiControllers/ModelDerivativeController.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,55 @@
 | 
			
		||||
using Autodesk.Forge.Model;
 | 
			
		||||
using Autodesk.Forge;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace FrontendWebApi.ApiControllers
 | 
			
		||||
{
 | 
			
		||||
    public class ModelDerivativeController : ControllerBase //MyBaseApiController<ModelDerivativeController>//Controller
 | 
			
		||||
    {
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Start the translation job for a give bucketKey/objectName
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="objModel"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        [Route("api/forge/modelderivative/jobs")]
 | 
			
		||||
        public async Task<dynamic> TranslateObject([FromBody] TranslateObjectModel objModel)
 | 
			
		||||
        {
 | 
			
		||||
            dynamic oauth = await OAuthController.GetInternalAsync();
 | 
			
		||||
 | 
			
		||||
            // prepare the payload
 | 
			
		||||
            List<JobPayloadItem> outputs = new List<JobPayloadItem>()
 | 
			
		||||
            {
 | 
			
		||||
            new JobPayloadItem(
 | 
			
		||||
                JobPayloadItem.TypeEnum.Svf,
 | 
			
		||||
                new List<JobPayloadItem.ViewsEnum>()
 | 
			
		||||
                {
 | 
			
		||||
                JobPayloadItem.ViewsEnum._2d,
 | 
			
		||||
                JobPayloadItem.ViewsEnum._3d
 | 
			
		||||
                })
 | 
			
		||||
            };
 | 
			
		||||
            JobPayload job;
 | 
			
		||||
            job = new JobPayload(new JobPayloadInput(objModel.objectName), new JobPayloadOutput(outputs));
 | 
			
		||||
 | 
			
		||||
            // start the translation
 | 
			
		||||
            DerivativesApi derivative = new DerivativesApi();
 | 
			
		||||
            derivative.Configuration.AccessToken = oauth.access_token;
 | 
			
		||||
            dynamic jobPosted = await derivative.TranslateAsync(job);
 | 
			
		||||
            return jobPosted;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Model for TranslateObject method
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public class TranslateObjectModel
 | 
			
		||||
        {
 | 
			
		||||
            public string bucketKey { get; set; }
 | 
			
		||||
            public string objectName { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										67
									
								
								FrontendWebApi/ApiControllers/OAuthController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								FrontendWebApi/ApiControllers/OAuthController.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,67 @@
 | 
			
		||||
using Autodesk.Forge;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace FrontendWebApi.ApiControllers
 | 
			
		||||
{
 | 
			
		||||
    public class OAuthController : ControllerBase //MyBaseApiController<OAuthController>//Controller
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        private static dynamic InternalToken { get; set; }
 | 
			
		||||
        private static dynamic PublicToken { get; set; }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Get access token with public (viewables:read) scope
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        [Route("api/forge/oauth/token")]
 | 
			
		||||
        public async Task<dynamic> GetPublicAsync()
 | 
			
		||||
        {
 | 
			
		||||
            if (PublicToken == null || PublicToken.ExpiresAt < DateTime.UtcNow)
 | 
			
		||||
            {
 | 
			
		||||
                PublicToken = await Get2LeggedTokenAsync(new Scope[] { Scope.ViewablesRead });
 | 
			
		||||
                PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in);
 | 
			
		||||
            }
 | 
			
		||||
            return PublicToken;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Get access token with internal (write) scope
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static async Task<dynamic> GetInternalAsync()
 | 
			
		||||
        {
 | 
			
		||||
            if (InternalToken == null || InternalToken.ExpiresAt < DateTime.UtcNow)
 | 
			
		||||
            {
 | 
			
		||||
                InternalToken = await Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll });
 | 
			
		||||
                InternalToken.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return InternalToken;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Get the access token from Autodesk
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        private static async Task<dynamic> Get2LeggedTokenAsync(Scope[] scopes)
 | 
			
		||||
        {
 | 
			
		||||
            TwoLeggedApi oauth = new TwoLeggedApi();
 | 
			
		||||
            string grantType = "client_credentials";
 | 
			
		||||
            dynamic bearer = await oauth.AuthenticateAsync(
 | 
			
		||||
              "TA3hqsFfzQbNOUXKpldKUKSew4SJ21w5",
 | 
			
		||||
              "D002f92d839144f8",
 | 
			
		||||
              grantType,
 | 
			
		||||
              scopes);
 | 
			
		||||
            return bearer;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Reads appsettings from web.config
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string GetAppSetting(string settingKey)
 | 
			
		||||
        {
 | 
			
		||||
            return Environment.GetEnvironmentVariable(settingKey).Trim();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										148
									
								
								FrontendWebApi/ApiControllers/OSSController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										148
									
								
								FrontendWebApi/ApiControllers/OSSController.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,148 @@
 | 
			
		||||
using Autodesk.Forge.Model;
 | 
			
		||||
using Autodesk.Forge;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using System.IO;
 | 
			
		||||
 | 
			
		||||
namespace FrontendWebApi.ApiControllers
 | 
			
		||||
{
 | 
			
		||||
    public class OSSController : ControllerBase //MyBaseApiController<OSSController>//Controller
 | 
			
		||||
    {
 | 
			
		||||
        private IWebHostEnvironment _env;
 | 
			
		||||
        public OSSController(IWebHostEnvironment env) { _env = env; }
 | 
			
		||||
        string id = "TA3hqsFfzQbNOUXKpldKUKSew4SJ21w5";
 | 
			
		||||
        public string ClientId { get { return id.ToLower(); } }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Return list of buckets (id=#) or list of objects (id=bucketKey)
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        [HttpGet]
 | 
			
		||||
        [Route("api/forge/oss/buckets")]
 | 
			
		||||
        public async Task<IList<TreeNode>> GetOSSAsync(string id)
 | 
			
		||||
        {
 | 
			
		||||
            IList<TreeNode> nodes = new List<TreeNode>();
 | 
			
		||||
            dynamic oauth = await OAuthController.GetInternalAsync();
 | 
			
		||||
 | 
			
		||||
            if (id == "#") // root
 | 
			
		||||
            {
 | 
			
		||||
                // in this case, let's return all buckets
 | 
			
		||||
                BucketsApi appBckets = new BucketsApi();
 | 
			
		||||
                appBckets.Configuration.AccessToken = oauth.access_token;
 | 
			
		||||
 | 
			
		||||
                // to simplify, let's return only the first 100 buckets
 | 
			
		||||
                dynamic buckets = await appBckets.GetBucketsAsync("US", 100);
 | 
			
		||||
                foreach (KeyValuePair<string, dynamic> bucket in new DynamicDictionaryItems(buckets.items))
 | 
			
		||||
                {
 | 
			
		||||
                    nodes.Add(new TreeNode(bucket.Value.bucketKey, bucket.Value.bucketKey.Replace(ClientId + "-", string.Empty), "bucket", true));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                // as we have the id (bucketKey), let's return all 
 | 
			
		||||
                ObjectsApi objects = new ObjectsApi();
 | 
			
		||||
                objects.Configuration.AccessToken = oauth.access_token;
 | 
			
		||||
                var objectsList = await objects.GetObjectsAsync(id, 100);
 | 
			
		||||
                foreach (KeyValuePair<string, dynamic> objInfo in new DynamicDictionaryItems(objectsList.items))
 | 
			
		||||
                {
 | 
			
		||||
                    nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId),
 | 
			
		||||
                      objInfo.Value.objectKey, "object", false));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return nodes;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Model data for jsTree used on GetOSSAsync
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public class TreeNode
 | 
			
		||||
        {
 | 
			
		||||
            public TreeNode(string id, string text, string type, bool children)
 | 
			
		||||
            {
 | 
			
		||||
                this.id = id;
 | 
			
		||||
                this.text = text;
 | 
			
		||||
                this.type = type;
 | 
			
		||||
                this.children = children;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public string id { get; set; }
 | 
			
		||||
            public string text { get; set; }
 | 
			
		||||
            public string type { get; set; }
 | 
			
		||||
            public bool children { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Create a new bucket 
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        [Route("api/forge/oss/buckets")]
 | 
			
		||||
        public async Task<dynamic> CreateBucket([FromBody] CreateBucketModel bucket)
 | 
			
		||||
        {
 | 
			
		||||
            BucketsApi buckets = new BucketsApi();
 | 
			
		||||
            dynamic token = await OAuthController.GetInternalAsync();
 | 
			
		||||
            buckets.Configuration.AccessToken = token.access_token;
 | 
			
		||||
            PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey.ToLower()), null,
 | 
			
		||||
              PostBucketsPayload.PolicyKeyEnum.Transient);
 | 
			
		||||
            return await buckets.CreateBucketAsync(bucketPayload, "US");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Input model for CreateBucket method
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public class CreateBucketModel
 | 
			
		||||
        {
 | 
			
		||||
            public string bucketKey { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Receive a file from the client and upload to the bucket
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        [HttpPost]
 | 
			
		||||
        [Route("api/forge/oss/objects")]
 | 
			
		||||
        public async Task<dynamic> UploadObject([FromForm] UploadFile input)
 | 
			
		||||
        {
 | 
			
		||||
            // save the file on the server
 | 
			
		||||
            var fileSavePath = Path.Combine(_env.WebRootPath, Path.GetFileName(input.fileToUpload.FileName));
 | 
			
		||||
            using (var stream = new FileStream(fileSavePath, FileMode.Create))
 | 
			
		||||
                await input.fileToUpload.CopyToAsync(stream);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            // get the bucket...
 | 
			
		||||
            dynamic oauth = await OAuthController.GetInternalAsync();
 | 
			
		||||
            ObjectsApi objects = new ObjectsApi();
 | 
			
		||||
            objects.Configuration.AccessToken = oauth.access_token;
 | 
			
		||||
 | 
			
		||||
            // upload the file/object, which will create a new object
 | 
			
		||||
            dynamic uploadedObj;
 | 
			
		||||
            using (StreamReader streamReader = new StreamReader(fileSavePath))
 | 
			
		||||
            {
 | 
			
		||||
                uploadedObj = await objects.UploadObjectAsync(input.bucketKey,
 | 
			
		||||
                       Path.GetFileName(input.fileToUpload.FileName), (int)streamReader.BaseStream.Length, streamReader.BaseStream,
 | 
			
		||||
                       "application/octet-stream");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // cleanup
 | 
			
		||||
            System.IO.File.Delete(fileSavePath);
 | 
			
		||||
 | 
			
		||||
            return uploadedObj;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public class UploadFile
 | 
			
		||||
        {
 | 
			
		||||
            public string bucketKey { get; set; }
 | 
			
		||||
            public IFormFile fileToUpload { get; set; }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Base64 enconde a string
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public static string Base64Encode(string plainText)
 | 
			
		||||
        {
 | 
			
		||||
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
 | 
			
		||||
            return System.Convert.ToBase64String(plainTextBytes);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,13 +1,4 @@
 | 
			
		||||
{
 | 
			
		||||
  "iisSettings": {
 | 
			
		||||
    "windowsAuthentication": false,
 | 
			
		||||
    "anonymousAuthentication": true,
 | 
			
		||||
    "iisExpress": {
 | 
			
		||||
      "applicationUrl": "http://localhost:3604",
 | 
			
		||||
      "sslPort": 0
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "$schema": "http://json.schemastore.org/launchsettings.json",
 | 
			
		||||
  "profiles": {
 | 
			
		||||
    "IIS Express": {
 | 
			
		||||
      "commandName": "IISExpress",
 | 
			
		||||
@ -22,9 +13,22 @@
 | 
			
		||||
      "launchBrowser": true,
 | 
			
		||||
      "environmentVariables": {
 | 
			
		||||
        "ASPNETCORE_ENVIRONMENT": "Development",
 | 
			
		||||
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
 | 
			
		||||
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation",
 | 
			
		||||
        "FORGE_CALLBACK_URL": "http://localhost:3000/api/forge/callback/oauth",
 | 
			
		||||
        "FORGE_CLIENT_ID": "TA3hqsFfzQbNOUXKpldKUKSew4SJ21w5",
 | 
			
		||||
        "FORGE_CLIENT_SECRET": "D002f92d839144f8",
 | 
			
		||||
        "ASPNETCORE_URLS": "http://localhost:5500"
 | 
			
		||||
      },
 | 
			
		||||
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "$schema": "http://json.schemastore.org/launchsettings.json",
 | 
			
		||||
  "iisSettings": {
 | 
			
		||||
    "windowsAuthentication": false,
 | 
			
		||||
    "anonymousAuthentication": true,
 | 
			
		||||
    "iisExpress": {
 | 
			
		||||
      "applicationUrl": "http://localhost:3604",
 | 
			
		||||
      "sslPort": 0
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user