fix 上传爬虫名称错误的问题

This commit is contained in:
陈景阳
2019-09-27 11:30:38 +08:00
parent 0847c6a991
commit f5371f94a4
3 changed files with 25 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/apex/log"
"github.com/globalsign/mgo/bson"
"os"
"runtime/debug"
"time"
)
@@ -25,6 +26,19 @@ type File struct {
Size int64 `json:"size"`
}
func GetAllGridFs() []*GridFs {
s, gf := database.GetGridFs("files")
defer s.Close()
var files []*GridFs
if err := gf.Find(nil).All(&files); err != nil {
log.Errorf("get all files error: {}", err.Error())
debug.PrintStack()
return nil
}
return files
}
func GetGridFs(id bson.ObjectId) *GridFs {
s, gf := database.GetGridFs("files")
defer s.Close()
@@ -33,6 +47,7 @@ func GetGridFs(id bson.ObjectId) *GridFs {
err := gf.Find(bson.M{"_id": id}).One(&gfFile)
if err != nil {
log.Errorf("get gf file error: %s, file_id: %s", err.Error(), id.Hex())
debug.PrintStack()
return nil
}
return &gfFile
@@ -41,6 +56,7 @@ func GetGridFs(id bson.ObjectId) *GridFs {
func RemoveFile(path string) error {
if !utils.Exists(path) {
log.Info("file not found: " + path)
debug.PrintStack()
return nil
}
if err := os.Remove(path); err != nil {

View File

@@ -124,6 +124,10 @@ func GetSpiderList(filter interface{}, skip int, limit int) ([]Spider, int, erro
return spiders, count, nil
}
func GetSpiderByFileId(fileId bson.ObjectId) {
}
func GetSpiderByName(name string) *Spider {
s, c := database.GetCol("spiders")
defer s.Close()

View File

@@ -161,8 +161,12 @@ func PutSpider(c *gin.Context) {
debug.PrintStack()
return
}
idx := strings.LastIndex(uploadFile.Filename, "/")
targetFilename := uploadFile.Filename[idx+1:]
// 判断爬虫是否存在
spiderName := strings.Replace(uploadFile.Filename, ".zip", "", -1)
spiderName := strings.Replace(targetFilename, ".zip", "", 1)
spider := model.GetSpiderByName(spiderName)
if spider == nil {
// 保存爬虫信息