param([string]$ModelDirectory = (Join-Path $PSScriptRoot '../nlp_model/v0.24.0')) $ErrorActionPreference = 'Stop' $version = 'v0.24.0' $archiveHash = '33188BA932BBA4717BAD5244BBEC0EF8B1C9CBB47E26E68394A7976D8D779083' $target = [System.IO.Path]::GetFullPath($ModelDirectory) if ((Test-Path -LiteralPath $target) -and (Get-ChildItem -LiteralPath $target -Force | Select-Object -First 1)) { throw '모델 대상 폴더가 비어 있지 않습니다. 기존 모델을 덮어쓰지 않습니다.' } $temporaryRoot = Join-Path ([System.IO.Path]::GetTempPath()) ('kart-kiwi-' + [guid]::NewGuid()) New-Item -ItemType Directory -Path $temporaryRoot | Out-Null $archive = Join-Path $temporaryRoot 'kiwi-model.tgz' try { Invoke-WebRequest -Uri "https://github.com/bab2min/Kiwi/releases/download/$version/kiwi_model_${version}_base.tgz" -OutFile $archive if ((Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash -ne $archiveHash) { throw 'Kiwi 모델 파일의 SHA-256이 일치하지 않습니다.' } tar -xzf $archive -C $temporaryRoot if ($LASTEXITCODE -ne 0) { throw 'Kiwi 모델 압축 해제에 실패했습니다.' } $source = Join-Path $temporaryRoot 'models/cong/base' if (!(Test-Path -LiteralPath (Join-Path $source 'cong.mdl'))) { throw 'Kiwi 모델 파일이 없습니다.' } New-Item -ItemType Directory -Path $target -Force | Out-Null Get-ChildItem -LiteralPath $source -File | ForEach-Object { Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $target $_.Name) } Write-Output 'Kiwi 0.24.0 모델을 설치했습니다.' } finally { $resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path $expectedParent = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd('\') + '\' $validTemporaryParent = $resolvedTemporaryRoot.StartsWith($expectedParent, [System.StringComparison]::OrdinalIgnoreCase) $validTemporaryName = [System.IO.Path]::GetFileName($resolvedTemporaryRoot) -match '^kart-kiwi-[0-9a-f-]{36}$' if (!$validTemporaryParent -or !$validTemporaryName) { throw '임시 폴더 경로 검증 실패: 삭제하지 않습니다.' } Remove-Item -LiteralPath $resolvedTemporaryRoot -Recurse -Force }