예제 #1
0
        public HttpResponseMessage Post(SnapshotConfig config)
        {
            // Validate credentials
            if (!ValidateCredentials(config.ApiId, config.Application))
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }

            // If Url is null the can´t crawl
            if (config.Url == null)
            {
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent("Url is required for crawl")
                });
            }

            // If storing snapshot then a expiration date is required
            if (config.Store && config.ExpirationDate == null)
            {
                return(new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.BadRequest,
                    Content = new StringContent("Can´t store the crawl result without an expiration date")
                });
            }

            //Create container for the app
            AzureHelper.CreateAppContainerIfNotExists(config.Application);

            if (AzureHelper.SnapshotExist(config.Application, config.Url))
            {
                // If the snapshot is expired then crawl again
                if (AzureHelper.SnapshotExpired(config.Application, config.Url))
                {
                    var result = Crawl(config.Url);

                    if (!result.Contains("Error : Unable to load url") && config.Store)
                    {
                        AzureHelper.SaveSnapshot(config.Application, config.Url, SkipMetaFragment(result));
                        AzureHelper.SetBlobAttributes(config.Application, config.Url, config.UserAgent, config.ExpirationDate);
                    }

                    return(new HttpResponseMessage()
                    {
                        Content = new StringContent(result)
                    });
                }

                // If not expired then read the stored snapshot
                return(new HttpResponseMessage()
                {
                    Content = new StringContent(AzureHelper.ReadSnapshot(config.Application, config.Url))
                });
            }
            else
            {
                // Crawl and store the snapshot
                var result = Crawl(config.Url);

                if (!result.Contains("Error : Unable to load url") && config.Store)
                {
                    AzureHelper.SaveSnapshot(config.Application, config.Url, SkipMetaFragment(result));
                    AzureHelper.SetBlobAttributes(config.Application, config.Url, config.UserAgent, config.ExpirationDate);
                }

                return(new HttpResponseMessage()
                {
                    Content = new StringContent(result)
                });
            }
        }