Esempio n. 1
0
func encode(in io.Reader, out io.Writer, encoding string, mediatype string) (err error) {
	defer func() {
		if e := recover(); e != nil {
			var ok bool
			err, ok = e.(error)
			if !ok {
				err = fmt.Errorf("%v", e)
			}
			return
		}
	}()
	b, err := ioutil.ReadAll(in)
	if err != nil {
		return
	}

	du := dataurl.New(b, mediatype)
	du.Encoding = encoding

	_, err = du.WriteTo(out)
	if err != nil {
		return
	}
	return
}
Esempio n. 2
0
func inlineImages(body []byte) ([]byte, error) {
	reader := bytes.NewReader(body)
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		return nil, err
	}
	wg := &sync.WaitGroup{}
	var err2 error
	doc.Find("img").Each(func(i int, sel *goquery.Selection) {
		wg.Add(1)
		go func() {
			link := strings.TrimSpace(sel.AttrOr("src", ""))
			img, err := httpGetOrCache(link)
			if err != nil {
				err2 = err
			}
			typ := http.DetectContentType(img)
			if typ == "application/octet-stream" {
				if typ2 := mime.TypeByExtension(filepath.Ext(link)); len(typ2) > 0 {
					typ = typ2
				}
			}
			dataURL := dataurl.New(img, typ)
			sel.SetAttr("src", dataURL.String())
			wg.Done()
		}()
	})
	wg.Wait()
	if err != nil {
		return nil, err
	}
	out, err := doc.Html()
	return []byte(out), err
}