public async Task <bool> CreateBucket(string bucketKey)
        {
            if (bucketKey == null)
            {
                throw new ArgumentNullException("bucketKey");
            }

            var isAuthorized = await IsAuthorized();

            if (!isAuthorized)
            {
                _logger.Log(LogType.Error, "Cannot be authorized for creating bucket");
                return(false);
            }

            var bucketsApi = new BucketsApi {
                Configuration = { AccessToken = AuthenticationToken.AccessToken }
            };
            var postBuckets = new PostBucketsPayload(bucketKey.ToLower(), null,
                                                     PostBucketsPayload.PolicyKeyEnum.Transient);

            try
            {
                await bucketsApi.CreateBucketAsync(postBuckets);
            }
            catch (Exception e)
            {
                _logger.Log(LogType.Error, "Exception when creating the Bucket: " + e);
                return(false);
            }

            return(true);
        }
Пример #2
0
        public async Task <dynamic> CreateBucket([FromBody] BucketModel bucket)
        {
            dynamic    NewBucket = null;
            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);

            try
            {
                NewBucket = await buckets.CreateBucketAsync(bucketPayload, bucketRegion);
            }
            catch (Exception e)
            {
                if (e.Message == "Error calling CreateBucket: {\"reason\":\"Bucket already exists\"}")
                {
                    dynamic allBuckets = await buckets.GetBucketsAsync("US", 100);

                    foreach (KeyValuePair <string, dynamic> actualBucket in new DynamicDictionaryItems(allBuckets.items))
                    {
                        string bucketName = actualBucket.Value.bucketKey;
                        if (bucketName.Contains(BucketName)) //kitchenconfig  designautomation
                        {
                            NewBucket = actualBucket;
                        }
                    }
                }
            }

            return(NewBucket);
        }
Пример #3
0
        /// <summary>
        /// The RunAsync.
        /// </summary>
        /// <returns>The <see cref="Task"/>.</returns>
        public async Task RunAsync()
        {
            if (string.IsNullOrEmpty(Owner))
            {
                Console.WriteLine("Please provide non-empty Owner.");
                return;
            }

            if (string.IsNullOrEmpty(UploadUrl))
            {
                Console.WriteLine($"Creating Bucket....");
                dynamic oauth = await GetInternalAsync();

                // 1. ensure bucket existis
                string bucketKey = Owner.ToLower();
                Console.WriteLine($"Creating Bucket {bucketKey}....");
                BucketsApi buckets = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                try
                {
                    PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                    dynamic            bucketsRes    = await buckets.CreateBucketAsync(bucketPayload, "US");
                }
                catch
                {
                    // in case bucket already exists
                    Console.WriteLine($"\tBucket {bucketKey} exists");
                };
                ObjectsApi objects = new ObjectsApi();
                objects.Configuration.AccessToken = oauth.access_token;

                //2. Upload input file and get signed URL
                string inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(FilePaths.InputFile));
                Console.WriteLine($"Uploading input file {inputFileNameOSS} to {bucketKey}..... ");
                using (StreamReader streamReader = new StreamReader(FilePaths.InputFile))
                {
                    dynamic res = await objects.UploadObjectAsync(bucketKey, inputFileNameOSS, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream");
                }



                try
                {
                    PostBucketsSigned bucketsSigned = new PostBucketsSigned(60);
                    dynamic           signedResp    = await objects.CreateSignedResourceAsync(bucketKey, inputFileNameOSS, bucketsSigned, "read");

                    downloadUrl = signedResp.signedUrl;
                    Console.WriteLine($"\tSuccess: File uploaded to... \n\t{downloadUrl}");
                }
                catch { }
            }

            if (!await SetupOwnerAsync())
            {
                Console.WriteLine("Exiting.");
                return;
            }

            await SubmitWorkItemAsync(ActivityName);
        }
Пример #4
0
 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");
 }
        public async Task <dynamic> CreateBucketAsync([FromBody] BucketModel bucket)
        {
            BucketsApi buckets = new BucketsApi();
            dynamic    token   = await OAuthController.GetInternalAsync();

            buckets.Configuration.AccessToken = token.access_token;
            PostBucketsPayload bucketPayload = new PostBucketsPayload(bucket.bucketKey, null, bucket.policyKey);

            return(await buckets.CreateBucketAsync(bucketPayload, "US"));
        }
Пример #6
0
        /// <summary>
        /// Create a bucket.
        /// </summary>
        private async static Task <dynamic> CreateBucket()
        {
            string             bucketKey  = "forgeapp" + Guid.NewGuid().ToString("N").ToLower();
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient /* erase after 24h*/);
            BucketsApi         bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = InternalToken.access_token;      //bearer is returned from 2 legged token
            dynamic newBucket = await bucketsApi.CreateBucketAsync(postBucket);

            return(newBucket);
        }
        /// <summary>
        /// Creates Bucket
        /// </summary>
        /// <returns>Newly created bucket</returns>
        private async static Task <dynamic> CreateBucket()
        {
            string             bucketKey  = "inventorio" + Guid.NewGuid().ToString("N").ToLower();
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
            BucketsApi         bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = InternalToken.access_token;
            dynamic newBucket = await bucketsApi.CreateBucketAsync(postBucket);

            return(newBucket);
        }
Пример #8
0
        public async Task RunAsync()
        {
            if (string.IsNullOrEmpty(Owner))
            {
                Console.WriteLine("Please provide non-empty Owner.");
                return;
            }

            if (string.IsNullOrEmpty(UploadUrl))
            {
                Console.WriteLine("Creating Bucket and OSS Object");


                dynamic oauth = await GetInternalAsync();

                // 1. ensure bucket existis
                string     bucketKey = Owner.ToLower();
                BucketsApi buckets   = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                dynamic bucketsRes = null;
                try
                {
                    PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                    bucketsRes = await buckets.CreateBucketAsync(bucketPayload, "US");
                }
                catch {
                    Console.WriteLine($"\tBucket {bucketKey} exists");
                }; // in case bucket already exists
                string     outputFileNameOSS = "output.zip";
                ObjectsApi objects           = new ObjectsApi();
                objects.Configuration.AccessToken = oauth.access_token;
                try
                {
                    PostBucketsSigned bucketsSigned = new PostBucketsSigned(60);
                    dynamic           signedResp    = await objects.CreateSignedResourceAsync(bucketKey, outputFileNameOSS, bucketsSigned, "readwrite");

                    UploadUrl = signedResp.signedUrl;
                }
                catch {}
            }

            if (!await SetupOwnerAsync())
            {
                Console.WriteLine("Exiting.");
                return;
            }

            var myApp = await SetupAppBundleAsync();

            var myActivity = await SetupActivityAsync(myApp);

            await SubmitWorkItemAsync(myActivity);
        }
Пример #9
0
        private async void btnCreateBucket_Click(object sender, EventArgs e)
        {
            string bucketKey = string.Empty;

            if (Prompt.ShowDialog("Enter bucket name: ", "Create new bucket", txtClientId.Text.ToLower() + DateTime.Now.Ticks.ToString(), out bucketKey) == DialogResult.OK)
            {
                BucketsApi buckets = new BucketsApi();
                buckets.Configuration.AccessToken = AccessToken;
                PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey.ToLower(), null, PostBucketsPayload.PolicyKeyEnum.Transient);
                await buckets.CreateBucketAsync(bucketPayload, cmbRegion.Text);

                btnRefreshToken_Click(null, null);
            }
        }
        private async Task <dynamic> CreateBucketsApi(BucketsApi bucketsApi, string bucketKey)
        {
            try
            {
                PostBucketsPayload postBucketsPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                var result = await bucketsApi.CreateBucketAsync(postBucketsPayload, "US");

                return(result);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Пример #11
0
        public async Task <dynamic> CreateBucket([FromBody] CreateBucketModel bucket)
        {
            BucketsApi buckets = new BucketsApi();
            //
            Credentials credentials = await Credentials.FromSessionAsync(base.Request.Cookies, Response.Cookies);

            //
            //dynamic token = await OAuthController.GetInternalAsync();
            buckets.Configuration.AccessToken = credentials.TokenInternal;
            PostBucketsPayload bucketPayload = new PostBucketsPayload(bucket.bucketKey, null,
                                                                      PostBucketsPayload.PolicyKeyEnum.Transient);

            return(await buckets.CreateBucketAsync(bucketPayload, "US"));
        }
        public async Task <dynamic> CreateBucket([FromBody] CreateBucketModel bucket)
        {
            if (!Utility.Buckets.IsValidBucketKey(bucket.bucketKey))
            {
                return(null);
            }

            BucketsApi buckets = new BucketsApi();
            dynamic    token   = await Utility.OAuth.Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate });

            buckets.Configuration.AccessToken = token.access_token;
            PostBucketsPayload bucketPayload = new PostBucketsPayload(bucket.bucketKey, null, bucket.policyKey);

            return(await buckets.CreateBucketAsync(bucketPayload, Enum.GetName(typeof(Region), bucket.region)));
        }
Пример #13
0
        /// <summary>
        /// Creates Bucket
        /// </summary>
        /// <returns>Newly created bucket</returns>
        public async static Task <dynamic> CreateBucket()
        {
            string     bucketKey  = "inventorilogicda" + ConsumerKey.ToLower();
            BucketsApi bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = InternalToken.access_token;
            dynamic buckets = await bucketsApi.GetBucketsAsync();

            bool bucketExists = buckets.items.ToString().Contains(bucketKey);

            if (!bucketExists)
            {
                PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                dynamic            newbucket  = await bucketsApi.CreateBucketAsync(postBucket);
            }
            return(bucketKey);
        }
        public async Task <IActionResult> Upload([FromForm] StartWorkitemInput input)
        {
            JObject workItemData = JObject.Parse(input.data);

            browserConnectionId = workItemData["browserConnectionId"].Value <string>();

            var fileSavePath = Path.Combine(_env.ContentRootPath, Path.GetFileName(input.inputFile.FileName));

            using (var stream = new FileStream(fileSavePath, FileMode.Create))
            {
                await input.inputFile.CopyToAsync(stream);
            }
            dynamic oauth = await GetInternalASync();

            // upload file to OSS Bucket
            // 1. ensure bucket exists
            BucketKey = String.Format("{0}_{1}", BucketKey, DateTime.Now.ToString("yyyyMMddhhmmss").ToLower());
            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
            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;


            inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), Path.GetFileName(input.inputFile.FileName)); // avoid overriding
            object res = default;

            using (StreamReader streamReader = new StreamReader(fileSavePath))
            {
                res = await objects.UploadObjectAsync(BucketKey, inputFileNameOSS, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream");
            }
            var json     = JsonConvert.SerializeObject(res, Formatting.Indented);
            var response = Response;
            await response.WriteJsonAsync(json);

            // delete server copy
            System.IO.File.Delete(fileSavePath);
            return(new EmptyResult());
        }
Пример #15
0
        public async Task <IActionResult> UploadOssFiles([FromBody] JObject appBundleSpecs)
        {
            if (OAuthController.GetAppSetting("DISABLE_SETUP") == "true")
            {
                return(Unauthorized());
            }

            System.Diagnostics.Debug.WriteLine("UploadOssFiles");
            // OAuth token
            dynamic oauth = await OAuthController.GetInternalAsync();

            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;

            DerivativesApi derivatives = new DerivativesApi();

            derivatives.Configuration.AccessToken = oauth.access_token;

            // upload file to OSS Bucket
            // 1. ensure bucket existis
            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

            string [] filePaths = System.IO.Directory.GetFiles(LocalFilesFolder);
            foreach (string filePath in filePaths)
            {
                string fileName = System.IO.Path.GetFileName(filePath);
                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    dynamic res = await objects.UploadObjectAsync(BucketKey, fileName, (int)streamReader.BaseStream.Length, streamReader.BaseStream, "application/octet-stream");

                    TranslateFile(res.objectId, null);
                }
            }

            return(Ok());
        }
Пример #16
0
        private async Task EnsureBucketExists(string bucketKey)
        {
            var auth = await GetAccessToken(INTERNAL_TOKEN_SCOPES);

            var client = new BucketsApi();

            client.Configuration.AccessToken = auth.AccessToken;
            var buckets = await client.GetBucketsAsync();

            foreach (KeyValuePair <string, dynamic> bucket in new DynamicDictionaryItems(buckets.items))
            {
                if (bucket.Value.bucketKey == bucketKey)
                {
                    return;
                }
            }
            await client.CreateBucketAsync(new PostBucketsPayload { BucketKey = bucketKey, PolicyKey = PostBucketsPayload.PolicyKeyEnum.Temporary });
        }
        /// <summary>
        /// Create Bucket
        /// </summary>
        private async Task <IActionResult> CreateBucket()
        {
            dynamic oauth = await OAuthController.GetInternalAsync();

            string     bucketkey  = "inventorilogicda" + nickName.ToLower();
            BucketsApi bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = oauth.access_token;
            dynamic buckets = await bucketsApi.GetBucketsAsync();

            bool bucketExists = buckets.items.ToString().Contains(bucketkey);

            if (!bucketExists)
            {
                PostBucketsPayload postBucket = new PostBucketsPayload(bucketkey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                dynamic            newbucket  = await bucketsApi.CreateBucketAsync(postBucket);
            }
            return(Ok());
        }
Пример #18
0
        private async Task <bool> UploadToBucket(string bucketKey, string objectName, string filePath)
        {
            // get the bucket...
            dynamic oauth = await OAuthController2L.GetInternalAsync();

            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;

            // upload file to OSS Bucket
            // 1. ensure bucket existis
            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

            // upload the file/object, which will create a new object

            dynamic uploadedObj;

            using (StreamReader streamReader = new StreamReader(filePath))
            {
                uploadedObj = await objects.UploadObjectAsync(
                    bucketKey,
                    objectName,
                    (int)streamReader.BaseStream.Length,
                    streamReader.BaseStream,
                    "application/octet-stream");
            }

            // cleanup
            System.IO.File.Delete(filePath);

            return(true);
        }
Пример #19
0
        protected async Task <XrefTreeArgument> BuildUploadURL(string resultFilename)
        {
            BucketsApi buckets = new BucketsApi();
            dynamic    token   = await Credentials.Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate, Scope.DataWrite });

            buckets.Configuration.AccessToken = token.access_token;
            PostBucketsPayload bucketPayload = new PostBucketsPayload(Utils.BucketName, null, PostBucketsPayload.PolicyKeyEnum.Transient);

            try
            {
                await buckets.CreateBucketAsync(bucketPayload, "US");
            }
            catch { }

            ObjectsApi objects   = new ObjectsApi();
            dynamic    signedUrl = await objects.CreateSignedResourceAsyncWithHttpInfo(Utils.BucketName, resultFilename, new PostBucketsSigned(5), "readwrite");

            return(new XrefTreeArgument()
            {
                Url = (string)(signedUrl.Data.signedUrl),
                Verb = Verb.Put
            });
        }
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            // basic input validation
            JObject workItemData       = JObject.Parse(input.data);
            string  widthParam         = workItemData["width"].Value <string>();
            string  heigthParam        = workItemData["height"].Value <string>();
            string  activityName       = string.Format("{0}.{1}", NickName, workItemData["activityName"].Value <string>());
            string  browerConnectionId = workItemData["browerConnectionId"].Value <string>();

            // 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. input json
            dynamic inputJson = new JObject();

            inputJson.Width  = widthParam;
            inputJson.Height = heigthParam;
            XrefTreeArgument inputJsonArgument = new XrefTreeArgument()
            {
                Url = "data:application/json, " + ((JObject)inputJson).ToString(Formatting.None).Replace("\"", "'")
            };
            // 3. output file
            string           outputFileNameOSS  = string.Format("{0}_output_{1}", 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}", OAuthController.GetAppSetting("FORGE_WEBHOOK_URL"), browerConnectionId, outputFileNameOSS);
            WorkItem workItemSpec = new WorkItem()
            {
                ActivityId = activityName,
                Arguments  = new Dictionary <string, IArgument>()
                {
                    { "inputFile", inputFileArgument },
                    { "inputJson", inputJsonArgument },
                    { "outputFile", outputFileArgument },
                    { "onComplete", new XrefTreeArgument {
                          Verb = Verb.Post, Url = callbackUrl
                      } }
                }
            };
            WorkItemStatus workItemStatus = await _designAutomation.CreateWorkItemAsync(workItemSpec);

            return(Ok(new { WorkItemId = workItemStatus.Id }));
        }
        public async Task CloneBucket()
        {
            var source = await _bucketsApi.CreateBucketAsync(GenerateName("robot sensor"), RetentionRule(), _organization);

            var properties = new Dictionary <string, string> {
                { "color", "green" }, { "location", "west" }
            };

            var label = await Client.GetLabelsApi()
                        .CreateLabelAsync(GenerateName("Cool Resource"), properties, _organization.Id);

            await _bucketsApi.AddLabelAsync(label, source);

            var name = GenerateName("cloned");

            var cloned = await _bucketsApi.CloneBucketAsync(name, source);

            Assert.AreEqual(name, cloned.Name);
            Assert.AreEqual(_organization.Id, cloned.OrgID);
            Assert.IsNull(cloned.Rp);
            Assert.AreEqual(1, cloned.RetentionRules.Count);
            Assert.AreEqual(3600, cloned.RetentionRules[0].EverySeconds);
            Assert.AreEqual(BucketRetentionRules.TypeEnum.Expire, cloned.RetentionRules[0].Type);

            var labels = await _bucketsApi.GetLabelsAsync(cloned);

            Assert.AreEqual(1, labels.Count);
            Assert.AreEqual(label.Id, labels[0].Id);
        }
Пример #22
0
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            // basic input validation
            JObject workItemData       = JObject.Parse(input.data);
            string  widthParam         = workItemData["width"].Value <string>();
            string  heigthParam        = workItemData["height"].Value <string>();
            string  activityName       = string.Format("{0}.{1}", NickName, workItemData["activityName"].Value <string>());
            string  browerConnectionId = workItemData["browerConnectionId"].Value <string>();

            // save the file on the server
            var fileSavePath = Path.Combine(_env.ContentRootPath, 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"), 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
            JObject inputFileArgument = new JObject
            {
                new JProperty("url", string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, inputFileNameOSS)),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + oauth.access_token)
                })
            };
            // 2. input json
            dynamic inputJson = new JObject();

            inputJson.Width  = widthParam;
            inputJson.Height = heigthParam;
            JObject inputJsonArgument = new JObject {
                new JProperty("url", "data:application/json, " + ((JObject)inputJson).ToString(Formatting.None).Replace("\"", "'"))
            };                                                                                                                               // ToDo: need to improve this
            // 3. output file
            string  outputFileNameOSS  = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), input.inputFile.FileName); // avoid overriding
            JObject outputFileArgument = new JObject
            {
                new JProperty("verb", "PUT"),
                new JProperty("url", string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, outputFileNameOSS)),
                new JProperty("headers",
                              new JObject {
                    new JProperty("Authorization", "Bearer " + oauth.access_token)
                })
            };

            // prepare & submit workitem
            string   callbackUrl  = string.Format("{0}/api/forge/callback/designautomation?id={1}", OAuthController.GetAppSetting("FORGE_WEBHOOK_CALLBACK_HOST"), browerConnectionId);
            WorkItem workItemSpec = new WorkItem(
                null, activityName,
                new Dictionary <string, JObject>()
            {
                { "inputFile", inputFileArgument },
                { "inputJson", inputJsonArgument },
                { "outputFile", outputFileArgument },
                //{ "onProgress", new JObject { new JProperty("verb", "POST"), new JProperty("url", callbackUrl) }},
                { "onComplete", new JObject {
                      new JProperty("verb", "POST"), new JProperty("url", callbackUrl)
                  } }
            },
                null);
            WorkItemsApi workItemApi = new WorkItemsApi();

            workItemApi.Configuration.AccessToken = oauth.access_token;;
            WorkItemStatus newWorkItem = await workItemApi.WorkItemsCreateWorkItemsAsync(null, null, workItemSpec);

            return(Ok(new { WorkItemId = newWorkItem.Id }));
        }
