72 lines
5.1 KiB
PowerShell
72 lines
5.1 KiB
PowerShell
param([string]$ContentRoot = (Join-Path (Split-Path -Parent $PSScriptRoot) 'dist\manga'))
|
|
$ErrorActionPreference = 'Stop'
|
|
$utf8 = [Text.UTF8Encoding]::new($false)
|
|
$root = [IO.Path]::GetFullPath($ContentRoot)
|
|
if (-not (Test-Path -LiteralPath $root -PathType Container)) { throw ('Content folder does not exist: ' + $root) }
|
|
$librarySeries = @()
|
|
foreach ($mangaFolder in (Get-ChildItem -LiteralPath $root -Directory | Sort-Object Name)) {
|
|
if ($mangaFolder.Name -cnotmatch '^[A-Z0-9]+(?:_[A-Z0-9]+)*$') { Write-Warning ('Skipped invalid series folder: ' + $mangaFolder.Name); continue }
|
|
$metadataPath = Join-Path $mangaFolder.FullName 'series.json'
|
|
if (-not (Test-Path -LiteralPath $metadataPath)) { Write-Warning ('Missing series.json: ' + $mangaFolder.Name); continue }
|
|
$metadata = Get-Content -LiteralPath $metadataPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
if (-not $metadata.title) { throw ('Missing title: ' + $metadataPath) }
|
|
$seriesId = if ($metadata.id) { [string]$metadata.id } else { $mangaFolder.Name }
|
|
if ($seriesId -cnotmatch '^[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*$') { throw ('Invalid series id: ' + $metadataPath) }
|
|
if (@($librarySeries | Where-Object { $_.id -eq $seriesId }).Count) { throw ('Duplicate series id: ' + $seriesId) }
|
|
$chapters = @()
|
|
$chapterFolders = @(Get-ChildItem -LiteralPath $mangaFolder.FullName -Directory | Where-Object { $_.Name -match '^chapter-\d+$' } | Sort-Object { [int]($_.Name -replace '^chapter-','') })
|
|
$seenNumbers = @{}
|
|
foreach ($chapterFolder in $chapterFolders) {
|
|
$number = [int]($chapterFolder.Name -replace '^chapter-','')
|
|
if ($number -lt 1 -or $seenNumbers.ContainsKey($number)) { throw ('Invalid or duplicate chapter number: ' + $chapterFolder.FullName) }
|
|
$seenNumbers[$number] = $true
|
|
$images = @(Get-ChildItem -LiteralPath $chapterFolder.FullName -File | Where-Object { $_.Extension -match '^\.(png|jpe?g|webp|avif)$' })
|
|
$byNumber = @{}; $issues = @()
|
|
foreach ($image in $images) {
|
|
if ($image.BaseName -notmatch '^\d+$') { $issues += ('Use numeric file names: ' + $image.Name); continue }
|
|
$pageNumber = [int]$image.BaseName
|
|
if ($pageNumber -lt 1 -or $pageNumber -gt 16) { $issues += ('Page outside 01-16: ' + $image.Name); continue }
|
|
if ($byNumber.ContainsKey($pageNumber)) { $issues += ('Duplicate page: ' + $pageNumber); continue }
|
|
if ($image.Length -eq 0) { $issues += ('Empty image: ' + $image.Name); continue }
|
|
$byNumber[$pageNumber] = $image
|
|
}
|
|
$missing = @(1..16 | Where-Object { -not $byNumber.ContainsKey($_) })
|
|
$ready = $missing.Count -eq 0 -and $issues.Count -eq 0
|
|
$title = 'Глава {0:00}' -f $number
|
|
$chapterMetadata = Join-Path $chapterFolder.FullName 'chapter.json'
|
|
if (Test-Path -LiteralPath $chapterMetadata) {
|
|
$custom = Get-Content -LiteralPath $chapterMetadata -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
if ($custom.title) { $title = [string]$custom.title }
|
|
}
|
|
$pages = @()
|
|
foreach ($pageNumber in ($byNumber.Keys | Sort-Object)) {
|
|
$image = $byNumber[$pageNumber]
|
|
$src = 'manga/' + $mangaFolder.Name + '/' + $chapterFolder.Name + '/' + [Uri]::EscapeDataString($image.Name)
|
|
$pages += [ordered]@{ src = $src }
|
|
}
|
|
$chapters += [ordered]@{ id = $chapterFolder.Name; title = $title; number = $number; expectedPages = 16; ready = $ready; pages = @($pages) }
|
|
if ($images.Count -and -not $ready) { Write-Warning ($mangaFolder.Name + '/' + $chapterFolder.Name + ': not published; missing ' + ($missing -join ', ') + '; ' + ($issues -join '; ')) }
|
|
Write-Host ('{0}/{1}: {2}/16, {3}' -f $mangaFolder.Name, $chapterFolder.Name, $byNumber.Count, $(if ($ready) {'ready'} else {'waiting'}))
|
|
}
|
|
$librarySeries += [ordered]@{ id = $seriesId; folder = $mangaFolder.Name; title = [string]$metadata.title; chapters = @($chapters) }
|
|
}
|
|
$manifest = [ordered]@{ version = 1; generatedAt = [DateTime]::UtcNow.ToString('o'); series = @($librarySeries) }
|
|
$manifestPath = Join-Path $root 'library.json'
|
|
if ([IO.File]::Exists($manifestPath)) {
|
|
try {
|
|
$existing = Get-Content -LiteralPath $manifestPath -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
$before = ConvertTo-Json -InputObject @($existing.series) -Depth 15 -Compress
|
|
$after = ConvertTo-Json -InputObject @($librarySeries) -Depth 15 -Compress
|
|
if ($existing.version -eq $manifest.version -and $before -ceq $after) {
|
|
Write-Host ('Library is current: ' + $manifestPath)
|
|
return
|
|
}
|
|
} catch { # A missing or invalid previous manifest is rebuilt below.
|
|
}
|
|
}
|
|
$tempPath = Join-Path $root ('library.' + [Guid]::NewGuid().ToString('N') + '.tmp')
|
|
try {
|
|
[IO.File]::WriteAllText($tempPath, ($manifest | ConvertTo-Json -Depth 15), $utf8)
|
|
if ([IO.File]::Exists($manifestPath)) { [IO.File]::Replace($tempPath, $manifestPath, [NullString]::Value) } else { [IO.File]::Move($tempPath, $manifestPath) }
|
|
} finally { if ([IO.File]::Exists($tempPath)) { [IO.File]::Delete($tempPath) } }
|
|
Write-Host ('Library updated: ' + $manifestPath) |