Inheritance: Raven.Documentation.Web.Controllers.BaseController
Ejemplo n.º 1
0
        public virtual ActionResult Generate(string key)
        {
            var parser =
                new ArticleParser(
                    new ParserOptions
            {
                PathToDocumentationDirectory = GetArticleDirectory(),
                RootUrl   = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")),
                ImagesUrl = DocsController.GetImagesUrl(DocumentStore)
            }, new NoOpGitFileInformationProvider());

            DocumentStore
            .DatabaseCommands
            .DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery {
                Query = "Tag:ArticlePages"
            })
            .WaitForCompletion();

            foreach (var page in parser.Parse())
            {
                DocumentSession.Store(page);

                Parallel.ForEach(
                    page.Images,
                    image =>
                {
                    if (System.IO.File.Exists(image.ImagePath) == false)
                    {
                        return;
                    }

                    using (var file = System.IO.File.OpenRead(image.ImagePath))
                    {
                        DocumentStore.DatabaseCommands.PutAttachment(image.ImageKey, null, file, new RavenJObject());
                    }
                });
            }

            foreach (var toc in parser.GenerateTableOfContents())
            {
                DocumentSession.Store(toc);
            }

            DocumentSession.SaveChanges();

            while (true)
            {
                var stats = DocumentStore.DatabaseCommands.GetStatistics();
                if (stats.StaleIndexes.Any() == false)
                {
                    break;
                }

                Thread.Sleep(500);
            }

            if (string.IsNullOrEmpty(key))
            {
                return(RedirectToAction(MVC.Articles.ActionNames.Articles));
            }

            return(RedirectToAction(MVC.Articles.ActionNames.Articles, MVC.Articles.Name, new { key = key }));
        }
Ejemplo n.º 2
0
        public virtual ActionResult Generate(string key)
        {
            var parser =
                new ArticleParser(
                    new ParserOptions
            {
                PathToDocumentationDirectory = GetArticleDirectory(),
                RootUrl           = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")),
                ImageUrlGenerator = DocsController.GetImageUrlGenerator(HttpContext, DocumentStore, ArticleType.Articles)
            }, new NoOpGitFileInformationProvider());

            var query = DocumentSession
                        .Advanced
                        .DocumentQuery <ArticlePage>()
                        .GetIndexQuery();

            DocumentStore
            .Operations
            .Send(new DeleteByQueryOperation(query))
            .WaitForCompletion();

            DocumentSession.SaveChanges();

            var toDispose    = new List <IDisposable>();
            var parserOutput = parser.Parse();

            try
            {
                foreach (var page in parserOutput.Pages)
                {
                    DocumentSession.Store(page);

                    foreach (var image in page.Images)
                    {
                        var fileInfo = new FileInfo(image.ImagePath);
                        if (fileInfo.Exists == false)
                        {
                            continue;
                        }

                        var file = fileInfo.OpenRead();
                        toDispose.Add(file);

                        var documentId = page.Id;
                        var fileName   = Path.GetFileName(image.ImagePath);

                        DocumentSession.Advanced.Attachments.Store(documentId, fileName, file, AttachmentsController.FileExtensionToContentTypeMapping[fileInfo.Extension]);
                    }
                }

                foreach (var toc in parserOutput.TableOfContents)
                {
                    DocumentSession.Store(toc);
                }

                DocumentSession.SaveChanges();
            }
            finally
            {
                foreach (var disposable in toDispose)
                {
                    disposable?.Dispose();
                }
            }

            while (true)
            {
                var stats = DocumentStore.Maintenance.Send(new GetStatisticsOperation());
                if (stats.StaleIndexes.Any() == false)
                {
                    break;
                }

                Thread.Sleep(500);
            }

            if (string.IsNullOrEmpty(key))
            {
                return(RedirectToAction(MVC.Articles.ActionNames.Articles));
            }

            return(RedirectToAction(MVC.Articles.ActionNames.Articles, MVC.Articles.Name, new { key = key }));
        }