mirror of
https://github.com/fengxxc/wechatmp2markdown.git
synced 2026-05-10 12:37:44 +08:00
feat: 添加三种文章内图片处理方式(原src、保存到本地、内嵌base64)
This commit is contained in:
77
util/util.go
Normal file
77
util/util.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func MergeMap(m1 map[string][]byte, m2 map[string][]byte) {
|
||||
for k, v := range m2 {
|
||||
m1[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func Zip(zipFileName string, files map[string][]byte) {
|
||||
f, err := os.Create(zipFileName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
zipWriter := zip.NewWriter(f)
|
||||
for name, file := range files {
|
||||
zw, err := zipWriter.Create(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(zw, bytes.NewReader(file)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
zipWriter.Close()
|
||||
}
|
||||
|
||||
func HttpDownloadZip(w http.ResponseWriter, files map[string][]byte) {
|
||||
zipWriter := zip.NewWriter(w)
|
||||
for name, file := range files {
|
||||
zw, err := zipWriter.Create(name)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(zw, bytes.NewReader(file)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
zipWriter.Close()
|
||||
}
|
||||
|
||||
func MD5(content []byte) string {
|
||||
hash := md5.New()
|
||||
hash.Write(content)
|
||||
md5Bytes := hash.Sum(nil)
|
||||
return hex.EncodeToString(md5Bytes)
|
||||
}
|
||||
|
||||
// 从图片src中解析出图片的扩展名
|
||||
func ParseImageExtFromSrc(src string) string {
|
||||
reg := regexp.MustCompile(`(wx_fmt=)([a-zA-Z]+)(&?)`)
|
||||
matches := reg.FindStringSubmatch(src)
|
||||
if len(matches) < 3 {
|
||||
return ""
|
||||
}
|
||||
return matches[2]
|
||||
}
|
||||
|
||||
// 判断路径是否存在
|
||||
func PathIsExists(path string) (os.FileInfo, bool) {
|
||||
f, err := os.Stat(path)
|
||||
return f, err == nil || os.IsExist(err)
|
||||
}
|
||||
Reference in New Issue
Block a user