func saveAttachment(att message.AttachmentType) error {
	defer func() {
		// Close stream if it's closeable
		_, ok := att.Reader.(io.Closer)
		if ok {
			_ = att.Reader.(io.Closer).Close()
		}
	}()

	_, ok := os.Stat(att.FileName)
	if ok == nil {
		fmt.Printf("cannot write to %s: file exists\n", att.FileName)
		return errors.New("cannot write to file")
	}

	f, err := os.Create(att.FileName)
	if err != nil {
		fmt.Printf("cannot open file %s: %s\n", att.FileName, err)
		return err
	}

	n, err := io.Copy(f, att.Reader)
	if err != nil || n != int64(att.Size) {
		fmt.Printf("error while writing file %s: %s (%d/%d bytes)\n", att.FileName, err, n, att.Size)
		return err
	}

	_ = f.Close()
	fmt.Printf("saved file: %s\n", att.FileName)

	return nil
}
// saveAttachment will save the given attachment to disk
saveAttachment will save the given attachment to disk
// saveAttachment will save the given attachment to disk
func saveAttachment(att message.AttachmentType) error {
	defer func() {
		_, ok := att.Reader.(io.Closer)
		if ok {
			_ = att.Reader.(io.Closer).Close()
		}
	}()
	_, ok := os.Stat(att.FileName)
	if ok == nil {
		fmt.Printf("cannot write to %s: file exists\n", att.FileName)
		return errors.New("cannot write to file")
	}
	f, err := os.Create(att.FileName)
	if err != nil {
		fmt.Printf("cannot open file %s: %s\n", att.FileName, err)
		return err
	}
	n, err := io.Copy(f, att.Reader)
	if err != nil || n != int64(att.Size) {
		fmt.Printf("error while writing file %s: %s (%d/%d bytes)\n", att.FileName, err, n, att.Size)
		return err
	}
	_ = f.Close()
	fmt.Printf("saved file: %s\n", att.FileName)
	return nil
}
func saveAttachment ( att message . AttachmentType ) error { defer func ( ) { _ , ok := att . Reader . ( io . Closer ) if ok { _ = att . Reader . ( io . Closer ) . Close ( ) } } ( ) _ , ok := os . Stat ( att . FileName ) if ok == nil { fmt . Printf ( " \n " , att . FileName ) return errors . New ( " " ) } f , err := os . Create ( att . FileName ) if err != nil { fmt . Printf ( " \n " , att . FileName , err ) return err } n , err := io . Copy ( f , att . Reader ) if err != nil || n != int64 ( att . Size ) { fmt . Printf ( " \n " , att . FileName , err , n , att . Size ) return err } _ = f . Close ( ) fmt . Printf ( " \n " , att . FileName ) return nil }
saveAttachment will save the given attachment to disk
saveAttachment will save the given attachment to disk
// Close stream if it's closeable