mirror of
https://github.com/fengxxc/wechatmp2markdown.git
synced 2026-03-03 11:10:45 +08:00
feat: 添加三种文章内图片处理方式(原src、保存到本地、内嵌base64)
This commit is contained in:
@@ -4,26 +4,43 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/fengxxc/wechatmp2markdown/format"
|
||||
"github.com/fengxxc/wechatmp2markdown/parse"
|
||||
"github.com/fengxxc/wechatmp2markdown/util"
|
||||
)
|
||||
|
||||
func Start(addr string) {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
wechatmpURL := r.FormValue("url")
|
||||
rawQuery := r.URL.RawQuery
|
||||
paramsMap := parseParams(rawQuery)
|
||||
|
||||
// url param
|
||||
wechatmpURL := paramsMap["url"]
|
||||
fmt.Printf("accept url: %s\n", wechatmpURL)
|
||||
imageArgValue := paramsMap["image"]
|
||||
fmt.Printf(" image: %s\n", imageArgValue)
|
||||
imagePolicy := parse.ImageArgValue2ImagePolicy(imageArgValue)
|
||||
|
||||
if wechatmpURL == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("param 'url' must not be empty. please put in a wechatmp URL and try again."))
|
||||
w.Write([]byte(defHTML))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
var articleStruct parse.Article = parse.ParseFromURL(wechatmpURL)
|
||||
var articleStruct parse.Article = parse.ParseFromURL(wechatmpURL, imagePolicy)
|
||||
title := articleStruct.Title.Val.(string)
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+title+".md")
|
||||
var mdString string = format.Format(articleStruct)
|
||||
w.Write([]byte(mdString))
|
||||
return
|
||||
mdString, saveImageBytes := format.Format(articleStruct)
|
||||
if len(saveImageBytes) > 0 {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+title+".zip")
|
||||
saveImageBytes[title] = []byte(mdString)
|
||||
util.HttpDownloadZip(w, saveImageBytes)
|
||||
} else {
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+title+".md")
|
||||
w.Write([]byte(mdString))
|
||||
}
|
||||
})
|
||||
|
||||
fmt.Printf("wechatmp2markdown server listening on %s\n", addr)
|
||||
@@ -31,3 +48,52 @@ func Start(addr string) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var defHTML string = `
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>wechatmp2markdown</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1 style="text-align: center; width: 100%;">wechatmp2markdown</h1>
|
||||
<ul style="margin: 0 auto; width: 89%;">
|
||||
<li>
|
||||
<strong>param 'url' is required.</strong> please put in a wechatmp URL and try again.
|
||||
</li>
|
||||
<li>
|
||||
<strong>param 'image' is optional</strong>, value include: 'url' / 'save' / 'base64'(default)
|
||||
</li>
|
||||
<li>
|
||||
<strong>example:</strong> http://localhost:8964/?url=https://mp.weixin.qq.com/s?__biz=aaaa==&mid=1111&idx=2&sn=bbbb&chksm=cccc&scene=123&image=save
|
||||
</li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
func parseParams(rawQuery string) map[string]string {
|
||||
result := make(map[string]string)
|
||||
reg := regexp.MustCompile(`(&?image=)([a-z]+)`)
|
||||
matcheImage := reg.FindStringSubmatch(rawQuery)
|
||||
var urlParamFull string = rawQuery
|
||||
if len(matcheImage) > 1 {
|
||||
// 有image参数
|
||||
imageParamFull := matcheImage[0]
|
||||
urlParamFull = strings.Replace(rawQuery, imageParamFull, "", 1)
|
||||
|
||||
if len(matcheImage) > 2 {
|
||||
imageParamVal := matcheImage[2]
|
||||
result["image"] = imageParamVal
|
||||
}
|
||||
}
|
||||
regUrl := regexp.MustCompile(`(&?url=)(.+)`)
|
||||
matcheUrl := regUrl.FindStringSubmatch(urlParamFull)
|
||||
if len(matcheUrl) > 2 {
|
||||
urlParamVal := matcheUrl[2]
|
||||
result["url"] = urlParamVal
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user