mimedata

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package mimedata defines MIME data support used in clipboard and drag-and-drop functions in the Cogent Core GUI. mimedata.Data struct contains format and []byte data, and multiple representations of the same data are encoded in mimedata.Mimes which is just a []mimedata.Data slice -- it can be encoded / decoded from mime multipart.

see fi package for known mimetypes

Index

Constants

View Source
const (
	MIMEVersion1            = "MIME-Version: 1.0"
	ContentType             = "Content-Type"
	ContentTransferEncoding = "Content-Transfer-Encoding"

	TextPlain = "text/plain"
	DataJson  = "application/json"
	DataXml   = "application/xml"
)

Variables

View Source
var (
	MIMEVersion1B            = ([]byte)(MIMEVersion1)
	ContentTypeB             = ([]byte)(ContentType)
	ContentTransferEncodingB = ([]byte)(ContentTransferEncoding)
)

Functions

func IsMultipart

func IsMultipart(str []byte) (isMulti bool, mediaType, boundary string, body []byte)

IsMultipart examines data bytes to see if it has a MIME-Version: 1.0 ContentType: multipart/* header -- returns the actual multipart media type, body of the data string after the header (assumed to be a single \n terminated line at start of string, and the boundary separating multipart elements (all from mime.ParseMediaType) -- mediaType is the mediaType if it is another MIME type -- can check that for non-empty string

func IsText

func IsText(typ string) bool

IsText returns true if type is any of the text/ types (literally looks for that at start of Type) or is another known text type (e.g., AppJSON, XML)

Types

type Data

type Data struct {
	// MIME Type string representing the data, e.g., text/plain, text/html, text/xml, text/uri-list, image/jpg, png etc
	Type string

	// Data for the item
	Data []byte
}

Data represents one element of MIME data as a type string and byte slice

func NewTextData

func NewTextData(text string) *Data

NewTextData returns a Data representation of the string -- good idea to always have a text/plain representation of everything on clipboard / drag-n-drop

func NewTextDataBytes

func NewTextDataBytes(text []byte) *Data

NewTextDataBytes returns a Data representation of the bytes string

type Mimes

type Mimes []*Data

Mimes is a slice of mime data, potentially encoding the same data in different formats -- this is used for all oswin API's for maximum flexibility

func FromMultipart

func FromMultipart(body []byte, boundary string) Mimes

FromMultipart parses a MIME multipart representation of multiple data elements into corresponding mime data components

func NewMime

func NewMime(typ string, data []byte) Mimes

NewMime returns a Mimes representation of one element

func NewMimes

func NewMimes(ln, cp int) Mimes

NewMimes returns a new Mimes slice of given length and capacity

func NewText

func NewText(text string) Mimes

NewText returns a Mimes representation of the string as a single text/plain Data

func NewTextBytes

func NewTextBytes(text []byte) Mimes

NewTextBytes returns a Mimes representation of the bytes string as a single text/plain Data

func NewTextPlus

func NewTextPlus(text, typ string, data []byte) Mimes

NewTextPlus returns a Mimes representation of an item as a text string plus a more specific type

func (Mimes) HasType

func (mi Mimes) HasType(typ string) bool

HasType returns true if Mimes has given type of data available

func (Mimes) Text

func (mi Mimes) Text(typ string) string

Text extracts all the text elements of given type as a string

func (Mimes) ToMultipart

func (mi Mimes) ToMultipart() []byte

ToMultipart produces a MIME multipart representation of multiple data elements present in the stream -- this should be used in goosi.Clipboard whenever there are multiple elements to be pasted, because windows doesn't support multiple clip elements, and linux isn't very convenient either

func (Mimes) TypeData

func (mi Mimes) TypeData(typ string) []byte

TypeData returns data associated with given MIME type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL