Skip to content

Commit 77ef3b7

Browse files
authored
fix: replace DownloadFileWithProxy to stream method (#11811)
* refactor: replace DownloadFileWithProxy with DownloadFileWithProxyStream for improved file downloading logic * refactor: remove commented-out DownloadFileWithProxy function to clean up code
1 parent 498052f commit 77ef3b7

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

core/app/service/script_library.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (u *ScriptService) Sync(req dto.OperateByTaskID) error {
233233
_ = os.MkdirAll(tmpDir, 0755)
234234
}
235235
scriptsUrl := fmt.Sprintf("%s/scripts/scripts.tar.gz", global.CONF.RemoteURL.ResourceURL)
236-
err = files.DownloadFileWithProxy(scriptsUrl, tmpDir+"/scripts.tar.gz")
236+
err = files.DownloadFileWithProxyStream(scriptsUrl, tmpDir+"/scripts.tar.gz")
237237
syncTask.LogWithStatus(i18n.GetMsgByKey("DownloadPackage"), err)
238238
if err != nil {
239239
return fmt.Errorf("download scripts.tar.gz failed, err: %v", err)

core/app/service/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
167167
_ = settingRepo.Update("SystemStatus", "Upgrading")
168168
go func() {
169169
oldLang := common.LoadParams("LANGUAGE")
170-
if err := files.DownloadFileWithProxy(downloadPath+"/"+fileName, downloadDir+"/"+fileName); err != nil {
170+
if err := files.DownloadFileWithProxyStream(downloadPath+"/"+fileName, downloadDir+"/"+fileName); err != nil {
171171
global.LOG.Errorf("download service file failed, err: %v", err)
172172
_ = settingRepo.Update("SystemStatus", "Free")
173173
return

core/utils/files/files.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package files
22

33
import (
4-
"bytes"
54
"crypto/md5"
65
"encoding/hex"
76
"errors"
@@ -189,22 +188,44 @@ func DownloadFile(url, dst string) error {
189188
return nil
190189
}
191190

192-
func DownloadFileWithProxy(url, dst string) error {
193-
_, resp, err := req_helper.HandleRequestWithProxy(url, http.MethodGet, constant.TimeOut5m)
191+
func DownloadFileWithProxyStream(url, dst string) error {
192+
resp, err := req_helper.HandleGetWithProxy(url)
194193
if err != nil {
195194
return err
196195
}
196+
defer resp.Body.Close()
197+
if resp.StatusCode >= http.StatusBadRequest {
198+
return fmt.Errorf("download file [%s] failed, status code: %d", url, resp.StatusCode)
199+
}
197200

198-
out, err := os.Create(dst)
201+
tmpDst := dst + ".part"
202+
_ = os.Remove(tmpDst)
203+
out, err := os.Create(tmpDst)
199204
if err != nil {
200205
return fmt.Errorf("create download file [%s] error, err %s", dst, err.Error())
201206
}
202-
defer out.Close()
207+
success := false
208+
defer func() {
209+
_ = out.Close()
210+
if !success {
211+
_ = os.Remove(tmpDst)
212+
}
213+
}()
203214

204-
reader := bytes.NewReader(resp)
205-
if _, err = io.Copy(out, reader); err != nil {
215+
n, err := io.Copy(out, resp.Body)
216+
if err != nil {
206217
return fmt.Errorf("save download file [%s] error, err %s", dst, err.Error())
207218
}
219+
if resp.ContentLength > 0 && n != resp.ContentLength {
220+
return fmt.Errorf("save download file [%s] error, content-length mismatch: expected %d, actual %d", dst, resp.ContentLength, n)
221+
}
222+
if err = out.Sync(); err != nil {
223+
return fmt.Errorf("sync download file [%s] error, err %s", dst, err.Error())
224+
}
225+
if err = os.Rename(tmpDst, dst); err != nil {
226+
return fmt.Errorf("rename download file [%s] error, err %s", dst, err.Error())
227+
}
228+
success = true
208229
return nil
209230
}
210231

0 commit comments

Comments
 (0)