Beispiel #1
0
        public ActionResult Blob(string id, string name, string path)
        {
            ViewBag.ID = id;
            if (!String.IsNullOrEmpty(id))
            {
                using (var browser = new RepositoryBrowser(Path.Combine(UserConfiguration.Current.Repositories, id)))
                {
                    string referenceName;
                    var    model = browser.BrowseBlob(name, path, out referenceName);
                    PopulateBranchesData(browser, referenceName);
                    PopulateAddressBarData(name, path);

                    model.Text   = FileDisplayHandler.GetText(model.Data);
                    model.IsText = model.Text != null;
                    if (model.IsText)
                    {
                        model.TextBrush = FileDisplayHandler.GetBrush(path);
                    }
                    else
                    {
                        model.IsImage = FileDisplayHandler.IsImage(path);
                    }

                    return(View(model));
                }
            }
            return(View());
        }
Beispiel #2
0
        public ActionResult Raw(string id, string encodedName, string encodedPath, bool display = false)
        {
            ViewBag.ID = id;
            if (String.IsNullOrEmpty(id))
            {
                return(HttpNotFound());
            }

            using (var browser = new RepositoryBrowser(Path.Combine(UserConfiguration.Current.Repositories, id)))
            {
                var    name = PathEncoder.Decode(encodedName);
                var    path = PathEncoder.Decode(encodedPath);
                string referenceName;
                var    model = browser.BrowseBlob(name, path, out referenceName);

                if (!display)
                {
                    return(File(model.Data, "application/octet-stream", model.Name));
                }
                if (model.IsText)
                {
                    return(Content(model.Text, "text/plain", model.Encoding));
                }
                if (model.IsImage)
                {
                    return(File(model.Data, FileDisplayHandler.GetMimeType(model.Name), model.Name));
                }
            }

            return(HttpNotFound());
        }
 public ActionResult Show(string repository, string tree, string path)
 {
     using (var browser = new RepositoryBrowser(Path.Combine(UserConfigurationManager.Repositories, repository)))
     {
         var leaf = browser.GetLeaf(tree, path);
         if (leaf != null)
         {
             return(new FileStreamResult(new MemoryStream(leaf.RawData), FileDisplayHandler.GetMimeType(Path.GetFileName(path))));
         }
     }
     return(null);
 }
        private string FillFileData(Leaf leaf)
        {
            var result = FileDisplayHandler.GetText(leaf.RawData);

            if (result != null)
            {
                return(result);
            }
            if (FileDisplayHandler.GetBrush(leaf.Name) != "plain")
            {
                return(ASCIIEncoding.ASCII.GetString(leaf.RawData));
            }
            return(null);
        }
