refactor: doDownload에서 downloadBody 분리

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-13 02:47:47 +09:00
parent 4cd118cef0
commit 208b2d3189

View File

@@ -126,22 +126,10 @@ func formatProgress(pct int, speedBytesPerSec float64, remaining float64) string
return fmt.Sprintf("다운로드 중... %d%% (%.1f MB/s, %d분 남음)", pct, speedMB, int(remaining/60)) return fmt.Sprintf("다운로드 중... %d%% (%.1f MB/s, %d분 남음)", pct, speedMB, int(remaining/60))
} }
// doDownload 파일을 다운로드하고 zip을 추출하여 destDir에 배치한다. // downloadBody 응답 본문을 tmpFile에 쓰고 진행률을 갱신한다.
func doDownload(downloadURL, destDir string) error { // downloaded는 이어받기 시작 오프셋, total은 전체 크기(미확정이면 0).
tmpPath := filepath.Join(os.TempDir(), tmpZipName) // 완료 또는 오류 시 tmpFile을 닫는다.
func downloadBody(resp *http.Response, tmpFile *os.File, tmpPath string, downloaded, total int64) error {
resp, resumeOffset, err := doDownloadRequest(downloadURL, tmpPath)
if err != nil {
return err
}
defer resp.Body.Close()
tmpFile, downloaded, total, err := openTmpFile(resp, tmpPath, resumeOffset)
if err != nil {
return err
}
// 다운로드 루프
buf := make([]byte, 32*1024) buf := make([]byte, 32*1024)
var lastSpeedUpdate time.Time var lastSpeedUpdate time.Time
var lastBytes int64 var lastBytes int64
@@ -166,7 +154,6 @@ func doDownload(downloadURL, destDir string) error {
return fmt.Errorf("다운로드 크기가 제한을 초과했습니다") return fmt.Errorf("다운로드 크기가 제한을 초과했습니다")
} }
// 500ms마다 속도 계산 및 진행률 갱신
now := time.Now() now := time.Now()
if now.Sub(lastSpeedUpdate) >= 500*time.Millisecond { if now.Sub(lastSpeedUpdate) >= 500*time.Millisecond {
elapsed := now.Sub(lastSpeedUpdate).Seconds() elapsed := now.Sub(lastSpeedUpdate).Seconds()
@@ -194,10 +181,29 @@ func doDownload(downloadURL, destDir string) error {
return fmt.Errorf("다운로드 중 오류: %w", readErr) return fmt.Errorf("다운로드 중 오류: %w", readErr)
} }
} }
tmpFile.Close() return tmpFile.Close()
// zip 파일은 ensureGame에서 해시 검증 후 삭제하므로 여기서는 삭제하지 않는다. }
// doDownload 파일을 다운로드하고 zip을 추출하여 destDir에 배치한다.
// 다운로드 완료 후 zip 파일은 tmpZipName 경로에 남겨둔다 (ensureGame에서 해시 검증 후 삭제).
func doDownload(downloadURL, destDir string) error {
tmpPath := filepath.Join(os.TempDir(), tmpZipName)
resp, resumeOffset, err := doDownloadRequest(downloadURL, tmpPath)
if err != nil {
return err
}
defer resp.Body.Close()
tmpFile, downloaded, total, err := openTmpFile(resp, tmpPath, resumeOffset)
if err != nil {
return err
}
if err := downloadBody(resp, tmpFile, tmpPath, downloaded, total); err != nil {
return err
}
// zip 추출
setProgress("압축을 해제하는 중...", -1) setProgress("압축을 해제하는 중...", -1)
tmpExtractDir, err := os.MkdirTemp("", "a301_extract_") tmpExtractDir, err := os.MkdirTemp("", "a301_extract_")