diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index ea7fd8ced..1bcc902b4 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -512,7 +512,7 @@ func (b *BaseApi) Ws(c *gin.Context) { if err != nil { return } - wsClient := websocket2.NewWsClient("13232", ws) + wsClient := websocket2.NewWsClient("wsClient", ws) go wsClient.Read() go wsClient.Write() } diff --git a/backend/app/service/file.go b/backend/app/service/file.go index 03fa66182..0aa6e751d 100644 --- a/backend/app/service/file.go +++ b/backend/app/service/file.go @@ -143,9 +143,8 @@ func (f FileService) ChangeMode(op request.FileCreate) error { func (f FileService) Compress(c request.FileCompress) error { fo := files.NewFileOp() if !c.Replace && fo.Stat(filepath.Join(c.Dst, c.Name)) { - return errors.New("file is exist") + return buserr.New(constant.ErrFileIsExit) } - return fo.Compress(c.Files, c.Dst, c.Name, files.CompressType(c.Type)) } @@ -216,7 +215,6 @@ func (f FileService) MvFile(m request.FileMove) error { if errString != "" { return errors.New(errString) } - return nil } diff --git a/backend/utils/files/file_op.go b/backend/utils/files/file_op.go index 33ca60dfa..94af994ce 100644 --- a/backend/utils/files/file_op.go +++ b/backend/utils/files/file_op.go @@ -130,18 +130,18 @@ func (w *WriteCounter) Write(p []byte) (n int, err error) { } func (w *WriteCounter) SaveProcess() { - percent := float64(w.Written) / float64(w.Total) * 100 - percentValue, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", percent), 64) - + percentValue := 0.0 + if w.Total > 0 { + percent := float64(w.Written) / float64(w.Total) * 100 + percentValue, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", percent), 64) + } process := Process{ Total: w.Total, Written: w.Written, Percent: percentValue, Name: w.Name, } - by, _ := json.Marshal(process) - if percentValue < 100 { if err := global.CACHE.Set(w.Key, string(by)); err != nil { global.LOG.Errorf("save cache error, err %s", err.Error()) @@ -154,33 +154,49 @@ func (w *WriteCounter) SaveProcess() { } func (f FileOp) DownloadFileWithProcess(url, dst, key string) error { - resp, err := http.Get(url) + client := &http.Client{} + request, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil + } + request.Header.Set("Accept-Encoding", "identity") + resp, err := client.Do(request) if err != nil { global.LOG.Errorf("get download file [%s] error, err %s", dst, err.Error()) return err } - header, err := http.Head(url) - if err != nil { - global.LOG.Errorf("get download file [%s] error, err %s", dst, err.Error()) - return err - } - out, err := os.Create(dst) if err != nil { global.LOG.Errorf("create download file [%s] error, err %s", dst, err.Error()) return err } - go func() { counter := &WriteCounter{} counter.Key = key - counter.Total = uint64(header.ContentLength) + if resp.ContentLength > 0 { + counter.Total = uint64(resp.ContentLength) + } counter.Name = filepath.Base(dst) if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil { global.LOG.Errorf("save download file [%s] error, err %s", dst, err.Error()) } out.Close() resp.Body.Close() + + value, err := global.CACHE.Get(counter.Key) + if err != nil { + global.LOG.Errorf("get cache error,err %s", err.Error()) + return + } + process := &Process{} + _ = json.Unmarshal(value, process) + process.Percent = 100 + process.Name = counter.Name + process.Total = process.Written + by, _ := json.Marshal(process) + if err := global.CACHE.SetWithTTL(counter.Key, string(by), time.Second*time.Duration(10)); err != nil { + global.LOG.Errorf("save cache error, err %s", err.Error()) + } }() return nil } diff --git a/backend/utils/websocket/client.go b/backend/utils/websocket/client.go index 60ac167a4..2665d2e05 100644 --- a/backend/utils/websocket/client.go +++ b/backend/utils/websocket/client.go @@ -30,7 +30,6 @@ func (c *Client) Read() { defer func() { close(c.Msg) }() - for { _, message, err := c.Socket.ReadMessage() if err != nil { @@ -46,7 +45,6 @@ func (c *Client) Write() { defer func() { c.Socket.Close() }() - for { message, ok := <-c.Msg if !ok { @@ -57,7 +55,6 @@ func (c *Client) Write() { } func ProcessData(c *Client, msg *WsMsg) { - if msg.Type == "wget" { var res []files.Process for _, k := range msg.Keys { @@ -66,7 +63,6 @@ func ProcessData(c *Client, msg *WsMsg) { global.LOG.Errorf("get cache error,err %s", err.Error()) return } - process := &files.Process{} _ = json.Unmarshal(value, process) res = append(res, *process) diff --git a/frontend/src/views/host/file-management/process/index.vue b/frontend/src/views/host/file-management/process/index.vue index ed097d723..40f3de4b9 100644 --- a/frontend/src/views/host/file-management/process/index.vue +++ b/frontend/src/views/host/file-management/process/index.vue @@ -1,12 +1,19 @@ @@ -15,7 +22,6 @@ import { FileKeys } from '@/api/modules/files'; import { computeSize } from '@/utils/util'; import { onBeforeUnmount, ref, toRefs } from 'vue'; -import DrawerHeader from '@/components/drawer-header/index.vue'; const props = defineProps({ open: { @@ -54,7 +60,7 @@ const onClose = () => {}; const initProcess = () => { let href = window.location.href; - let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss'; + let protocol = href.split('//')[0] === 'http' ? 'ws' : 'wss'; let ipLocal = href.split('//')[1].split('/')[0]; processSocket = new WebSocket(`${protocol}://${ipLocal}/api/v1/files/ws`); processSocket.onopen = onOpenProcess; @@ -65,6 +71,8 @@ const initProcess = () => { }; const getKeys = () => { + keys.value = []; + res.value = []; FileKeys().then((res) => { if (res.data.keys.length > 0) { keys.value = res.data.keys;