Exemple #1
0
func (m *MessageAsText) Read(p []byte) (int, error) {
	var err error
	if m.rs == nil {
		m.rs = &readState{}
		f, err := os.Open(m.path)
		if err != nil {
			m.rs = nil
			return 0, err
		}
		defer f.Close()
		mtree, err := mime.GetMimeTree(f, 10)
		if err != nil {
			m.rs = nil
			return 0, err
		}

		m.rs.buffers = traverse(mtree, true)

		readers := make([]io.Reader, len(m.rs.buffers))
		for i, b := range m.rs.buffers {
			newb := bytes.Replace(b.Bytes(), []byte("\r\n"), []byte("\n"), -1)
			readers[i] = bytes.NewBuffer(newb)
		}
		m.rs.r = io.MultiReader(readers...)
	}
	ret, err := m.rs.r.Read(p)
	if err != nil {
		m.rs = nil
	}
	return ret, err
}
Exemple #2
0
func (m *MessageAsMimeTree) Read(p []byte) (int, error) {
	if m.rs == nil {
		m.rs = &readState{}
		var printM func(w io.Writer, depth int, m *mime.MimePart)
		printM = func(w io.Writer, depth int, m *mime.MimePart) {
			fmt.Fprintf(w, "%s%s\n", strings.Repeat("-", depth), mime.MimeTypeTxt(m.MimeType))
			if m.Child != nil {
				printM(w, depth+1, m.Child)
			}
			for cur := m.Next; cur != nil; cur = cur.Next {
				printM(w, depth, cur)
			}
		}
		f, err := os.Open(m.path)
		if err != nil {
			m.rs = nil
			return 0, err
		}
		defer f.Close()
		mtree, err := mime.GetMimeTree(f, 10)
		if err != nil {
			m.rs = nil
			return 0, err
		}
		buf := &bytes.Buffer{}
		printM(buf, 0, mtree)
		m.rs.r = buf

	}
	ret, err := m.rs.r.Read(p)
	if err != nil {
		m.rs = nil
	}
	return ret, err
}