Пример #23
0
        /// <summary>
        /// The RunAsync.
        /// </summary>
        /// <returns>The <see cref="Task"/>.</returns>
        public async Task RunAsync()
        {
            if (string.IsNullOrEmpty(Owner))
            {
                Console.WriteLine("Please provide non-empty Owner.");
                return;
            }

            if (string.IsNullOrEmpty(UploadUrl))
            {
                Console.WriteLine("Creating Bucket and OSS Object");


                dynamic oauth = await GetInternalAsync();

                // 1. ensure bucket exists

                BucketsApi buckets = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                try
                {
                    PostBucketsPayload bucketPayload = new PostBucketsPayload(BucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                    dynamic            bucketsRes    = await buckets.CreateBucketAsync(bucketPayload, "US");
                }
                catch
                {
                    // in case bucket already exists
                    Console.WriteLine($"\tBucket {BucketKey} exists");
                };
                ObjectsApi objectsApi = new ObjectsApi();
                objectsApi.Configuration.AccessToken = oauth.access_token;



                //2. Upload input file and get signed URL

                long fileSize       = (new FileInfo(FilePaths.InputFile)).Length;
                long chunkSize      = 2 * 1024 * 1024; // 100Kb
                int  numberOfChunks = (int)Math.Round((double)(fileSize / chunkSize)) + 1;
                var  options        = new ProgressBarOptions
                {
                    ProgressCharacter   = '#',
                    ProgressBarOnBottom = false,
                    ForegroundColorDone = ConsoleColor.Green,
                    ForegroundColor     = ConsoleColor.White
                };

                using var pbar = new ProgressBar(numberOfChunks, $"Uploading input file {inputFileNameOSS} to {BucketKey}..... ", options);
                long start = 0;
                chunkSize = (numberOfChunks > 1 ? chunkSize : fileSize);
                long   end       = chunkSize;
                string sessionId = Guid.NewGuid().ToString();
                // upload one chunk at a time
                using BinaryReader reader = new BinaryReader(new FileStream(FilePaths.InputFile, FileMode.Open));
                for (int chunkIndex = 0; chunkIndex < numberOfChunks; chunkIndex++)
                {
                    string range = string.Format("bytes {0}-{1}/{2}", start, end, fileSize);

                    long         numberOfBytes = chunkSize + 1;
                    byte[]       fileBytes     = new byte[numberOfBytes];
                    MemoryStream memoryStream  = new MemoryStream(fileBytes);
                    reader.BaseStream.Seek((int)start, SeekOrigin.Begin);
                    int count = reader.Read(fileBytes, 0, (int)numberOfBytes);
                    memoryStream.Write(fileBytes, 0, (int)numberOfBytes);
                    memoryStream.Position = 0;

                    dynamic chunkUploadResponse = await objectsApi.UploadChunkAsyncWithHttpInfo(BucketKey, inputFileNameOSS, (int)numberOfBytes, range, sessionId, memoryStream);

                    start     = end + 1;
                    chunkSize = ((start + chunkSize > fileSize) ? fileSize - start - 1 : chunkSize);
                    end       = start + chunkSize;
                    double size       = chunkIndex == 0 ? chunkSize / 1024 : (chunkIndex * chunkSize) / 1024;
                    var    CustomText = $"{(fileSize-chunkSize)/ 1024} Kb uploaded...";
                    pbar.Tick(CustomText);
                }



                try
                {
                    PostBucketsSigned bucketsSigned = new PostBucketsSigned(60);
                    dynamic           signedResp    = await objectsApi.CreateSignedResourceAsync(BucketKey, inputFileNameOSS, bucketsSigned, "read");

                    DownloadUrl = signedResp.signedUrl;
                    signedResp  = await objectsApi.CreateSignedResourceAsync(BucketKey, outputFileNameOSS, bucketsSigned, "readwrite");

                    UploadUrl = signedResp.signedUrl;
                    Console.WriteLine($"\tSuccess: signed resource for input.zip created!\n\t{DownloadUrl}");
                    Console.WriteLine($"\tSuccess: signed resource for result.pdf created!\n\t{UploadUrl}");
                }
                catch { }
            }

            if (!await SetupOwnerAsync())
            {
                Console.WriteLine("Exiting.");
                return;
            }

            var myApp = await SetupAppBundleAsync();

            var myActivity = await SetupActivityAsync(myApp);

            await SubmitWorkItemAsync(myActivity);
        }
Пример #24
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 }));
        }
Пример #25
0
        protected async void uploadAndTranslate(object sender, EventArgs e)
        {
            // create a randomg bucket name (fixed prefix + randomg guid)
            string bucketKey = "forgeapp" + Guid.NewGuid().ToString("N").ToLower();

            // upload the file (to your server)
            string fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), bucketKey, FileUpload1.FileName);

            Directory.CreateDirectory(Path.GetDirectoryName(fileSavePath));
            FileUpload1.SaveAs(fileSavePath);

            // get a write enabled token
            TwoLeggedApi oauthApi = new TwoLeggedApi();
            dynamic      bearer   = await oauthApi.AuthenticateAsync(
                WebConfigurationManager.AppSettings["FORGE_CLIENT_ID"],
                WebConfigurationManager.AppSettings["FORGE_CLIENT_SECRET"],
                "client_credentials",
                new Scope[] { Scope.BucketCreate, Scope.DataCreate, Scope.DataWrite, Scope.DataRead });

            // create the Forge bucket
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient /* erase after 24h*/);
            BucketsApi         bucketsApi = new BucketsApi();

            bucketsApi.Configuration.AccessToken = bearer.access_token;
            dynamic newBucket = await bucketsApi.CreateBucketAsync(postBucket);

            // upload file (a.k.a. Objects)
            ObjectsApi objectsApi = new ObjectsApi();

            oauthApi.Configuration.AccessToken = bearer.access_token;
            dynamic newObject;

            using (StreamReader fileStream = new StreamReader(fileSavePath))
            {
                newObject = await objectsApi.UploadObjectAsync(bucketKey, FileUpload1.FileName,
                                                               (int)fileStream.BaseStream.Length, fileStream.BaseStream,
                                                               "application/octet-stream");
            }

            // translate file
            string objectIdBase64 = ToBase64(newObject.objectId);
            List <JobPayloadItem> postTranslationOutput = new List <JobPayloadItem>()
            {
                new JobPayloadItem(
                    JobPayloadItem.TypeEnum.Svf /* Viewer*/,
                    new List <JobPayloadItem.ViewsEnum>()
                {
                    JobPayloadItem.ViewsEnum._3d,
                    JobPayloadItem.ViewsEnum._2d
                })
            };
            JobPayload postTranslation = new JobPayload(
                new JobPayloadInput(objectIdBase64),
                new JobPayloadOutput(postTranslationOutput));
            DerivativesApi derivativeApi = new DerivativesApi();

            derivativeApi.Configuration.AccessToken = bearer.access_token;
            dynamic translation = await derivativeApi.TranslateAsync(postTranslation);

            // check if is ready
            int progress = 0;

            do
            {
                System.Threading.Thread.Sleep(1000); // wait 1 second
                try
                {
                    dynamic manifest = await derivativeApi.GetManifestAsync(objectIdBase64);

                    progress = (string.IsNullOrWhiteSpace(Regex.Match(manifest.progress, @"\d+").Value) ? 100 : Int32.Parse(Regex.Match(manifest.progress, @"\d+").Value));
                }
                catch (Exception ex)
                {
                }
            } while (progress < 100);

            // ready!!!!

            // register a client-side script to show this model
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowModel", string.Format("<script>showModel('{0}');</script>", objectIdBase64));

            // clean up
            Directory.Delete(Path.GetDirectoryName(fileSavePath), true);
        }
