修复可配置爬虫无法保存问题

This commit is contained in:
marvzhang
2020-03-20 08:27:54 +08:00
parent e33dfd7992
commit 7dc151f030
2 changed files with 22 additions and 0 deletions

View File

@@ -51,6 +51,9 @@ func PutConfigSpider(c *gin.Context) {
// 将FileId置空
spider.FileId = bson.ObjectIdHex(constants.ObjectIdNull)
// UserId
spider.UserId = services.GetCurrentUserId(c)
// 创建爬虫目录
spiderDir := filepath.Join(viper.GetString("spider.path"), spider.Name)
if utils.Exists(spiderDir) {
@@ -109,8 +112,12 @@ func UploadConfigSpider(c *gin.Context) {
spider, err := model.GetSpider(bson.ObjectIdHex(id))
if err != nil {
HandleErrorF(http.StatusBadRequest, c, fmt.Sprintf("cannot find spider (id: %s)", id))
return
}
// UserId
spider.UserId = services.GetCurrentUserId(c)
// 获取上传文件
file, header, err := c.Request.FormFile("file")
if err != nil {
@@ -205,6 +212,11 @@ func PostConfigSpiderSpiderfile(c *gin.Context) {
return
}
// UserId
if !spider.UserId.Valid() {
spider.UserId = bson.ObjectIdHex(constants.ObjectIdNull)
}
// 反序列化
var configData entity.ConfigSpiderData
if err := yaml.Unmarshal([]byte(content), &configData); err != nil {
@@ -247,6 +259,11 @@ func PostConfigSpiderConfig(c *gin.Context) {
return
}
// UserId
if !spider.UserId.Valid() {
spider.UserId = bson.ObjectIdHex(constants.ObjectIdNull)
}
// 反序列化配置数据
var configData entity.ConfigSpiderData
if err := c.ShouldBindJSON(&configData); err != nil {

View File

@@ -126,6 +126,11 @@ func PostSpider(c *gin.Context) {
return
}
// UserId
if !item.UserId.Valid() {
item.UserId = bson.ObjectIdHex(constants.ObjectIdNull)
}
if err := model.UpdateSpider(bson.ObjectIdHex(id), item); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return