Esempio n. 1
0
        public async Task <IActionResult> WebhookCallback([FromBody] JObject body)
        {
            // catch any errors, we don't want to return 500
            try
            {
                string eventType = body["hook"]["event"].ToString();
                string userId    = body["hook"]["createdBy"].ToString();
                string projectId = "b." + body["payload"]["project"].ToString();//body["hook"]["hookAttribute"]["projectId"].ToString();
                string versionId = body["resourceUrn"].ToString();
                string hubId     = "b." + body["payload"]["tenant"].ToString();
                string fileName  = body["payload"]["name"].ToString();
                string extension = fileName.Split(".").Last();
                string folderUrn = body["hook"]["tenant"].ToString();
                string itemUrn   = body["payload"]["lineageUrn"].ToString();

                // do you want to filter events??
                if (eventType != "dm.version.added")
                {
                    return(Ok());
                }
                if (Config.SupportedFormats.IndexOf(extension) == -1)
                {
                    return(Ok());
                }

                // use Hangfire to schedule a job
                BackgroundJobClient metadataQueue = new BackgroundJobClient();
                IState state = new EnqueuedState("metadata");
                metadataQueue.Create(() => ModelDerivativeController.ProcessFileAsync(userId, hubId, projectId, folderUrn, itemUrn, versionId, fileName, null), state);
            }
            catch { }

            // ALWAYS return ok (200)
            return(Ok());
        }
Esempio n. 2
0
        private async Task GetItemVersionsAsync(Credentials credentials, string hubId, string folderUrn, string itemHref, PerformContext context)
        {
            await credentials.RefreshAsync();

            BackgroundJobClient metadataQueue = new BackgroundJobClient();
            IState state = new EnqueuedState("metadata");

            // the API SDK
            ItemsApi itemApi = new ItemsApi();

            itemApi.Configuration.AccessToken = credentials.TokenInternal;

            // extract the projectId & itemId from the href
            string[] idParams  = itemHref.Split('/');
            string   itemUrn   = idParams[idParams.Length - 1];
            string   projectId = idParams[idParams.Length - 3];

            var item = await itemApi.GetItemAsync(projectId, itemUrn);

            string versionUrn = (string)item.data.relationships.tip.data.id;
            string fileName   = (string)item.data.attributes.displayName;
            string extension  = fileName.Split(".").Last().ToLower();

            if (Config.SupportedFormats.IndexOf(extension) == -1)
            {
                return;
            }

            string absolutePath = string.Format("/manifest/_doc/{0}", ModelDerivativeController.Base64Encode(itemUrn));

            RestClient  client  = new RestClient(Config.ElasticSearchServer);
            RestRequest request = new RestRequest(absolutePath, RestSharp.Method.GET);

            if (!string.IsNullOrEmpty(Config.AWSKey) && !string.IsNullOrEmpty(Config.AWSSecret))
            {
                SortedDictionary <string, string> headers = AWS.Signature.SignatureHeader(
                    Amazon.RegionEndpoint.GetBySystemName(Config.AWSRegion),
                    new Uri(Config.ElasticSearchServer).Host,
                    "GET", string.Empty, absolutePath);
                foreach (var entry in headers)
                {
                    request.AddHeader(entry.Key, entry.Value);
                }
            }

            IRestResponse res = await client.ExecuteTaskAsync(request);

            if (res.StatusCode == HttpStatusCode.OK && Config.SkipAlreadyIndexed)
            {
                context.WriteLine(string.Format("{0}: already indexed, skip", fileName));
                System.Threading.Thread.Sleep(1000); // otherwise we'll reach the rate limit...
                return;
            }

            context.WriteLine(string.Format("{0}: {1}", fileName, versionUrn));

            await ModelDerivativeHub.NotifyFileFound(_hubContext, hubId);

            metadataQueue.Create(() => ProcessFile(credentials.UserId, hubId, projectId, folderUrn, itemUrn, versionUrn, fileName, null), state);
        }
Esempio n. 3
0
        public async Task ProcessFile(string userId, string hubId, string projectId, string folderUrn, string itemUrn, string versionUrn, string fileName, PerformContext console)
        {
            await ModelDerivativeController.ProcessFileAsync(userId, hubId, projectId, folderUrn, itemUrn, versionUrn, fileName, console);

            await ModelDerivativeHub.NotifyFileComplete(_hubContext, hubId);
        }