refactor: doDownload에서 downloadBody 분리
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
46
download.go
46
download.go
@@ -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))
|
||||
}
|
||||
|
||||
// doDownload 파일을 다운로드하고 zip을 추출하여 destDir에 배치한다.
|
||||
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
|
||||
}
|
||||
|
||||
// 다운로드 루프
|
||||
// downloadBody 응답 본문을 tmpFile에 쓰고 진행률을 갱신한다.
|
||||
// downloaded는 이어받기 시작 오프셋, total은 전체 크기(미확정이면 0).
|
||||
// 완료 또는 오류 시 tmpFile을 닫는다.
|
||||
func downloadBody(resp *http.Response, tmpFile *os.File, tmpPath string, downloaded, total int64) error {
|
||||
buf := make([]byte, 32*1024)
|
||||
var lastSpeedUpdate time.Time
|
||||
var lastBytes int64
|
||||
@@ -166,7 +154,6 @@ func doDownload(downloadURL, destDir string) error {
|
||||
return fmt.Errorf("다운로드 크기가 제한을 초과했습니다")
|
||||
}
|
||||
|
||||
// 500ms마다 속도 계산 및 진행률 갱신
|
||||
now := time.Now()
|
||||
if now.Sub(lastSpeedUpdate) >= 500*time.Millisecond {
|
||||
elapsed := now.Sub(lastSpeedUpdate).Seconds()
|
||||
@@ -194,10 +181,29 @@ func doDownload(downloadURL, destDir string) error {
|
||||
return fmt.Errorf("다운로드 중 오류: %w", readErr)
|
||||
}
|
||||
}
|
||||
tmpFile.Close()
|
||||
// zip 파일은 ensureGame에서 해시 검증 후 삭제하므로 여기서는 삭제하지 않는다.
|
||||
return tmpFile.Close()
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
tmpExtractDir, err := os.MkdirTemp("", "a301_extract_")
|
||||
|
||||
Reference in New Issue
Block a user