更新 main.go

This commit is contained in:
XOF
2025-12-17 02:12:11 +08:00
parent e3f2409c23
commit 3d0dbe7cf2

33
main.go
View File

@@ -174,8 +174,8 @@ func checkAndDownload() {
return
}
var data []byte
client := &http.Client{}
var downloaded bool
for i := 0; i < maxRetries; i++ {
resp, err := client.Get(downloadURL)
@@ -185,30 +185,37 @@ func checkAndDownload() {
continue
}
data, err = io.ReadAll(resp.Body)
tmpFile := filePath + ".tmp"
f, err := os.Create(tmpFile)
if err != nil {
resp.Body.Close()
log.Printf("Attempt %d: failed to create file: %v", i+1, err)
time.Sleep(time.Duration(i+1) * 10 * time.Second)
continue
}
written, err := io.Copy(f, resp.Body)
f.Close()
resp.Body.Close()
if err == nil && len(data) > 10000000 {
if err == nil && written > 10000000 {
os.Rename(tmpFile, filePath)
os.WriteFile(sha256File, []byte(sha256), 0644)
log.Printf("Downloaded: %s (%.2f MB)", filename, float64(written)/1024/1024)
downloaded = true
break
}
log.Printf("Attempt %d: invalid response (size: %d bytes)", i+1, len(data))
os.Remove(tmpFile)
log.Printf("Attempt %d: invalid response (size: %d bytes)", i+1, written)
time.Sleep(time.Duration(i+1) * 10 * time.Second)
}
if len(data) < 10000000 {
if !downloaded {
log.Println("All download attempts failed")
return
}
if err := os.WriteFile(filePath, data, 0644); err != nil {
log.Printf("Error saving file: %v", err)
return
}
os.WriteFile(sha256File, []byte(sha256), 0644)
log.Printf("Downloaded: %s (%.2f MB)", filename, float64(len(data))/1024/1024)
cleanupOldVersions()
}