go-fastdfs/server/http_remove.go

137 lines
3.4 KiB
Go
Raw Normal View History

package server
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
2021-05-17 16:33:44 +08:00
"github.com/astaxie/beego/httplib"
log "github.com/sjqzhang/seelog"
"github.com/syndtr/goleveldb/leveldb/util"
)
func (c *Server) RemoveDownloading() {
RemoveDownloadFunc := func() {
for {
iter := c.ldb.NewIterator(util.BytesPrefix([]byte("downloading_")), nil)
for iter.Next() {
key := iter.Key()
keys := strings.Split(string(key), "_")
if len(keys) == 3 {
if t, err := strconv.ParseInt(keys[1], 10, 64); err == nil && time.Now().Unix()-t > 60*10 {
os.Remove(DOCKER_DIR + keys[2])
}
}
}
iter.Release()
time.Sleep(time.Minute * 3)
}
}
go RemoveDownloadFunc()
}
func (c *Server) RemoveEmptyDir(w http.ResponseWriter, r *http.Request) {
var (
result JsonResult
)
result.Status = "ok"
if c.IsPeer(r) {
2023-10-31 15:50:37 +08:00
//go c.util.RemoveEmptyDir(DATA_DIR)
go cleanEmptyFolders(STORE_DIR)
result.Message = "clean job start ..,don't try again!!!"
w.Write([]byte(c.util.JsonEncodePretty(result)))
} else {
result.Message = c.GetClusterNotPermitMessage(r)
w.Write([]byte(c.util.JsonEncodePretty(result)))
}
}
func (c *Server) RemoveFile(w http.ResponseWriter, r *http.Request) {
var (
err error
md5sum string
fileInfo *FileInfo
fpath string
delUrl string
result JsonResult
inner string
name string
)
_ = delUrl
_ = inner
r.ParseForm()
md5sum = r.FormValue("md5")
fpath = r.FormValue("path")
inner = r.FormValue("inner")
result.Status = "fail"
if !c.IsPeer(r) {
w.Write([]byte(c.GetClusterNotPermitMessage(r)))
return
}
if Config().AuthUrl != "" && !c.CheckAuth(w, r) {
c.NotPermit(w, r)
return
}
if fpath != "" && md5sum == "" {
2021-05-28 17:46:12 +08:00
if Config().Group!="" && Config().SupportGroupManage {
fpath = strings.Replace(fpath, "/"+Config().Group+"/", STORE_DIR_NAME+"/", 1)
} else {
fpath = strings.Replace(fpath, "/", STORE_DIR_NAME+"/", 1)
}
md5sum = c.util.MD5(fpath)
}
if inner != "1" {
for _, peer := range Config().Peers {
delFile := func(peer string, md5sum string, fileInfo *FileInfo) {
delUrl = fmt.Sprintf("%s%s", peer, c.getRequestURI("delete"))
req := httplib.Post(delUrl)
req.Param("md5", md5sum)
req.Param("inner", "1")
req.SetTimeout(time.Second*5, time.Second*10)
if _, err = req.String(); err != nil {
log.Error(err)
}
}
go delFile(peer, md5sum, fileInfo)
}
}
if len(md5sum) < 32 {
result.Message = "md5 unvalid"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if fileInfo, err = c.GetFileInfoFromLevelDB(md5sum); err != nil {
result.Message = err.Error()
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
if fileInfo.OffSet >= 0 {
result.Message = "small file delete not support"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
name = fileInfo.Name
if fileInfo.ReName != "" {
name = fileInfo.ReName
}
fpath = fileInfo.Path + "/" + name
if fileInfo.Path != "" && c.util.FileExists(DOCKER_DIR+fpath) {
c.SaveFileMd5Log(fileInfo, CONST_REMOME_Md5_FILE_NAME)
if err = os.Remove(DOCKER_DIR + fpath); err != nil {
result.Message = err.Error()
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
} else {
result.Message = "remove success"
result.Status = "ok"
w.Write([]byte(c.util.JsonEncodePretty(result)))
return
}
}
result.Message = "fail remove"
w.Write([]byte(c.util.JsonEncodePretty(result)))
}