From 435f91a5e1fabf9a75b3e938492e0dd85c6455ce Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Tue, 10 Sep 2024 15:59:53 +0800 Subject: [PATCH] update PreviewFile and DeleteFile (#187) --- node/delete_file.go | 20 ++++++++++++++++++++ node/open_file.go | 22 ++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/node/delete_file.go b/node/delete_file.go index 6bd6e43..916bad2 100644 --- a/node/delete_file.go +++ b/node/delete_file.go @@ -8,9 +8,13 @@ package node import ( + "errors" "net/http" + "os" + "path/filepath" "github.com/CESSProject/DeOSS/common/utils" + "github.com/CESSProject/cess-go-sdk/chain" "github.com/gin-gonic/gin" ) @@ -46,6 +50,22 @@ func (n *Node) DeleteFile(c *gin.Context) { return } n.RemoveCacheRecord(fid) + n.Logdel("info", clientIp+" DeleteFile suc: "+blockHash) c.JSON(200, map[string]string{"block hash": blockHash}) + + _, err = n.QueryFile(fid, -1) + if err != nil { + if errors.Is(err, chain.ERR_RPC_EMPTY_VALUE) { + data, err := n.ParseTrackFile(fid) + if err == nil { + for _, segment := range data.Segment { + for _, fragment := range segment.FragmentHash { + os.Remove(fragment) + } + } + } + os.Remove(filepath.Join(n.fileDir, fid)) + } + } } diff --git a/node/open_file.go b/node/open_file.go index 880cab0..b8cd0a9 100644 --- a/node/open_file.go +++ b/node/open_file.go @@ -39,11 +39,25 @@ func (n *Node) PreviewFile(c *gin.Context) { if clientIp == "" { clientIp = c.ClientIP() } - temp := strings.Split(fid, ".") - fid = temp[0] - if format == "" && len(temp) > 1 { - format = temp[1] + + if strings.Contains(fid, ".") { + temp := strings.Split(fid, ".") + fid = temp[0] + if format == "" && len(temp) > 1 { + format = temp[1] + } + } else { + if len(fid) > chain.FileHashLen { + tmp_fid := fid[:chain.FileHashLen] + format = fid[chain.FileHashLen:] + fid = tmp_fid + } else if len(fid) < chain.FileHashLen { + n.Logopen("err", clientIp+" invalid fid: "+fid) + c.JSON(404, "invalid fid") + return + } } + n.Logopen("info", clientIp+" open file: "+fid+" account: "+account+" format: "+format+" Range: "+rgn) var err error