Пример #26
0
        public async Task <IActionResult> StartWorkitem([FromForm] StartWorkitemInput input)
        {
            try
            {
                DataSetBuilder dataSetBuilder = new DataSetBuilder(LocalDataSetFolder, "DataSet");
                dataSetBuilder.SaveJsonData(input.shelfData, "params.json");
                dataSetBuilder.ZipFolder("MyWallShelf.zip");
            }
            catch (Exception ex)
            {
                return(Ok(new { WorkItemId = ex.Message }));;
            }

            JObject connItemData       = JObject.Parse(input.forgeData);
            string  uniqueActivityName = string.Format("{0}.{1}+{2}", NickName, ActivityName, Alias);
            string  browerConnectionId = connItemData["browerConnectionId"].Value <string>();

            // TODO - this piece of cod will be used for sending picture in Visualization module
            // save the file on the server
            var fileSavePath = Path.Combine(LocalDataSetFolder, "MyWallShelf.zip");
            //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
            BucketsApi buckets = new BucketsApi();

            buckets.Configuration.AccessToken = oauth.access_token;
            try
            {
                PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                await buckets.CreateBucketAsync(bucketPayload, bucketRegion);                                                     //TODO - use EMEA buckets also
            }
            catch { };                                                                                                            // in case bucket already exists
                                                                                                                                  // 2. upload inputFile
            string     inputFileNameOSS = string.Format("{0}_input_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), "MyShelf.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 = "Wall_shelf",
                PathInZip = "MyWallShelf.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 }
                }
            };
            // 2. input json

            /*dynamic inputJson = new JObject();
             * inputJson.Width = widthParam;
             * inputJson.Height = heigthParam;
             * XrefTreeArgument inputJsonArgument = new XrefTreeArgument()
             * {
             *  Url = "data:application/json, " + ((JObject)inputJson).ToString(Formatting.None).Replace("\"", "'")
             * };*/
            // 3. output file
            // TODO - output file name should be passed from client
            string           outputFileNameOSS  = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), "Result.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 }
                }
            };
            // 3a. output pdf fajl out of zipping
            string           outputPDFFileNameOSS  = string.Format("{0}_output_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), "Result.pdf"); // avoid overriding
            XrefTreeArgument outputPDFFileArgument = new XrefTreeArgument()
            {
                Url     = string.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, outputPDFFileNameOSS),
                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://webwallshelfbuilder.herokuapp.com",
                browerConnectionId,
                outputFileNameOSS);
            WorkItem workItemSpec = new WorkItem()
            {
                ActivityId = uniqueActivityName,
                Arguments  = new Dictionary <string, IArgument>()
                {
                    { "inputFile", inputFileArgument },
                    //{ "inputJson",  inputJsonArgument },
                    { "outputFile", outputFileArgument },
                    { "outputPDFFile", outputPDFFileArgument },
                    { "onComplete", new XrefTreeArgument {
                          Verb = Verb.Post, Url = callbackUrl
                      } }
                }
            };

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

                return(Ok(new { WorkItemId = workItemStatus.Id }));
            }
            catch (Exception e)
            {
                return(Ok(new { WorkItemId = e.Message }));
            }
        }
