浏览代码

base64附件上传

maxiaoshan 2 年之前
父节点
当前提交
0ce8d7c04a
共有 2 个文件被更改,包括 82 次插入34 次删除
  1. 4 0
      src/res/util/comm.lua
  2. 78 34
      src/spider/script.go

+ 4 - 0
src/res/util/comm.lua

@@ -587,6 +587,10 @@ function common.getFileAttachmentsArrayWithTag(href,tags,content,withend,param,h
 		local url,name,size,ftype,fid=downloadFile(file_name, v["href"], "get",param,head,ck)
 		-- 附件原地址(默认为空)
 		local init_url = v["href"]
+		local r,b = stringFind(init_url,"data:image")
+		if b then
+			init_url = ""
+		end
 		if url == "" then
 			local u = 0
 			while u < 2 do

+ 78 - 34
src/spider/script.go

@@ -15,6 +15,7 @@ import (
 	"encoding/json"
 	"github.com/shopspring/decimal"
 	gojs "gorunjs/client"
+	"io"
 	"io/ioutil"
 	mu "mfw/util"
 	"net/http"
@@ -241,42 +242,62 @@ func (s *Script) LoadScript(site, channel, user *string, code, script_file strin
 		url := S.ToString(-5)
 		fileName := S.ToString(-6)
 		ishttps := strings.Contains(url, "https")
-		var mycookie []*http.Cookie
-		if cookie != "{}" {
-			json.Unmarshal([]byte(cookie), &mycookie)
+		//base64匹配
+		base64UrlReg := regexp.MustCompile("data:image")
+		indexArr := base64UrlReg.FindStringIndex(url)
+		name, size, ftype, fid := "", "", "", ""
+		var ret []byte
+		var err error
+		//base64 url
+		if len(indexArr) == 2 { //base64 http://www.mmjyjt.com/data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqAAAAOwCAYAAAD
+			//截取base64
+			start := indexArr[0]
+			url = url[start:]
+			fileName = "文件下载.jpg"
+			index := strings.Index(url, ",")
+			dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(url[index+1:]))
+			ret, err = io.ReadAll(dec)
+			if err == nil && len(ret) > 0 {
+				url, name, size, ftype, fid = util.UploadFile(s.SCode, fileName, "", ret)
+			}
 		} else {
-			mycookie = make([]*http.Cookie, 0)
-		}
-		fileName = strings.TrimSpace(fileName)
-		url = strings.TrimSpace(url)
-		var retLen int64
-		ret := DownloadFile(&retLen, s.Downloader, url, method, util.GetTable(param), util.GetTable(head), mycookie, s.Encoding, s.Userproxy, ishttps, s.SCode, s.Timeout)
-		//流量统计
-		//if retLen > 0 {
-		//	key := Today + "+" + code
-		//	if sf, ok := SpiderFlowMap.Load(key); ok && sf != nil {
-		//		if sfMap, ok := sf.(*SpiderFlow); ok {
-		//			sfMap.Flow += retLen
-		//			//sfMap.Site = *site
-		//			//sfMap.Channel = *channel
-		//			//sfMap.ModifyUser = *user
-		//			SpiderFlowMap.Store(key, sfMap)
-		//		}
-		//	} else {
-		//		SpiderFlowMap.Store(key, &SpiderFlow{
-		//			//Code:       code,
-		//			Site:       *site,
-		//			Channel:    *channel,
-		//			Flow:       retLen,
-		//			ModifyUser: *user,
-		//		})
-		//	}
-		//}
+			var mycookie []*http.Cookie
+			if cookie != "{}" {
+				json.Unmarshal([]byte(cookie), &mycookie)
+			} else {
+				mycookie = make([]*http.Cookie, 0)
+			}
+			fileName = strings.TrimSpace(fileName)
+			url = strings.TrimSpace(url)
+			var retLen int64
+			ret = DownloadFile(&retLen, s.Downloader, url, method, util.GetTable(param), util.GetTable(head), mycookie, s.Encoding, s.Userproxy, ishttps, s.SCode, s.Timeout)
+			//流量统计
+			//if retLen > 0 {
+			//	key := Today + "+" + code
+			//	if sf, ok := SpiderFlowMap.Load(key); ok && sf != nil {
+			//		if sfMap, ok := sf.(*SpiderFlow); ok {
+			//			sfMap.Flow += retLen
+			//			//sfMap.Site = *site
+			//			//sfMap.Channel = *channel
+			//			//sfMap.ModifyUser = *user
+			//			SpiderFlowMap.Store(key, sfMap)
+			//		}
+			//	} else {
+			//		SpiderFlowMap.Store(key, &SpiderFlow{
+			//			//Code:       code,
+			//			Site:       *site,
+			//			Channel:    *channel,
+			//			Flow:       retLen,
+			//			ModifyUser: *user,
+			//		})
+			//	}
+			//}
 
-		url, name, size, ftype, fid := util.UploadFile(s.SCode, fileName, url, ret)
-		if strings.TrimSpace(ftype) == "" {
-			if len(path.Ext(name)) > 0 {
-				ftype = path.Ext(name)[1:]
+			url, name, size, ftype, fid = util.UploadFile(s.SCode, fileName, url, ret)
+			if strings.TrimSpace(ftype) == "" {
+				if len(path.Ext(name)) > 0 {
+					ftype = path.Ext(name)[1:]
+				}
 			}
 		}
 		//特殊处理中国招标投标公共服务平台异常附件过滤
@@ -303,6 +324,29 @@ func (s *Script) LoadScript(site, channel, user *string, code, script_file strin
 		s.FileLastThreeTimes = append(s.FileLastThreeTimes, end)
 		return 5
 	}))
+	//下载、上传base64图片
+	s.L.SetGlobal("downloadBase64File", s.L.NewFunction(func(S *lua.LState) int {
+		url := S.ToString(-3)
+		fileName := S.ToString(-2)
+		base64Img := S.ToString(-1)
+		if fileName == "" {
+			fileName = "文件下载"
+		}
+		fileName = fileName + ".jpg"
+		i := strings.Index(base64Img, ",")
+		dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64Img[i+1:]))
+		ret, err := io.ReadAll(dec)
+		name, size, ftype, fid := "", "", "", ""
+		if err == nil && len(ret) > 0 {
+			url, name, size, ftype, fid = util.UploadFile(s.SCode, fileName, url, ret)
+		}
+		S.Push(lua.LString(url))
+		S.Push(lua.LString(name))
+		S.Push(lua.LString(size))
+		S.Push(lua.LString(ftype))
+		S.Push(lua.LString(fid))
+		return 5
+	}))
 	//保存验证错误日志
 	s.L.SetGlobal("saveErrLog", s.L.NewFunction(func(S *lua.LState) int {
 		code := S.ToString(-4)