From 21b1242a942b2d1522310dd1c25f3dd01d3f2f3b Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:12:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E9=83=A8=E5=88=86=20O?= =?UTF-8?q?neDrive=20=E4=B8=8A=E4=BC=A0=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#4117)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #4125 --- backend/app/service/snapshot_create.go | 15 +++++-------- .../utils/cloud_storage/client/onedrive.go | 22 ++++++++++++++----- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/backend/app/service/snapshot_create.go b/backend/app/service/snapshot_create.go index 3919b1f82..b0f2a8884 100644 --- a/backend/app/service/snapshot_create.go +++ b/backend/app/service/snapshot_create.go @@ -154,10 +154,6 @@ func snapPanelData(snap snapHelper, localDir, targetDir string) { } func snapCompress(snap snapHelper, rootDir string) { - defer func() { - global.LOG.Debugf("remove snapshot file %s", rootDir) - _ = os.RemoveAll(rootDir) - }() _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusRunning}) tmpDir := path.Join(global.CONF.System.TmpDir, "system") fileName := fmt.Sprintf("%s.tar.gz", path.Base(rootDir)) @@ -178,15 +174,13 @@ func snapCompress(snap snapHelper, rootDir string) { snap.Status.Compress = constant.StatusDone snap.Status.Size = size _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusDone, "size": size}) + + global.LOG.Debugf("remove snapshot file %s", rootDir) + _ = os.RemoveAll(rootDir) } func snapUpload(snap snapHelper, accounts string, file string) { source := path.Join(global.CONF.System.TmpDir, "system", path.Base(file)) - defer func() { - global.LOG.Debugf("remove snapshot file %s", source) - _ = os.Remove(source) - }() - _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": constant.StatusUploading}) accountMap, err := loadClientMap(accounts) if err != nil { @@ -207,6 +201,9 @@ func snapUpload(snap snapHelper, accounts string, file string) { } snap.Status.Upload = constant.StatusDone _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"upload": constant.StatusDone}) + + global.LOG.Debugf("remove snapshot file %s", source) + _ = os.Remove(source) } func handleSnapTar(sourceDir, targetDir, name, exclusionRules string) error { diff --git a/backend/utils/cloud_storage/client/onedrive.go b/backend/utils/cloud_storage/client/onedrive.go index 5a5a65acd..99dabd5b3 100644 --- a/backend/utils/cloud_storage/client/onedrive.go +++ b/backend/utils/cloud_storage/client/onedrive.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "crypto/tls" "encoding/json" "errors" "fmt" @@ -14,6 +15,7 @@ import ( "path" "strconv" "strings" + "time" "github.com/1Panel-dev/1Panel/backend/utils/files" odsdk "github.com/goh-chunlin/go-onedrive/onedrive" @@ -321,7 +323,6 @@ func (o *oneDriveClient) upSmall(srcPath, folderID string, fileSize int64) (bool if err := o.client.Do(context.Background(), req, false, &response); err != nil { return false, fmt.Errorf("do request for list failed, err: %v", err) } - fmt.Println(response) return true, nil } @@ -362,7 +363,13 @@ func (o *oneDriveClient) upBig(ctx context.Context, srcPath, folderID string, fi splitCount += 1 } bfReader := bufio.NewReader(file) - var fileUploadResp *UploadSessionUploadResponse + httpClient := http.Client{ + Timeout: time.Minute * 10, + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, + } for splitNow := int64(0); splitNow < splitCount; splitNow++ { length, err := bfReader.Read(buffer) if err != nil { @@ -376,12 +383,15 @@ func (o *oneDriveClient) upBig(ctx context.Context, srcPath, folderID string, fi if err != nil { return false, err } - if err := o.client.Do(ctx, sessionFileUploadReq, false, &fileUploadResp); err != nil { + res, err := httpClient.Do(sessionFileUploadReq) + if err != nil { return false, err } - } - if fileUploadResp.Id == "" { - return false, errors.New("something went wrong. file upload incomplete. consider upload the file in a step-by-step manner") + if res.StatusCode != 201 && res.StatusCode != 202 && res.StatusCode != 200 { + data, _ := io.ReadAll(res.Body) + res.Body.Close() + return false, errors.New(string(data)) + } } return true, nil }