Пример #1
0
        public async Task <IActionResult> OnCallback(string id, string outputFileName, string name, string type, [FromBody] dynamic body)
        {
            try
            {
                dynamic oauth = await OAuthController.GetInternalAsync();

                string bucketkey = NickName.ToLower() + "-designautomation";

                ObjectsApi objectsApi = new ObjectsApi();
                objectsApi.Configuration.AccessToken = oauth.access_token;
                dynamic objIPT = await objectsApi.GetObjectDetailsAsync(bucketkey, outputFileName);

                dynamic urnIPT = null;
                if (type == "part")
                {
                    urnIPT = TranslateObject(objIPT, outputFileName, false);
                }
                else if (type == "assembly")
                {
                    urnIPT = TranslateObject(objIPT, name + ".iam", true);
                }

                await _hubContext.Clients.Client(id).SendAsync("onComplete", (string)await urnIPT);

                //generate a signed URL to download the result file and send to the client
                dynamic signedUrl = await objectsApi.CreateSignedResourceAsyncWithHttpInfo(NickName.ToLower() + "-designautomation", outputFileName, new PostBucketsSigned(10), "read");

                await _hubContext.Clients.Client(id).SendAsync("downloadResult", (string)(signedUrl.Data.signedUrl));
            }
            catch {
            }

            // ALWAYS return ok (200)
            return(Ok());
        }
Пример #2
0
        public async Task <List <string> > GetAvailableEngines()
        {
            dynamic oauth = await OAuthController.GetInternalAsync();

            // define Engines API
            Page <string> engines = await _designAutomation.GetEnginesAsync();

            engines.Data.Sort();

            return(engines.Data); // return list of engines
        }
Пример #3
0
        /// <summary>
        /// Translate object
        /// </summary>
        private async Task <dynamic> TranslateObject(dynamic objModel, string outputFileName, bool compressed)
        {
            dynamic oauth = await OAuthController.GetInternalAsync();

            string objectIdBase64 = ToBase64(objModel.objectId);
            // prepare the payload
            List <JobPayloadItem> postTranslationOutput = 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(objectIdBase64, compressed, outputFileName),
                new JobPayloadOutput(postTranslationOutput)
                );

            // start the translation
            DerivativesApi derivative = new DerivativesApi();

            derivative.Configuration.AccessToken = oauth.access_token;
            dynamic jobPosted = await derivative.TranslateAsync(job, true);

            // check if it is complete.
            dynamic manifest = null;

            do
            {
                System.Threading.Thread.Sleep(1000); // wait 1 second
                try
                {
                    manifest = await derivative.GetManifestAsync(objectIdBase64);
                }
                catch (Exception) { }
            } while (manifest.progress != "complete");
            return(jobPosted.urn);
        }
Пример #4
0
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            // basic input validation
            WorkItemStatus workItemStatus = null;
            JObject        workItemData   = JObject.Parse(input.data);

            string        browerConnectionId = workItemData["browerConnectionId"].Value <string>();
            string        fileType           = workItemData["fileType"].Value <string>();
            string        activityName       = string.Format("{0}.{1}", NickName, workItemData["activityName"].Value <string>());
            List <string> activityList       = await GetDefinedActivities();

            if (!activityList.Contains(workItemData["activityName"].Value <string>()))
            {
                await CreateAppBundle();
                await CreateActivity(fileType);
            }
            if (fileType == "assembly")
            {
                string path = _env.ContentRootPath;
                Trace.TraceInformation("Zipping started");
                string fileSavePath = await CreateZipFileStreamAsync(input.inputFile, input.inputFiles, path);

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

                // upload file to OSS Bucket
                // 1. ensure bucket existis
                string     bucketKey = NickName.ToLower() + "-designautomation";
                BucketsApi buckets   = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                try
                {
                    PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                    await buckets.CreateBucketAsync(bucketPayload, "US");
                }
                catch { };                                                                                                                             // in case bucket already exists
                                                                                                                                                       // 2. upload inputFile
                string     inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(fileSavePath)); // 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()
                {
                    Url       = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, inputFileNameOSS),
                    PathInZip = input.inputFile.FileName,
                    Headers   = new Dictionary <string, string>()
                    {
                        { "Authorization", "Bearer " + oauth.access_token }
                    }
                };

                // 2. output file
                //string outputFileNameOSS = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // avoid overriding
                //string outputFileNameOSS = string.Format("{0}_output_{1}.zip", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // avoid overriding
                string           outputFileNameOSS  = string.Format("{0}_output_{1}.zip", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // 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}&name={3}&type={4}", OAuthController.GetAppSetting("FORGE_WEBHOOK_URL"), browerConnectionId, outputFileNameOSS, Path.GetFileNameWithoutExtension(input.inputFile.FileName), fileType);
                WorkItem workItemSpec = new WorkItem()
                {
                    ActivityId = activityName,
                    Arguments  = new Dictionary <string, IArgument>()
                    {
                        { "inputFile", inputFileArgument },
                        { "outputFile", outputFileArgument },
                        { "onComplete", new XrefTreeArgument {
                              Verb = Verb.Post, Url = callbackUrl
                          } }
                    }
                };
                workItemStatus = await _designAutomation.CreateWorkItemAsync(workItemSpec);
            }
            else if (fileType == "part")
            {
                // save the file on the server
                var fileSavePath = Path.Combine(_env.ContentRootPath, Path.GetFileName(input.inputFile.FileName));
                using (var stream = new FileStream(fileSavePath, FileMode.Create)) await input.inputFile.CopyToAsync(stream);

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

                // upload file to OSS Bucket
                // 1. ensure bucket existis
                string     bucketKey = NickName.ToLower() + "-designautomation";
                BucketsApi buckets   = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                try
                {
                    PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                    await buckets.CreateBucketAsync(bucketPayload, "US");
                }
                catch { };                                                                                                                                         // in case bucket already exists
                                                                                                                                                                   // 2. upload inputFile
                string     inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // 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()
                {
                    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 }
                    }
                };

                // 2. output file
                //string outputFileNameOSS = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // avoid overriding
                string           outputFileNameOSS  = string.Format("{0}_output_{1}.ipt", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // 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}&name={3}&type={4}", OAuthController.GetAppSetting("FORGE_WEBHOOK_URL"), browerConnectionId, outputFileNameOSS, "", fileType);
                WorkItem workItemSpec = new WorkItem()
                {
                    ActivityId = activityName,
                    Arguments  = new Dictionary <string, IArgument>()
                    {
                        { "inputFile", inputFileArgument },
                        { "outputFile", outputFileArgument },
                        { "onComplete", new XrefTreeArgument {
                              Verb = Verb.Post, Url = callbackUrl
                          } }
                    }
                };
                workItemStatus = await _designAutomation.CreateWorkItemAsync(workItemSpec);
            }



            return(Ok(new { WorkItemId = workItemStatus.Id }));
        }