<commit_before>﻿namespace SourceBrowser.Site.Controllers
{
    using System.IO;
    using System.Web.Mvc;

    using SourceBrowser.SolutionRetriever;
    using SourceBrowser.Generator.Transformers;

    public class UploadController : Controller
    {
        // GET: Upload
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Submit(string githubUrl)
        {
            // If someone navigates to submit directly, just send 'em back to index
            if (string.IsNullOrWhiteSpace(githubUrl))
            {
                return View("Index");
            }

            var retriever = new GitHubRetriever(githubUrl);
            if (!retriever.IsValidUrl())
            {
                // TODO: Return error
                ViewBag.Error = "Invalid GitHub repository. Please try another";
                return View("Index");
            }

            string filePath = retriever.RetrieveProject();
            var organizationPath = System.Web.Hosting.HostingEnvironment.MapPath("~/")  + "SB_Files\\" + retriever.UserName;
            var repoPath = Path.Combine(organizationPath, retriever.RepoName);

            // Generate the source browser files for this solution
            var solutionPaths = GetSolutionPaths(filePath);
            if (solutionPaths.Length == 0)
            {
                ViewBag.Error = "No C# solution was found. Ensure that a valid .sln file exists within your repository.";
                return View("Index");
            }

            // TODO: Use parallel for.
            foreach (var path in solutionPaths)
            {
                var filenamePosition = path.LastIndexOf('\\');
                var solutionName = path.Substring(filenamePosition);
                var solutionPath = repoPath + solutionName; // don't use Path.Combine because solutionName contains "\"
                var sourceGenerator = new Generator.SolutionAnalayzer(path);
                //Build the workspace
                var workspaceModel = sourceGenerator.BuildWorkspaceModel(solutionPath);

                //One pass to lookup all declarations
                var typeTransformer = new TokenLookupTransformer();
                typeTransformer.Visit(workspaceModel);
                var tokenLookup = typeTransformer.TokenLookup;

                //Another pass to generate HTMLs
                var htmlTransformer = new HtmlTransformer(tokenLookup, repoPath);
                htmlTransformer.Visit(workspaceModel);
            }

            return Redirect("/Browse/" + retriever.UserName + "/" + retriever.RepoName);
        }

        /// <summary><commit_msg>Generates tree view on repo upload<commit_after>﻿namespace SourceBrowser.Site.Controllers
{
    using System.IO;
    using System.Web.Mvc;

    using SourceBrowser.SolutionRetriever;
    using SourceBrowser.Generator.Transformers;

    public class UploadController : Controller
    {
        // GET: Upload
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Submit(string githubUrl)
        {
            // If someone navigates to submit directly, just send 'em back to index
            if (string.IsNullOrWhiteSpace(githubUrl))
            {
                return View("Index");
            }

            var retriever = new GitHubRetriever(githubUrl);
            if (!retriever.IsValidUrl())
            {
                // TODO: Return error
                ViewBag.Error = "Invalid GitHub repository. Please try another";
                return View("Index");
            }

            string filePath = retriever.RetrieveProject();
            var organizationPath = System.Web.Hosting.HostingEnvironment.MapPath("~/")  + "SB_Files\\" + retriever.UserName;
            var repoPath = Path.Combine(organizationPath, retriever.RepoName);

            // Generate the source browser files for this solution
            var solutionPaths = GetSolutionPaths(filePath);
            if (solutionPaths.Length == 0)
            {
                ViewBag.Error = "No C# solution was found. Ensure that a valid .sln file exists within your repository.";
                return View("Index");
            }

            // TODO: Use parallel for.
            foreach (var path in solutionPaths)
            {
                var filenamePosition = path.LastIndexOf('\\');
                var solutionName = path.Substring(filenamePosition);
                var solutionPath = repoPath + solutionName; // don't use Path.Combine because solutionName contains "\"
                var sourceGenerator = new Generator.SolutionAnalayzer(path);
                //Build the workspace
                var workspaceModel = sourceGenerator.BuildWorkspaceModel(solutionPath);

                //One pass to lookup all declarations
                var typeTransformer = new TokenLookupTransformer();
                typeTransformer.Visit(workspaceModel);
                var tokenLookup = typeTransformer.TokenLookup;

                //Another pass to generate HTMLs
                var htmlTransformer = new HtmlTransformer(tokenLookup, repoPath);
                htmlTransformer.Visit(workspaceModel);

                // Generate HTML of the tree view
                var treeViewTransformer = new TreeViewTransformer(repoPath, retriever.UserName, retriever.RepoName);
                treeViewTransformer.Visit(workspaceModel);
            }

            return Redirect("/Browse/" + retriever.UserName + "/" + retriever.RepoName);
        }

        /// <summary>
{'fraction_non_alphanumeric': 0.08163265306122448, 'fraction_numerical': 0.00035803795202291446, 'mean_word_length': 1.8374809547993907, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3867558', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1291, 'n_words': 353}