Ejemplo n.º 1
0
        /// <summary>
        /// Delete a StaticSiteItem from disk, and publish the changes
        /// </summary>
        /// <param name="item">a StaticSiteItem</param>
        private void DoDeleteItem(StaticSiteItem item)
        {
            var backupFileName = Path.GetTempFileName();

            File.Copy(item.FilePathById, backupFileName, true);

            try
            {
                File.Delete(item.FilePathById);

                // Build the site, if required
                if (Config.BuildCommand != string.Empty)
                {
                    DoSiteBuild();
                }

                // Publish the site
                DoSitePublish();
            }
            catch (Exception ex)
            {
                File.Copy(backupFileName, item.FilePathById, overwrite: true);
                File.Delete(backupFileName);

                // Throw the exception up
                throw ex;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generic method to prepare and publish a new StaticSiteItem derived instance
        /// </summary>
        /// <param name="item">a new StaticSiteItem derived instance</param>
        /// <returns>the new StaticSitePost ID</returns>
        private string DoNewItem(StaticSiteItem item)
        {
            // Ensure the post has an ID
            var newPostId = item.EnsureId();

            // Ensure the post has a date
            item.EnsureDatePublished();
            // Ensure the post has a safe slug
            item.EnsureSafeSlug();
            // Save the post to disk under it's new slug-based path
            item.SaveToFile(item.FilePathBySlug);

            try
            {
                // Build the site, if required
                if (Config.BuildCommand != string.Empty)
                {
                    DoSiteBuild();
                }

                // Publish the site
                DoSitePublish();

                return(newPostId);
            }
            catch (Exception ex)
            {
                // Clean up our output file
                File.Delete(item.FilePathBySlug);
                // Throw the exception up
                throw ex;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generic method to edit an already-published StaticSiteItem derived instance
        /// </summary>
        /// <param name="item">an existing StaticSiteItem derived instance</param>
        /// <returns>True if successful</returns>
        private bool DoEditItem(StaticSiteItem item)
        {
            // Copy the existing post to a temporary file
            var backupFileName = Path.GetTempFileName();

            File.Copy(item.FilePathById, backupFileName, true);

            bool renameOccurred = false;
            // Store the old file path and slug
            string oldPath = item.FilePathById;

            //string oldSlug = item.DiskSlugFromFilePathById;

            try
            {
                // Determine if the post file needs renaming (slug, date or parent change)
                if (item.FilePathById != item.FilePathBySlug)
                {
                    renameOccurred = true;

                    // Find a new safe slug for the post
                    item.Slug = item.FindNewSlug(item.Slug, safe: true);
                    // Remove the old file
                    File.Delete(oldPath);
                    // Save to the new file
                    item.SaveToFile(item.FilePathBySlug);
                }
                else
                {
                    // Save the post to disk based on it's existing id
                    item.SaveToFile(item.FilePathById);
                }

                // Build the site, if required
                if (Config.BuildCommand != string.Empty)
                {
                    DoSiteBuild();
                }

                // Publish the site
                DoSitePublish();

                return(true);
            }
            catch (Exception ex)
            {
                // Clean up the failed output
                if (renameOccurred)
                {
                    // Delete the rename target
                    File.Delete(item.FilePathBySlug);
                }
                else
                {
                    // Delete the original file
                    File.Delete(item.FilePathById);
                }

                // Copy the backup to the old location
                File.Copy(backupFileName, oldPath, overwrite: true);

                // Delete the backup
                File.Delete(backupFileName);

                // Throw the exception up
                throw ex;
            }
        }