代码拉取完成,页面将自动刷新
package file
import (
"fmt"
"regexp"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo/param"
)
const (
fileCSPattern = `(?:[^"'#\(\)]+)`
fileStartPattern = `["'\(]`
fileEndPattern = `["'\)]`
)
var (
//defaultFilePattern = `["'\(]([^"'#\(\)]+)#FileID-(\d+)["'\)]`
filePattern = fileStartPattern + `(` + fileCSPattern + `\.(?:[\w]+)` + fileCSPattern + `?)` + fileEndPattern
fileRGX = regexp.MustCompile(filePattern)
)
// ReplaceEmbeddedRes 替换正文中的资源网址
func ReplaceEmbeddedRes(v string, reses map[uint64]string) (r string) {
for fid, rurl := range reses {
re := regexp.MustCompile(`(` + fileStartPattern + `)` + fileCSPattern + `#FileID-` + fmt.Sprint(fid) + `(` + fileEndPattern + `)`)
v = re.ReplaceAllString(v, `${1}`+rurl+`${2}`)
}
return v
}
// ReplaceRelatedRes 替换字段中的资源网址
func ReplaceRelatedRes(v string, reses map[uint64]string, seperator ...string) (r string) {
var fileList []string
var sep string
if len(seperator) > 0 && len(seperator[0]) > 0 {
sep = seperator[0]
fileList = strings.Split(v, sep)
} else {
fileList = append(fileList, v)
}
replaced := map[int]struct{}{}
for fid, rurl := range reses {
suffix := `#FileID-` + fmt.Sprint(fid)
for key, file := range fileList {
if _, ok := replaced[key]; ok {
continue
}
if strings.HasSuffix(file, suffix) {
fileList[key] = rurl
replaced[key] = struct{}{}
}
}
}
v = strings.Join(fileList, sep)
return v
}
// EmbeddedRes 获取正文中的资源
func EmbeddedRes(v string, fn func(string, int64)) [][]string {
if len(v) == 0 {
return nil
}
list := fileRGX.FindAllStringSubmatch(v, -1)
if fn == nil {
return list
}
for _, a := range list {
resource := a[1]
var fileID int64
if len(a) > 2 {
fileID = param.AsInt64(a[2])
}
fn(resource, fileID)
}
return list
}
// RelatedRes 获取字段中关联的资源
func RelatedRes(v string, fn func(string, int64), seperator ...string) {
if len(v) == 0 {
return
}
var fileList []string
if len(seperator) > 0 && len(seperator[0]) > 0 {
fileList = strings.Split(v, seperator[0])
} else {
fileList = append(fileList, v)
}
for _, file := range fileList {
file = strings.TrimSpace(file)
if len(file) == 0 {
fn(file, 0)
continue
}
p := strings.LastIndex(file, `#FileID-`)
if p < 0 {
fn(file, 0)
continue
}
var fid int64
fileID := file[p+8:]
if len(fileID) > 0 {
fid = com.Int64(fileID)
}
file = file[0:p]
fn(file, fid)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。