Пример #27
0
        static async Task Main(string[] args)
        {
            ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddFilter("Microsoft", LogLevel.Warning)
                .AddFilter("System", LogLevel.Warning)
                .AddFilter("phetracker.Program", LogLevel.Debug)
                .AddConsole(c =>
                {
                    c.TimestampFormat = "[HH:mm:ss] ";
                });
            });
            ILogger logger = loggerFactory.CreateLogger <Program>();

            logger.LogInformation("Init services....");
            populationData = LoadPopulationData();
            Maps maps       = new Maps();
            var  trustsData = maps.GetTrustsForLtlas();

            logger.LogInformation("Starting...");
            string dataFilePath = "datacache.json";

            logger.LogInformation("Downloading data from API");
            APIClient client    = new APIClient(logger);
            CaseData  data      = new CaseData();
            var       trustData = await client.GetNHSTrustData();

            var processedTrustData = new List <NhsTrustDataPoint>();

            if (System.IO.File.Exists(dataFilePath))
            {
                logger.LogInformation("Using Cached data delete data.json to load fresh data");
                data = JsonConvert.DeserializeObject <CaseData>(System.IO.File.ReadAllText(dataFilePath));
            }
            else
            {
                var caseDataV1 = await client.GetLTLAData(logger);

                data.ltlas    = new List <CaseRecord>();
                data.metadata = new Metadata();
                foreach (var item in caseDataV1)
                {
                    data.ltlas.Add(new CaseRecord()
                    {
                        kind     = "localAuthorityCases",
                        areaCode = item.areaCode,
                        areaName = item.areaName,
                        dailyLabConfirmedCases = item.newCasesBySpecimenDate,
                        specimenDate           = item.date,
                    });
                }

                var caseDataString = JsonConvert.SerializeObject(data);
                System.IO.File.WriteAllText(dataFilePath, caseDataString);
            }

            // always process the hospital data
            // Build trust data structure
            var trustDataDictionary = trustData.GroupBy(y => y.areaName).ToDictionary(y => y.Key, z =>
            {
                return(z.ToDictionary(v => v.date));
            });

            foreach (var ltla in data.ltlas.GroupBy(y => y.areaName))
            {
                try
                {
                    var hospitals = trustsData[ltla.Key];
                    foreach (var hospital in hospitals)
                    {
                        var hospitalData = trustDataDictionary[hospital.OrganisationName];
                        foreach (var dataPoint in hospitalData)
                        {
                            Console.WriteLine("Adding datapoint for ltal" + ltla.Key);
                            dataPoint.Value.ltlaName = ltla.Key;
                            processedTrustData.Add(dataPoint.Value);
                        }
                    }
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Failed to find hospitals for ltla:" + ltla.Key);
                }
            }

            // Combine and dedupe (for regions) case data
            var allCaseData = data.ltlas;

            var pheBucketName = "pheCovidData";
            var orgId         = "05d0f71967e52000";
            var Token         = "hYf9V7UQge2McZNCTKerUkPwLvqncruS2hczL9jJX3ohSP-zJ7Yt1Za5J33qNcKkn_rI7pU3SiWHbV5waBt4dA==".ToCharArray();

            using (var influxDBClient = InfluxDBClientFactory.Create("http://localhost:9999", Token))
                using (var writeApi = influxDBClient.GetWriteApi())
                {
                    BucketsApi bucketsApi = influxDBClient.GetBucketsApi();
                    try
                    {
                        var pheBucketInstance = await bucketsApi.FindBucketByNameAsync(pheBucketName);

                        await bucketsApi.DeleteBucketAsync(pheBucketInstance);
                    }
                    catch (Exception e)
                    {
                        logger.LogError("Failed to delete bucket, may not exist" + e.ToString(), e);
                    }
                    var bucket = await bucketsApi.CreateBucketAsync(pheBucketName, orgId);

                    var points = new List <PointData>(allCaseData.Count);

                    foreach (var record in allCaseData)
                    {
                        var recordPer100k = ConvertToPer100kValue(record);
                        var point         = PointData.Measurement("confirmedCases")
                                            .Tag("localAuth", record.areaName)
                                            .Tag("localAuthKind", record.kind)
                                            .Tag("localAuthCode", record.areaCode)
                                            .Field("daily", record.dailyLabConfirmedCases ?? 0)
                                            .Field("daily100k", recordPer100k?.dailyLabConfirmedCases ?? 0)
                                            .Field("total", record.dailyTotalLabConfirmedCasesRate)
                                            .Field("total100k", recordPer100k?.dailyTotalLabConfirmedCasesRate ?? 0)
                                            .Timestamp(record.specimenDate.ToUniversalTime(), WritePrecision.S);
                        points.Add(point);
                    }

                    foreach (var record in processedTrustData)
                    {
                        var point = PointData.Measurement("trustData")
                                    .Tag("localAuth", record.ltlaName)
                                    .Tag("trust", record.areaName)
                                    .Field("newAdmissions", record.newAdmissions ?? 0)
                                    .Field("cases", record.hospitalCases ?? 0)
                                    .Field("mvBeds", record.covidOccupiedMVBeds ?? 0)
                                    .Timestamp(record.date.ToUniversalTime(), WritePrecision.S);
                        points.Add(point);
                    }
                    writeApi.WritePoints(pheBucketName, orgId, points);
                }
        }
