From 8f098815d5a254e2540f0ee610301b3d62ef13fe Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Sun, 21 Jul 2019 01:31:22 +1000 Subject: [PATCH] ssg: StaticSiteClient: DoEditItem method impl for generic item editing --- .../Clients/StaticSite/StaticSiteClient.cs | 137 +++++++++--------- 1 file changed, 70 insertions(+), 67 deletions(-) diff --git a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSiteClient.cs b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSiteClient.cs index 46a2b76c..76995081 100644 --- a/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSiteClient.cs +++ b/src/managed/OpenLiveWriter.BlogClient/Clients/StaticSite/StaticSiteClient.cs @@ -118,74 +118,8 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite // Set slug to existing slug on post ssgPost.Slug = post.Slug; - // Copy the existing post to a temporary file - var backupFileName = Path.GetTempFileName(); - File.Copy(ssgPost.FilePathById, backupFileName, true); - bool renameOccurred = false; - // Store the old file path and slug - string oldPath = ssgPost.FilePathById; - string oldSlug = ssgPost.DiskSlugFromFilePathById; - - try - { - // Determine if the post file needs renaming (slug change) - if (ssgPost.FilePathById != ssgPost.FilePathBySlug) - { - // Set the new safe slug - ssgPost.Slug = ssgPost.FindNewSlug(ssgPost.Slug, safe: true); - // If the new slug is equal to the old, save to the existing file (most likely a date change) - if (oldSlug == ssgPost.Slug) - { - // Save the post to disk based on it's existing id - ssgPost.SaveToFile(ssgPost.FilePathById); - } - else - { - renameOccurred = true; - - // Remove the old file - File.Delete(oldPath); - // Save to the new file - ssgPost.SaveToFile(ssgPost.FilePathBySlug); - } - } - else - { - // Save the post to disk based on it's existing id - ssgPost.SaveToFile(ssgPost.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(ssgPost.FilePathBySlug); - } else - { - // Delete the original file - File.Delete(ssgPost.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; - } + return DoEditItem(ssgPost); } /// @@ -338,6 +272,75 @@ namespace OpenLiveWriter.BlogClient.Clients.StaticSite } + /// + /// Generic method to edit an already-published StaticSiteItem derived instance + /// + /// an existing StaticSiteItem derived instance + /// True if successful + 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; + } + } + /// /// Build the static site ///