Beispiel #5
0
        private ActionResult DisplayFiles(IEnumerable <RepositoryTreeDetailModel> files, string path, string id, bool returnAsBinary)
        {
            if (files != null)
            {
                var model = new RepositoryTreeModel();
                model.Name   = id;
                model.IsTree = !(files.Count() == 1 && !files.First().IsTree&& files.First().Path == path);
                if (model.IsTree)
                {
                    model.Files = files.OrderByDescending(i => i.IsTree).ThenBy(i => i.Name);
                }
                else
                {
                    model.File = files.First();
                    if (!returnAsBinary)
                    {
                        model.Text       = FileDisplayHandler.GetText(model.File.Data);
                        model.IsTextFile = model.Text != null;

                        if (model.IsTextFile)
                        {
                            model.TextBrush = FileDisplayHandler.GetBrush(model.File.Name);
                        }

                        if (!model.IsTextFile)
                        {
                            model.IsImage = FileDisplayHandler.IsImage(model.File.Name);
                        }
                    }

                    if (!model.IsImage && !model.IsTextFile)
                    {
                        using (var stream = new MemoryStream(model.File.Data))
                        {
                            return(File(stream, "application/octet-stream", model.File.Name));
                        }
                    }
                }

                return(View(model));
            }

            return(View());
        }
        public FileContentResult Binary(string project, string path = "", string file = "", bool returnAsBinary = false)
        {
            bool isAuthenticated = HttpContext.User.Identity.IsAuthenticated;

            if (isAuthenticated)
            {
                isAuthenticated = AuthService.CanRead(project, HttpContext.User.Identity.Name);
            }
            if (!AuthService.IsRepositoryPublic(project) && !isAuthenticated)
            {
                return(new FileContentResult(new byte[] { }, "binary/octet-stream"));
            }
            var model = new LevelViewModel();

            //Opening an existing git repository
            Lib.Repository gitasprepo = repositories.GetRepository(project);
            model.Project = project;

            var directory = gitasprepo.GitDirectory();

            var repo = new GitSharp.Repository(directory);

            if (repo == null || repo.Head == null || repo.Head.CurrentCommit == null)
            {
                return(new FileContentResult(new byte[] { }, "binary/octet-stream"));
            }


            var tree = repo.Head.CurrentCommit.Tree;

            model.SetBreadCrumb(path.Split(Path.DirectorySeparatorChar).ToList());

            if (model.BreadCrumb.Count > 0)
            {
                foreach (var breadCrumb in model.BreadCrumb)
                {
                    var subTree = tree.Trees.Where(a => a.Name == Path.GetFileName(breadCrumb)).FirstOrDefault();
                    if (subTree != null)
                    {
                        tree = subTree;
                    }
                }
            }

            if (string.IsNullOrEmpty(file))
            {
                if (tree == null)
                {
                    throw new Exception("Path not found");
                }
                //Now you can browse throught that tree by iterating over its child trees
                foreach (Tree subtree in tree.Trees)
                {
                    model.Directories.Add(new LevelItemViewModel(model)
                    {
                        Name = subtree.Path
                    });
                }

                //Or printing the names of the files it contains
                foreach (Leaf leaf in tree.Leaves)
                {
                    model.Files.Add(new LevelItemViewModel(model)
                    {
                        Name = leaf.Path
                    });
                }
            }
            else
            {
                //Or printing the names of the files it contains
                foreach (Leaf leaf in tree.Leaves)
                {
                    if (file == Path.GetFileName(leaf.Path))
                    {
                        var fcr = new FileContentResult(leaf.RawData, FileDisplayHandler.GetMimeType(file));
                        fcr.FileDownloadName = leaf.Name;
                        return(fcr);
                    }
                }
            }

            return(new FileContentResult(new byte[] { }, "binary/octet-stream"));
        }
        public ActionResult Index(string project, string path = "", string file = "", bool returnAsBinary = false)
        {
            bool isAuthenticated = HttpContext.User.Identity.IsAuthenticated;

            if (isAuthenticated)
            {
                isAuthenticated = AuthService.CanRead(project, HttpContext.User.Identity.Name);
            }
            if (!AuthService.IsRepositoryPublic(project) && !isAuthenticated)
            {
                return(Redirect("../"));
            }

            var model = new LevelViewModel();

            //Opening an existing git repository
            Lib.Repository gitasprepo = repositories.GetRepository(project);
            model.Project = project;

            var directory = gitasprepo.GitDirectory();

            var repo = new GitSharp.Repository(directory);

            if (repo == null || repo.Head == null || repo.Head.CurrentCommit == null)
            {
                return(View(model));
            }


            var tree = repo.Head.CurrentCommit.Tree;

            model.SetBreadCrumb(path.Split(Path.DirectorySeparatorChar).ToList());

            if (model.BreadCrumb.Count > 0)
            {
                foreach (var breadCrumb in model.BreadCrumb)
                {
                    var subTree = tree.Trees.Where(a => a.Name == Path.GetFileName(breadCrumb)).FirstOrDefault();
                    if (subTree != null)
                    {
                        tree = subTree;
                    }
                }
            }

            if (string.IsNullOrEmpty(file))
            {
                if (tree == null)
                {
                    throw new Exception("Path not found");
                }
                //Now you can browse throught that tree by iterating over its child trees
                foreach (Tree subtree in tree.Trees)
                {
                    model.Directories.Add(new LevelItemViewModel(model)
                    {
                        Name = subtree.Path
                    });
                }

                //Or printing the names of the files it contains
                foreach (Leaf leaf in tree.Leaves)
                {
                    model.Files.Add(new LevelItemViewModel(model)
                    {
                        Name = leaf.Path
                    });
                }
            }
            else
            {
                model.Ext = Path.GetExtension(file);
                if (!string.IsNullOrEmpty(model.Ext))
                {
                    model.Ext = model.Ext.Substring(1);
                }
                //Or printing the names of the files it contains
                foreach (Leaf leaf in tree.Leaves)
                {
                    if (file == Path.GetFileName(leaf.Path))
                    {
                        model.FileName = string.Format("Binary?project={0}&path={1}&file={2}",
                                                       (project),
                                                       (path),
                                                       (file));

                        if (FileDisplayHandler.IsImage(file))
                        {
                            model.ImageFile = string.Format("Binary?project={0}&path={1}&file={2}",
                                                            (project),
                                                            (path),
                                                            (file));
                            model.FileName = model.ImageFile;
                        }
                        else
                        {
                            model.TextFile = FillFileData(leaf);

                            if (model.TextFile == null)
                            {
                                model.BinaryFile = model.FileName;
                            }
                        }
                    }
                }
            }

            return(View(model));
        }