Exemple #1
0
        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))
                {
                    string actualBucket = bucket.Value.bucketKey;
                    if (actualBucket.Contains(BucketName)) //kitchenconfig  designautomation
                    {
                        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 = objects.GetObjects(id);
                foreach (KeyValuePair <string, dynamic> objInfo in new DynamicDictionaryItems(objectsList.items))
                {
                    string fileName = objInfo.Value.objectKey;
                    fileName.ToLower();
                    if (fileName.Contains("zip") && fileName.Contains("output")) //result output
                    {
                        nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId),
                                               objInfo.Value.objectKey, "zipfile", false));
                    }

                    /* ovaj deo mi ne treba ovde jer neću da prikazujem ništa što nije rezult*.zip
                     * else
                     * {
                     *  nodes.Add(new TreeNode(Base64Encode((string)objInfo.Value.objectId),
                     *  objInfo.Value.objectKey, "object", false));
                     * }*/
                }
            }
            return(nodes);
        }
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            // basic input validation
            JObject workItemData       = JObject.Parse(input.data);
            string  uniqueActivityName = string.Format("{0}.{1}+{2}", NickName, ActivityName, Alias);
            string  browerConnectionId = workItemData["browerConnectionId"].Value <string>();

            // save the file on the server
            var fileSavePath = Path.Combine(LocalDataSetFolder, "Kitchen.zip");
            //using (var stream = new FileStream(fileSavePath, FileMode.Create)) await input.inputFile.CopyToAsync(stream);

            // OAuth token
            dynamic oauth = await OAuthController.GetInternalAsync();

            string     inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), "Kitchen.zip"); // avoid overriding
            ObjectsApi objects          = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;
            using (StreamReader streamReader = new StreamReader(fileSavePath))
                await objects.UploadObjectAsync(bucketKey, inputFileNameOSS, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream");
            //System.IO.File.Delete(fileSavePath);// delete server copy

            // prepare workitem arguments
            // 1. input file
            XrefTreeArgument inputFileArgument = new XrefTreeArgument()
            {
                Verb      = Verb.Get,
                LocalName = "Kitchen",
                PathInZip = "CleanSlate.iam",
                Url       = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, inputFileNameOSS),
                Headers   = new Dictionary <string, string>()
                {
                    { "Authorization", "Bearer " + oauth.access_token }
                }
            };
            // 2b. input json from viewer
            string           inputJsonStr      = workItemData.ToString(Newtonsoft.Json.Formatting.None).Replace("\"", "'");
            XrefTreeArgument inputJsonArgument = new XrefTreeArgument()
            {
                Verb = Verb.Get,
                Url  = "data:application/json, " + inputJsonStr
            };
            // 3. output file
            string           outputFileNameOSS  = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), "Kitchen.zip"); // avoid overriding
            XrefTreeArgument outputFileArgument = new XrefTreeArgument()
            {
                Url     = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, outputFileNameOSS),
                Verb    = Verb.Put,
                Headers = new Dictionary <string, string>()
                {
                    { "Authorization", "Bearer " + oauth.access_token }
                }
            };

            // prepare & submit workitem
            // the callback contains the connectionId (used to identify the client) and the outputFileName of this workitem
            string callbackUrl = string.Format(
                "{0}/api/forge/callback/designautomation?id={1}&outputFileName={2}",
                /* OAuthController.GetAppSetting("FORGE_WEBHOOK_URL"),*/
                "https://webkitchenbuilder.herokuapp.com",
                browerConnectionId,
                outputFileNameOSS
                );
            WorkItem workItemSpec = new WorkItem()
            {
                ActivityId = uniqueActivityName,
                Arguments  = new Dictionary <string, IArgument>()
                {
                    { "inputFile", inputFileArgument },
                    { "inputJson", inputJsonArgument },
                    { "outputFile", outputFileArgument },
                    { "onComplete", new XrefTreeArgument {
                          Verb = Verb.Post, Url = callbackUrl
                      } }
                }
            };

            try
            {
                WorkItemStatus workItemStatus = await _designAutomation.CreateWorkItemAsync(workItemSpec);

                return(Ok(new { workItemId = workItemStatus.Id }));
            }
            catch (Exception ex)
            {
                return(Ok(new { workItemId = ex.Message }));
            }
        }