From 4de3772c6deb4792a5f9a3fb6d9151089897dda4 Mon Sep 17 00:00:00 2001 From: "a624669980@163.com" Date: Wed, 8 Jun 2022 19:26:54 +0800 Subject: [PATCH] add cancel file operate --- CHANGELOG.md | 2 +- route/route.go | 1 + route/v1/file.go | 25 ++++++++++++- service/notify.go | 94 +++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 113 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2369b5..5b1c23c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -## [0.3.2-pre] +## [0.3.2-pre] - 2022-06-08 ### Added diff --git a/route/route.go b/route/route.go index 23d585a..f423ab4 100644 --- a/route/route.go +++ b/route/route.go @@ -152,6 +152,7 @@ func InitRouter() *gin.Engine { v1FileGroup.DELETE("/delete", v1.DeleteFile) v1FileGroup.PUT("/update", v1.PutFileContent) v1FileGroup.GET("/image", v1.GetFileImage) + v1FileGroup.DELETE("/operate/:id", v1.DeleteOperateFileOrDir) //v1FileGroup.GET("/download", v1.UserFileDownloadCommonService) } diff --git a/route/v1/file.go b/route/v1/file.go index 6122bd9..4842215 100644 --- a/route/v1/file.go +++ b/route/v1/file.go @@ -15,6 +15,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "github.com/IceWhaleTech/CasaOS/model" "github.com/IceWhaleTech/CasaOS/pkg/config" @@ -514,7 +515,7 @@ func PostOperateFileOrDir(c *gin.Context) { if len(service.OpStrArr) == 1 { go service.ExecOpFile() go service.CheckFileStatus() - go service.MyService.Notify().SendFileOperateNotify() + go service.MyService.Notify().SendFileOperateNotify(false) } c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) @@ -623,3 +624,25 @@ func GetFileImage(c *gin.Context) { } c.Writer.WriteString(string(data)) } + +func DeleteOperateFileOrDir(c *gin.Context) { + id := c.Param("id") + if id == "0" { + service.FileQueue = sync.Map{} + service.OpStrArr = []string{} + } else { + + service.FileQueue.Delete(id) + tempList := []string{} + for _, v := range service.OpStrArr { + if v != id { + tempList = append(tempList, v) + } + } + service.OpStrArr = tempList + + } + + go service.MyService.Notify().SendFileOperateNotify(true) + c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) +} diff --git a/service/notify.go b/service/notify.go index 7eebdee..ab52bc9 100644 --- a/service/notify.go +++ b/service/notify.go @@ -35,7 +35,7 @@ type NotifyServer interface { SendUSBInfoBySocket(list []model2.DriveUSB) SendDiskInfoBySocket(disk model2.Summary) SendPersonStatusBySocket(status notify.Person) - SendFileOperateNotify() + SendFileOperateNotify(nowSend bool) SendInstallAppBySocket(app notify.Application) } @@ -44,19 +44,32 @@ type notifyServer struct { } // Send periodic broadcast messages -func (i *notifyServer) SendFileOperateNotify() { - for { +func (i *notifyServer) SendFileOperateNotify(nowSend bool) { + if nowSend { len := 0 FileQueue.Range(func(k, v interface{}) bool { len++ return true }) + model := notify.NotifyModel{} + listMsg := make(map[string]interface{}) if len == 0 { + model.Data = []string{} + + listMsg["file_operate"] = model + msg := gosf.Message{} + msg.Success = true + msg.Body = listMsg + msg.Text = "file_operate" + + notify := notify.Message{} + notify.Path = "file_operate" + notify.Msg = msg + NotifyMsg <- notify return } - listMsg := make(map[string]interface{}) - model := notify.NotifyModel{} + model.State = "NORMAL" list := []notify.File{} OpStrArrbak := OpStrArr @@ -78,7 +91,7 @@ func (i *notifyServer) SendFileOperateNotify() { } else { task.Status = "PROCESSING" } - if temp.ProcessedSize == temp.TotalSize { + if temp.ProcessedSize >= temp.TotalSize { task.Finished = true task.Status = "FINISHED" FileQueue.Delete(v) @@ -109,8 +122,75 @@ func (i *notifyServer) SendFileOperateNotify() { notify.Path = "file_operate" notify.Msg = msg NotifyMsg <- notify - time.Sleep(time.Second * 3) + } else { + for { + + len := 0 + FileQueue.Range(func(k, v interface{}) bool { + len++ + return true + }) + if len == 0 { + return + } + listMsg := make(map[string]interface{}) + model := notify.NotifyModel{} + model.State = "NORMAL" + list := []notify.File{} + OpStrArrbak := OpStrArr + + for _, v := range OpStrArrbak { + tempItem, ok := FileQueue.Load(v) + temp := tempItem.(model2.FileOperate) + if !ok { + continue + } + task := notify.File{} + task.Id = v + task.ProcessedSize = temp.ProcessedSize + task.TotalSize = temp.TotalSize + task.To = temp.To + task.Type = temp.Type + if task.ProcessedSize == 0 { + task.Status = "STARTING" + } else { + task.Status = "PROCESSING" + } + if temp.ProcessedSize >= temp.TotalSize { + task.Finished = true + task.Status = "FINISHED" + FileQueue.Delete(v) + OpStrArr = OpStrArr[1:] + go ExecOpFile() + list = append(list, task) + continue + } + for _, v := range temp.Item { + if v.Size != v.ProcessedSize { + task.ProcessingPath = v.From + break + } + } + + list = append(list, task) + } + model.Data = list + + listMsg["file_operate"] = model + + msg := gosf.Message{} + msg.Success = true + msg.Body = listMsg + msg.Text = "file_operate" + + notify := notify.Message{} + notify.Path = "file_operate" + notify.Msg = msg + NotifyMsg <- notify + time.Sleep(time.Second * 3) + } } + } func (i *notifyServer) SendPersonStatusBySocket(status notify.Person) {