From 97ca3b74b8ffc877581168e0de180faca9da4159 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Wed, 4 Dec 2019 14:54:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=88=AC=E8=99=AB=E8=A2=AB?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/routes/config_spider.go | 10 ++++++++-- backend/services/config_spider.go | 7 ++++++- backend/services/spider_handler/spider.go | 1 - backend/utils/file.go | 1 - frontend/src/views/spider/SpiderList.vue | 1 - 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/routes/config_spider.go b/backend/routes/config_spider.go index 3e0f0e56..e387935a 100644 --- a/backend/routes/config_spider.go +++ b/backend/routes/config_spider.go @@ -254,8 +254,8 @@ func PostConfigSpiderConfig(c *gin.Context) { return } - // 根据序列化后的数据处理爬虫文件 - if err := services.ProcessSpiderFilesFromConfigData(spider, configData); err != nil { + // 校验configData + if err := services.ValidateSpiderfile(configData); err != nil { HandleError(http.StatusInternalServerError, c, err) return } @@ -266,6 +266,12 @@ func PostConfigSpiderConfig(c *gin.Context) { return } + // 根据序列化后的数据处理爬虫文件 + if err := services.ProcessSpiderFilesFromConfigData(spider, configData); err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + c.JSON(http.StatusOK, Response{ Status: "ok", Message: "success", diff --git a/backend/services/config_spider.go b/backend/services/config_spider.go index adce0531..7c736cc7 100644 --- a/backend/services/config_spider.go +++ b/backend/services/config_spider.go @@ -245,7 +245,12 @@ func GenerateSpiderfileFromConfigData(spider model.Spider, configData entity.Con } // 打开文件 - f, err := os.OpenFile(sfPath, os.O_WRONLY|os.O_TRUNC, 0777) + var f *os.File + if utils.Exists(sfPath) { + f, err = os.OpenFile(sfPath, os.O_WRONLY|os.O_TRUNC, 0777) + } else { + f, err = os.OpenFile(sfPath, os.O_CREATE, 0777) + } if err != nil { return err } diff --git a/backend/services/spider_handler/spider.go b/backend/services/spider_handler/spider.go index e8ec6a19..c3a2500d 100644 --- a/backend/services/spider_handler/spider.go +++ b/backend/services/spider_handler/spider.go @@ -100,7 +100,6 @@ func (s *SpiderSync) Download() { // 创建临时文件 tmpFilePath := filepath.Join(tmpPath, randomId.String()+".zip") tmpFile := utils.OpenFile(tmpFilePath) - defer utils.Close(tmpFile) // 将该文件写入临时文件 if _, err := io.Copy(tmpFile, f); err != nil { diff --git a/backend/utils/file.go b/backend/utils/file.go index 2dacc9ed..c71b2cb0 100644 --- a/backend/utils/file.go +++ b/backend/utils/file.go @@ -167,7 +167,6 @@ func DeCompress(srcFile *os.File, dstPath string) error { debug.PrintStack() continue } - defer Close(newFile) // 拷贝该文件到新文件中 if _, err := io.Copy(newFile, srcFile); err != nil { diff --git a/frontend/src/views/spider/SpiderList.vue b/frontend/src/views/spider/SpiderList.vue index cc3d6acb..78c87a36 100644 --- a/frontend/src/views/spider/SpiderList.vue +++ b/frontend/src/views/spider/SpiderList.vue @@ -281,7 +281,6 @@ import { import dayjs from 'dayjs' import CrawlConfirmDialog from '../../components/Common/CrawlConfirmDialog' import StatusTag from '../../components/Status/StatusTag' -import request from '../../api/request' export default { name: 'SpiderList',