コード例 #1
0
ファイル: ContentController.cs プロジェクト: sidiandi/gitter
        // GET: /<controller>/
        public async Task <IActionResult> Index(
            [FromServices] IContentProvider contentProvider,
            [FromServices] IMarkdownRenderer renderer,
            [FromServices] IHistory history,
            [FromServices] IGit git,
            [FromQuery] string log,
            [FromQuery] string q,
            string path)
        {
            var contentPath = ContentPath.FromUrlPath(path);

            if (this.HttpContext.WebSockets.IsWebSocketRequest)
            {
                await NotifyContentChanged(this.HttpContext.WebSockets, contentProvider, contentPath);

                return(Ok());
            }

            if (log != null)
            {
                var r = await git.Run(new[] { "log" }.Concat(Utils.SplitArguments(log)).Concat(new[] { "--", git.GetPath(contentPath) }));

                return(await MarkdownView(renderer, contentPath, AsCode(r.Output)));
            }

            if (q != null)
            {
                var pretty  = $"--pretty=format:* [%ar: %s]({git.GetPath(contentPath)}?log=--stat+-p+-1+-U+%H), by %an";
                var grepLog = await git.Run(new[] { "log", pretty, "-100", "-S", q, git.GetPath(contentPath) });

                var grepText = git.GrepOutputToMarkdown((await git.Run(new[] { "grep", "-I", "-n", q })).Output);
                return(await MarkdownView(renderer, contentPath, grepLog.Output + hr + grepText));
            }

            var children = contentProvider.GetChildren(contentPath);

            if (children.Any())
            {
                if (!this.HttpContext.Request.Path.Value.EndsWith("/"))
                {
                    var url        = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetEncodedUrl(this.HttpContext.Request);
                    var redirectTo = url + "/";
                    return(this.Redirect(redirectTo));
                }
                return(await MarkdownView(renderer, contentPath, GetDirectoryMarkdown(contentProvider, history, contentPath, children)));
            }

            if (contentPath.IsExtension(new[] { ".md" }))
            {
                return(await MarkdownView(renderer, contentPath, GetMarkdown(contentProvider, history, contentPath)));
            }

            var mdFile = contentPath.CatName(".md");

            if (mdFile.HasValue && contentProvider.Exists(mdFile.Value))
            {
                return(await MarkdownView(renderer, contentPath, GetText(contentProvider, mdFile.Value)));
            }

            if (IsText(contentProvider, contentPath))
            {
                return(await MarkdownView(renderer, contentPath, GetSourceAsMarkdown(contentProvider, history, contentPath)));
            }

            contentProvider.Pull();

            // raw file
            return(await Raw(contentProvider, path));
        }