Example #1
0
 public override void CopyFile(String source, string physicalSource, string physicalDest)
 {
     if (publishedFileManager.IsDraftedFile(source, physicalSource))
     {
         GitDraftInfo publishInfo = publishedFileManager.LoadDraftInfo(physicalSource);
         if (publishInfo.Sha != null) //If we have publish info and it specifies an earlier published version, load that version
         {
             using (var destStream = File.Open(physicalDest, FileMode.Create, FileAccess.Write, FileShare.None))
             {
                 using (var srcStream = OpenRepoStream(physicalSource, publishInfo))
                 {
                     srcStream.CopyTo(destStream);
                 }
             }
         }
         else
         {
             throw new FileNotFoundException($"Cannot find draft version of {source}.");
         }
     }
     else
     {
         base.CopyFile(source, physicalSource, physicalDest);
     }
 }
Example #2
0
        public GitDraftInfo LoadDraftInfo(string file)
        {
            GitDraftInfo draftInfo     = null;
            var          draftInfoFile = GetDraftInfoFileName(file);

            //See if we can read the file
            try
            {
                if (File.Exists(draftInfoFile))
                {
                    //Always read file directly, this gets called during the ReadFile function call.
                    using (var reader = new JsonTextReader(new StreamReader(File.Open(draftInfoFile, FileMode.Open, FileAccess.Read, FileShare.Read))))
                    {
                        draftInfo = serializer.Deserialize <GitDraftInfo>(reader);
                    }
                }
            }
            catch (Exception) { }

            if (draftInfo == null)
            {
                draftInfo = new GitDraftInfo();
            }

            return(draftInfo);
        }
Example #3
0
        public override Stream OpenReadStream(String originalFile, String physicalFile)
        {
            if (publishedFileManager.IsDraftedFile(originalFile, physicalFile))
            {
                GitDraftInfo publishInfo = publishedFileManager.LoadDraftInfo(physicalFile);
                if (publishInfo.Sha != null) //If we have publish info and it specifies an earlier published version, load that version
                {
                    return(OpenRepoStream(physicalFile, publishInfo));
                }

                throw new FileNotFoundException($"Cannot find draft version of {originalFile}.");
            }
            else
            {
                return(base.OpenReadStream(originalFile, physicalFile));
            }
        }
Example #4
0
        public bool SendPageToDraft(String file, String physicalFile, IFileFinder fileFinder)
        {
            if (draftIdentfier.AllowFile(file, physicalFile) && File.Exists(physicalFile))
            {
                using (var repo = new Repository(Repository.Discover(physicalFile)))
                {
                    var latestCommit = repo.Commits.FirstOrDefault();
                    if (latestCommit != null)
                    {
                        GitDraftInfo publishInfo = LoadDraftInfo(physicalFile);

                        publishInfo.Sha = latestCommit.Sha;

                        using (var writer = new JsonTextWriter(new StreamWriter(fileFinder.WriteFile(GetDraftInfoFileName(file)))))
                        {
                            serializer.Serialize(writer, publishInfo);
                        }
                    }
                }
                return(true);
            }
            return(false);
        }
Example #5
0
        public DraftInfo GetDraftStatus(String file, string physicalFile, IFileFinder fileFinder)
        {
            file = file.TrimStartingPathChars();

            GitDraftInfo gitDraftInfo = LoadDraftInfo(physicalFile);

            //If the file has a sha,
            if (gitDraftInfo.Sha != null)
            {
                var repoPath = Path.GetFullPath(Repository.Discover(physicalFile) + "/..");
                using (var repo = new Repository(repoPath))
                {
                    //This is close, lookup the draft commit for the settings, css and js files also
                    var draftCommit = repo.Lookup <Commit>(gitDraftInfo.Sha);
                    if (draftCommit != null)
                    {
                        var compare      = repo.Diff.Compare <TreeChanges>(draftCommit.Tree, repo.Head.Tip.Tree);
                        var contentFiles = new List <String>(5);
                        contentFiles.Add(file.Replace('/', '\\'));
                        contentFiles.AddRange(fileFinder.GetPageContentFiles(file).Select(i => i.Replace('/', '\\')));

                        foreach (var compareFile in compare.Added.Concat(compare.Modified).Select(i => i.Path.Replace('/', '\\')))
                        {
                            if (contentFiles.Contains(compareFile))
                            {
                                return(new DraftInfo(draftCommit.Author.When.LocalDateTime, DraftStatus.UndraftedEdits, file));
                            }
                        }

                        //If we got here the draft is up to date
                        return(new DraftInfo(draftCommit.Author.When.LocalDateTime, DraftStatus.UpToDate, file));
                    }
                }
            }

            return(null);
        }
Example #6
0
        private static Stream OpenRepoStream(string physicalFile, GitDraftInfo publishInfo)
        {
            var discoverRepoPath = Repository.Discover(physicalFile);

            if (discoverRepoPath == null)
            {
                throw new FileNotFoundException($"Cannot find parent repository for file {physicalFile}.");
            }
            var repoPath       = Path.GetFullPath(discoverRepoPath + "../");
            var fileInRepoPath = physicalFile.Substring(repoPath.Length).Replace('\\', '/').TrimStartingPathChars();

            using (var repo = new Repository(repoPath))
            {
                var commit    = repo.Lookup <Commit>(publishInfo.Sha);
                var treeEntry = commit[fileInRepoPath];
                if (treeEntry == null)
                {
                    throw new FileNotFoundException($"Cannot find file {fileInRepoPath} in commit {publishInfo.Sha}.");
                }
                var blob = treeEntry.Target as Blob;

                return(blob.GetContentStream());
            }
        }