处理html中的OSS数据:如果是用于预览的内容,则把img等的链接的相对路径转成绝对路径,否则反之 @param htmlstr html字符串 @param forPreview 是否是供浏览的页面需求 @return str 处理后返回的字符串
(htmlStr string, forPreview bool)
| 148 | //@param forPreview 是否是供浏览的页面需求 |
| 149 | //@return str 处理后返回的字符串 |
| 150 | func (o *Oss) HandleContent(htmlStr string, forPreview bool) (str string) { |
| 151 | doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr)) |
| 152 | if err != nil { |
| 153 | beego.Error(err.Error()) |
| 154 | return htmlStr |
| 155 | } |
| 156 | doc.Find("img").Each(func(i int, s *goquery.Selection) { |
| 157 | // For each item found, get the band and title |
| 158 | if src, exist := s.Attr("src"); exist { |
| 159 | //预览 |
| 160 | if forPreview { |
| 161 | //不存在http开头的图片链接,则更新为绝对链接 |
| 162 | if !(strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://")) { |
| 163 | s.SetAttr("src", o.Domain+"/"+strings.TrimLeft(src, "./")) |
| 164 | } |
| 165 | } else { |
| 166 | s.SetAttr("src", strings.TrimPrefix(src, o.Domain)) |
| 167 | } |
| 168 | } |
| 169 | }) |
| 170 | str, _ = doc.Find("body").Html() |
| 171 | return |
| 172 | } |
| 173 | |
| 174 | //从HTML中提取图片文件,并删除 |
| 175 | func (o *Oss) DelByHtmlPics(htmlStr string) { |