using Autodesk.Forge; using Autodesk.Forge.Model; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace forgeSample.Controllers { [ApiController] public class OSSController : ControllerBase { private IWebHostEnvironment _env; private static IConfiguration _configuration; public OSSController(IWebHostEnvironment env, IConfiguration configuration) { _env = env; _configuration = configuration; } public string ClientId { get { return _configuration.GetValue("Forge:CLIENT_ID").ToLower(); } } /// /// Return list of buckets (id=#) or list of objects (id=bucketKey) /// [HttpGet] [Route("api/forge/oss/buckets")] public async Task> GetOSSAsync(string id) { IList nodes = new List(); dynamic oauth = await OAuthController.GetInternalAsync(_configuration); 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 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 objInfo in new DynamicDictionaryItems(objectsList.items)) { nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId), objInfo.Value.objectKey, "object", false)); } } return nodes; } /// /// Model data for jsTree used on GetOSSAsync /// 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; } } /// /// Create a new bucket /// [HttpPost] [Route("api/forge/oss/buckets")] public async Task CreateBucket([FromBody] CreateBucketModel bucket) { BucketsApi buckets = new BucketsApi(); dynamic token = await OAuthController.GetInternalAsync(_configuration); 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"); } /// /// Input model for CreateBucket method /// public class CreateBucketModel { public string bucketKey { get; set; } } /// /// Receive a file from the client and upload to the bucket /// /// [HttpPost] [Route("api/forge/oss/objects")] public async Task 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(_configuration); 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); string urn = Base64Encode((string)uploadedObj.objectId); string result = urn + "," + uploadedObj.objectKey; return result;//uploadedObj; } public class UploadFile { public string bucketKey { get; set; } public IFormFile fileToUpload { get; set; } } /// /// Base64 enconde a string /// public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); } } }