# 올리기.ps1 — 앱 소스 폴더를 zip 으로 묶어 trading.crowny.org 에 올린다 (Windows PowerShell 5.1+, PS7 호환) # 판단 로직 정본: 올리기.한선 # 종료코드: 0 티, 1 서버 거부, 2 준비물 없음, 3 크기 초과 param( [Parameter(Mandatory)][string]$Source, [string]$Name, [string]$Server = 'https://trading.crowny.org', [string]$Memo = '', [switch]$KeepZip ) $ErrorActionPreference = 'Stop' try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {} $MaxBytes = 16777216 if (-not (Test-Path -LiteralPath $Source -PathType Container)) { Write-Host "폴더가 없습니다: $Source"; exit 2 } $full = (Resolve-Path -LiteralPath $Source).Path.TrimEnd('\','/') $base = Split-Path -Leaf $full $parent = Split-Path -Parent $full $mark = (Test-Path -LiteralPath (Join-Path $full 'Package.swift')) -or (Test-Path -LiteralPath (Join-Path $full 'project.yml')) -or [bool](Get-ChildItem -LiteralPath $full -Filter '*.xcodeproj' -ErrorAction SilentlyContinue) -or [bool](Get-ChildItem -LiteralPath $full -Filter '*.xcworkspace' -ErrorAction SilentlyContinue) if (-not $mark) { Write-Warning '프로젝트 표지(Package.swift|*.xcodeproj|*.xcworkspace|project.yml)가 없습니다. 올바른 폴더인지 확인하세요.' } if (-not $Name) { $Name = ($base -replace '[^A-Za-z0-9._-]', '_') + '.zip' } if ($Name -notmatch '\.(zip|tgz|tar\.gz)$') { $Name = $Name + '.zip' } if (-not (Get-Command tar.exe -ErrorAction SilentlyContinue)) { Write-Host 'tar.exe 가 없습니다. 윈도우 10 1803 이상으로 업데이트하거나 PowerShell 7 의 Compress-Archive 를 쓰세요.' exit 2 } $tmp = Join-Path ([IO.Path]::GetTempPath()) ('handoff-' + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tmp | Out-Null $zip = Join-Path $tmp $Name $code = 1 try { do { Write-Host "[1/5] 압축: $base -> $Name" & tar.exe -a -cf $zip --exclude=.build --exclude=DerivedData --exclude=.git --exclude=xcuserdata --exclude=Pods --exclude=node_modules --exclude=.swiftpm '--exclude=*.ipa' '--exclude=*.dSYM' '--exclude=*.app' -C $parent $base if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $zip)) { Write-Host 'tar.exe 압축 실패'; $code = 2; break } $bytes = (Get-Item -LiteralPath $zip).Length Write-Host "[2/5] 크기: $bytes 바이트 (상한 $MaxBytes)" if ($bytes -gt $MaxBytes) { Write-Host '16MB 를 넘습니다. 제외 목록(.build DerivedData .git ...)을 확인하세요. 그래도 넘으면 scp(C안)로 보내세요.' $code = 3; break } $localSha = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash.ToLower() Write-Host "[3/5] 로컬 sha256: $localSha" $uid = Read-Host '아이디' $sec = Read-Host '비밀번호' -AsSecureString $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec) try { $plain = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) $loginJson = (@{ id = $uid; pw = $plain } | ConvertTo-Json -Compress) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) $plain = $null } $sess = $null try { $r = Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$Server/api/kps/login" -ContentType 'application/json; charset=utf-8' -Body ([Text.Encoding]::UTF8.GetBytes($loginJson)) -SessionVariable sess Write-Host "[4/5] 로그인 응답 코드: $($r.StatusCode)" } catch { $st = 0; try { $st = [int]$_.Exception.Response.StatusCode } catch {} Write-Host "로그인 실패 (상태 $st). 401 이면 아이디·비밀번호를 확인하세요." $loginJson = $null; $code = 1; break } $loginJson = $null $url = "$Server/api/kps/upload/bundle?name=$([uri]::EscapeDataString($Name))&sha256=$localSha&memo=$([uri]::EscapeDataString($Memo))" $status = 0; $text = '' try { $resp = Invoke-WebRequest -UseBasicParsing -Method Put -Uri $url -ContentType 'application/zip' -InFile $zip -WebSession $sess $status = [int]$resp.StatusCode; $text = $resp.Content } catch { try { $status = [int]$_.Exception.Response.StatusCode } catch {} try { $rs = New-Object IO.StreamReader($_.Exception.Response.GetResponseStream()) $text = $rs.ReadToEnd() } catch { $text = $_.Exception.Message } } Write-Host "[5/5] 업로드 응답 코드: $status" Write-Host "응답: $text" try { Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$Server/api/kps/logout" -ContentType 'application/json' -Body '{}' -WebSession $sess | Out-Null } catch {} $serverSha = '' try { $j = $text | ConvertFrom-Json; if ($j.sha256) { $serverSha = ([string]$j.sha256).ToLower() } } catch {} switch ($status) { 200 { if (-not $serverSha) { Write-Host "[옴] 200 수신, 서버 sha256 응답 없음(미비교) name=$Name bytes=$bytes"; $code = 0 } elseif ($serverSha -eq $localSha) { Write-Host "[티] 수신 확인 name=$Name bytes=$bytes sha256=$($localSha.Substring(0,12))"; $code = 0 } else { Write-Host "[오류] 서버 sha256 이 다릅니다 (서버 $($serverSha.Substring(0,12)) / 로컬 $($localSha.Substring(0,12)))"; $code = 1 } } 401 { Write-Host '[로그인] 401 로그인 필요 또는 세션 만료. 다시 실행하세요.'; $code = 1 } 413 { Write-Host '[크기] 413 16MB 초과. 제외 목록 확인, 그래도 넘으면 scp(C안).'; $code = 1 } 415 { Write-Host '[형식] 415 zip/tgz 가 아닙니다. 확장자·내용 확인.'; $code = 1 } 422 { Write-Host '[불일치] 422 sha256 불일치(저장 안 됨). 그대로 1회 재전송, 반복되면 scp(C안).'; $code = 1 } default { Write-Host "[오류] 코드 $status"; $code = 1 } } } while ($false) } finally { if ($KeepZip) { Write-Host "zip 유지: $zip" } else { Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue } } exit $code