/// <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; } }
/// <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; } }