Пример #28
0
        public async Task <IActionResult> UploadOssFiles([FromBody] JObject appBundleSpecs)
        {
            if (OAuthController.GetAppSetting("DISABLE_SETUP") == "true")
            {
                return(Unauthorized());
            }

            System.Diagnostics.Debug.WriteLine("UploadOssFiles");
            // OAuth token
            dynamic oauth = await OAuthController.GetInternalAsync();

            ObjectsApi objects = new ObjectsApi();

            objects.Configuration.AccessToken = oauth.access_token;

            DerivativesApi derivatives = new DerivativesApi();

            derivatives.Configuration.AccessToken = oauth.access_token;

            // upload file to OSS Bucket
            // 1. ensure bucket existis
            BucketsApi buckets = new BucketsApi();

            buckets.Configuration.AccessToken = oauth.access_token;
            try
            {
                PostBucketsPayload bucketPayload = new PostBucketsPayload(PersistentBucketKey, null, PostBucketsPayload.PolicyKeyEnum.Persistent);
                await buckets.CreateBucketAsync(bucketPayload, "US");
            }
            catch { }; // in case bucket already exists
            try
            {
                PostBucketsPayload bucketPayload = new PostBucketsPayload(TransientBucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                await buckets.CreateBucketAsync(bucketPayload, "US");
            }
            catch { }; // in case bucket already exists

            string [] filePaths = System.IO.Directory.GetFiles(LocalFilesFolder);
            foreach (string filePath in filePaths)
            {
                string fileName = System.IO.Path.GetFileName(filePath);
                string objectId = await UploadFile(PersistentBucketKey, filePath);

                System.Diagnostics.Debug.WriteLine("Translating " + fileName);
                _ = TranslateFile(objectId, fileName.Replace(".zip", ""));
            }

            DerivativeWebhooksApi webhooks = new DerivativeWebhooksApi();

            webhooks.Configuration.AccessToken = oauth.access_token;

            dynamic webhookRes = await webhooks.GetHooksAsync(DerivativeWebhookEvent.ExtractionFinished);

            foreach (KeyValuePair <string, dynamic> item in new DynamicDictionaryItems(webhookRes.data))
            {
                Guid hookId = new Guid(item.Value.hookId);
                System.Diagnostics.Debug.WriteLine("Deleting webhook, hookId " + hookId);
                await webhooks.DeleteHookAsync(DerivativeWebhookEvent.ExtractionFinished, hookId);
            }

            string callbackComplete = string.Format(
                "{0}/api/forge/callback/ontranslated",
                OAuthController.GetAppSetting("FORGE_WEBHOOK_URL")
                );

            System.Diagnostics.Debug.WriteLine("Creating webhook with workflowId = " + WorkflowId);
            dynamic res = await webhooks.CreateHookAsync(DerivativeWebhookEvent.ExtractionFinished, callbackComplete, WorkflowId);

            System.Diagnostics.Debug.WriteLine("Created webhook");

            return(Ok());
        }
Пример #29
0
        public async Task <IActionResult> Post(IFormFile file)
        {
            string fileName     = "Swisscom.rvt";
            string bucketKey    = "forgeapp" + Guid.NewGuid().ToString("N").ToLower();
            string tempFilePath = _env.ContentRootPath + "/wwwroot/layout/Swisscom_detached.rvt";

            //Get Token
            TwoLeggedApi oauthApi = new TwoLeggedApi();

            dynamic bearer = await oauthApi.AuthenticateAsync(
                "mUAnGJsDnZAALOTZdNGDcV68ReVuscXO",
                "coCCQ99xevcPpLjD",
                //_developer.forgeClientId,
                //_developer.forgeClientSecret,
                "client_credentials",
                new Scope[] { Scope.BucketCreate, Scope.DataCreate, Scope.DataWrite, Scope.DataRead });

            //Create Forge Bucket
            PostBucketsPayload postBucket = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
            BucketsApi         bucketApi  = new BucketsApi();

            bucketApi.Configuration.AccessToken = bearer.access_token;

            dynamic newBucket = await bucketApi.CreateBucketAsync(postBucket);

            //Upload File
            ObjectsApi objectsApi = new ObjectsApi();

            oauthApi.Configuration.AccessToken = bearer.access_token;
            dynamic newObject;

            using (StreamReader fileStream = new StreamReader(tempFilePath))
            {
                newObject = await objectsApi.UploadObjectAsync(bucketKey, fileName, (int)fileStream.BaseStream.Length,
                                                               fileStream.BaseStream, "application/octet-stream"); //TODO add comment
            }

            //Translate File
            string objectIdBase64 = ToBase64(newObject.objectId);
            List <JobPayloadItem> postTranslationOutput = new List <JobPayloadItem>()
            {
                new JobPayloadItem(
                    JobPayloadItem.TypeEnum.Svf, new List <JobPayloadItem.ViewsEnum>()
                {
                    JobPayloadItem.ViewsEnum._2d,
                    JobPayloadItem.ViewsEnum._3d
                })
            };

            JobPayload postTranslation = new JobPayload(
                new JobPayloadInput(objectIdBase64),
                new JobPayloadOutput(postTranslationOutput));

            DerivativesApi derivativeApi = new DerivativesApi();

            derivativeApi.Configuration.AccessToken = bearer.access_token;
            dynamic translation = await derivativeApi.TranslateAsync(postTranslation);

            //Translation finish check
            int progress = 0;

            do
            {
                System.Threading.Thread.Sleep(1000);
                try
                {
                    dynamic manifest = await derivativeApi.GetManifestAsync(objectIdBase64);

                    progress = (string.IsNullOrEmpty(Regex.Match(manifest.progress, @"\d+").Value)
                        ? 100
                        : Int32.Parse(Regex.Match(manifest.progress, @"\d+").Value));
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error in translation: " + ex.Message);
                }
            } while (progress < 100);

            //Delete temp file
            //System.IO.File.Delete(tempFilePath);

            return(RedirectToAction("Index", new { URN = (object)objectIdBase64 }));
        }
        public async Task <string> UploadObject()
        {
            HttpRequest req = HttpContext.Current.Request;

            // we must have 1 file on the request (multiple files not handles on this sample)
            if (req.Files.Count != 1)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unexpected number of files"));
            }
            HttpPostedFile file = req.Files[0];

            // save the file on the server
            string bucketKey    = Utils.GenerateRandomBucketName();
            var    fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), bucketKey, file.FileName);

            if (!Directory.Exists(fileSavePath))
            {
                Directory.CreateDirectory(fileSavePath);
            }
            file.SaveAs(fileSavePath);

            try
            {
                // authenticate with Forge
                dynamic oauth = await Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate, Scope.DataRead, Scope.DataCreate, Scope.DataWrite });

                // create the bucket
                BucketsApi buckets = new BucketsApi();
                buckets.Configuration.AccessToken = oauth.access_token;
                PostBucketsPayload bucketPayload = new PostBucketsPayload(bucketKey, null, PostBucketsPayload.PolicyKeyEnum.Transient);
                dynamic            bucketResult  = await buckets.CreateBucketAsync(bucketPayload);

                // upload the file/object, which will create a new object
                ObjectsApi objects = new ObjectsApi();
                objects.Configuration.AccessToken = oauth.access_token;
                dynamic uploadedObj;
                using (StreamReader streamReader = new StreamReader(fileSavePath))
                {
                    uploadedObj = await objects.UploadObjectAsync(bucketKey,
                                                                  file.FileName, (int)streamReader.BaseStream.Length, streamReader.BaseStream,
                                                                  "application/octet-stream");
                }

                // start translating the file
                List <JobPayloadItem> outputs = new List <JobPayloadItem>()
                {
                    new JobPayloadItem(
                        JobPayloadItem.TypeEnum.Svf,
                        new List <JobPayloadItem.ViewsEnum>()
                    {
                        JobPayloadItem.ViewsEnum._2d,
                        JobPayloadItem.ViewsEnum._3d
                    })
                };
                JobPayload     job        = new JobPayload(new JobPayloadInput(Utils.Base64Encode(uploadedObj.objectId)), new JobPayloadOutput(outputs));
                DerivativesApi derivative = new DerivativesApi();
                derivative.Configuration.AccessToken = oauth.access_token;
                dynamic jobPosted = await derivative.TranslateAsync(job);
            }
            catch (System.Exception ex)
            {
                // for this testing app, let's throw a full descriptive expcetion,
                // which is not a good idea in production
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message + ex.InnerException));
            }

            // cleanup server
            File.Delete(fileSavePath);

            return(bucketKey);
        }