newmindjet/restore-mmap.ps1

25 lines
1.4 KiB
PowerShell

# Restore nomad_27 copy.mmap from ./chunks or Release mmap-current
$ErrorActionPreference = "Stop"
$outFile = Join-Path $PSScriptRoot "nomad_27 copy.mmap"
$chunkDir = Join-Path $PSScriptRoot "chunks"
function Join-Parts($parts, $outFile) {
if (Test-Path $outFile) { Remove-Item $outFile -Force }
$fs = [IO.File]::Create($outFile)
try { foreach ($p in $parts) { $b=[IO.File]::ReadAllBytes($p.FullName); $fs.Write($b,0,$b.Length) } }
finally { $fs.Close() }
Write-Host "Wrote $outFile ($((Get-Item $outFile).Length) bytes)"
}
if (Test-Path $chunkDir) {
$parts = Get-ChildItem $chunkDir -File -Filter "part.*" | Sort-Object Name
if ($parts.Count -gt 0) { Write-Host "Joining $($parts.Count) local chunks ..."; Join-Parts $parts $outFile; exit 0 }
}
Write-Host "No local chunks; downloading Release mmap-current ..."
$api = "https://git.nomadro.cn/api/v1/repos/hackrobot/newmindjet/releases/tags/mmap-current"
$rel = Invoke-RestMethod -Uri $api
$tmp = Join-Path $env:TEMP ("mmap-restore-" + [guid]::NewGuid().ToString("n"))
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
$n=0; foreach ($a in ($rel.assets | Sort-Object name)) { $n++; Write-Host "[$n/$($rel.assets.Count)] $($a.name)"; Invoke-WebRequest -Uri $a.browser_download_url -OutFile (Join-Path $tmp $a.name) -UseBasicParsing }
Join-Parts (Get-ChildItem $tmp -File | Sort-Object Name) $outFile
} finally { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue }