Browse Source

base64图片上传

maxiaoshan 2 years ago
parent
commit
b2006986d5
2 changed files with 37 additions and 12 deletions
  1. 4 0
      src/res/util/comm.lua
  2. 33 12
      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

+ 33 - 12
src/spider/script.go

@@ -14,6 +14,7 @@ import (
 	"encoding/base64"
 	"encoding/json"
 	gojs "gorunjs/client"
+	"io"
 	"io/ioutil"
 	mu "mfw/util"
 	"net/http"
@@ -535,19 +536,39 @@ func (s *Script) LoadScript(site *string, code, script_file string, newstate boo
 		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)
-		ret := DownloadFile(s.Downloader, url, method, util.GetTable(param), util.GetTable(head), mycookie, s.Encoding, s.Userproxy, ishttps, s.SCode, s.Timeout)
-		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:]
+			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)
+			ret = DownloadFile(s.Downloader, url, method, util.GetTable(param), util.GetTable(head), mycookie, s.Encoding, s.Userproxy, ishttps, s.SCode, s.Timeout)
+			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:]
+				}
 			}
 		}
 		//特殊处理中国招标投标公共服务平台异常附件过滤