Private/Compare-GhostPostFile.ps1
|
function Compare-GhostPostFile { <# .SYNOPSIS Says what an export has to do with the file of a post or a page: write it, save its images or neither. .DESCRIPTION Export-GhostPost and Export-GhostPage use this for every post or page, before they change anything. The file of a post can be written in two forms, see ConvertTo-GhostPostFile: with every image as its address, or with an uploaded image as the path of a file in the folder images next to the post file. A file in either form is the post. So is a file in between, with a path for the images that have a file and an address for the others, which is what an export leaves behind when an image could not be saved. A post that came from a file has the addresses that Sync-GhostPost uploaded the image files of that file to. Those addresses are written as the paths the file has, so that such a file is the post too. The answer has the action for the file as it is now: - Created: there is no file. It is to be written. - Unchanged: the file is the post, in one of the forms. With -IncludeImage the images that have no file yet are to be saved, and the file is to be written again when it has an address where a path belongs. - Updated: the file differs and -Force says to write it. - Skipped: the file differs and is left alone. Save-GhostPostFile does what the answer says. .EXAMPLE Compare-GhostPostFile -Post $post -Path $file -Fields $fields -IncludeImage Returns the action, the text to write and the images to save for that post and that file. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # The post or the page, read with the formats lexical and html and with its tags. [Parameter(Mandatory)] [object] $Post, # Full path of the file of the post. [Parameter(Mandatory)] [string] $Path, # The fields the front matter may set, as Read-GhostPostFile takes them, to read the file that is there. [Parameter(Mandatory)] [System.Collections.IDictionary] $Fields, # Keep the uploaded images as files next to the post file. [switch] $IncludeImage, # Write a file that differs. [switch] $Force ) $directory = [System.IO.Path]::GetDirectoryName($Path) $existing = $null $known = [System.Collections.Generic.Dictionary[string, string]]::new([System.StringComparer]::Ordinal) if ([System.IO.File]::Exists($Path)) { $existing = [System.IO.File]::ReadAllText($Path, [System.Text.Encoding]::UTF8) -replace '\r\n?', "`n" # The image files of this file that the post on the site already has, under the name they were # uploaded with. try { $source = Read-GhostPostFile -Path $Path -Fields $Fields foreach ($image in Get-GhostPostImage -Source $source -Post $Post) { if ($image.Url -and -not $known.ContainsKey($image.Url)) { $known[$image.Url] = $image.Destination } } } catch { Write-Debug -Message "The images of $Path are not compared: $($_.Exception.Message)" } } $plain = ConvertTo-GhostPostFile -Post $Post $full = ConvertTo-GhostPostFile -Post $Post -IncludeImage -KnownImage $known $missing = @( foreach ($image in $full.Images) { $file = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($directory, $image.Path)) if (-not [System.IO.File]::Exists($file)) { [pscustomobject]@{ Url = $image.Url; Path = $file } } } ) # The form in between: a path for the images that have a file, an address for the others. $partial = $full if ($missing.Count -gt 0) { $addresses = @($missing | ForEach-Object { $_.Url }) $partial = ConvertTo-GhostPostFile -Post $Post -IncludeImage -KnownImage $known -ExcludeImage $addresses } $text = $plain.Text $images = @() if ($IncludeImage) { $text = $full.Text $images = $missing } $write = $false if ($null -eq $existing) { $action = 'Created' $write = $true } elseif (@($plain.Text, $full.Text, $partial.Text) -ccontains $existing) { # The post, but with -IncludeImage perhaps not with a path for every image yet. $action = 'Unchanged' $write = $IncludeImage -and $existing -cne $full.Text } elseif ($Force) { $action = 'Updated' $write = $true } else { $action = 'Skipped' $images = @() } [pscustomobject]@{ Action = $action Write = $write Path = $Path Existing = $existing Text = $text Content = $plain.Content Images = @($images) Known = $known } } |