filecat

package
v0.9.14 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2020 License: BSD-3-Clause Imports: 11 Imported by: 11

Documentation

Overview

Package filecat categorizes file types -- it is the single, consolidated place where mimetypes and filetypes etc are managed in GoGi / GoKi.

This whole space is a bit of a heterogenous mess -- most file types we care about are not registered on the official iana registry, which seems mainly to have paid registrations in application/ category, and doesn't have any of the main programming languages etc.

The official Go std library support depends on different platform libraries and mac doesn't have one, so it has very limited support

So we sucked it up and made a full list of all the major file types that we really care about and also provide a broader category-level organization that is useful for functionally organizing / operating on files.

As fallback, we are using both of the go packages developed by others: github.com/gabriel-vasile/mimetype github.com/h2non/filetype (which constrained our package name to not be either of those two)

Index

Constants

View Source
const (
	TextPlain = "text/plain"
	DataJson  = "application/json"
	DataXml   = "application/xml"
	DataCsv   = "text/csv"
)

These are the super high-frequency used mime types, to have very quick const level support

Variables

View Source
var AvailMimes map[string]MimeType

AvailMimes is the full list (as a map from mimetype) of available defined mime types built from StdMimes (compiled in) and then CustomMimes can override

View Source
var CustomMimes []MimeType

CustomMimes can be set by other apps to contain custom mime types that go beyond what is in the standard ones, and can also redefine and override the standard one

View Source
var ExtMimeMap = map[string]string{}

ExtMimeMap is the map from extension to mime type, built from AvailMimes

View Source
var KiT_Cat = kit.Enums.AddEnum(CatN, kit.NotBitFlag, nil)
View Source
var KiT_Supported = kit.Enums.AddEnumAltLower(SupportedN, kit.NotBitFlag, nil, "")
View Source
var StdMimes = []MimeType{}/* 282 elements not displayed */

StdMimes is the full list of standard mime types compiled into our code various other maps etc are constructed from it. When there are multiple types associated with the same real type, pick one to be the canonical one and give it, and *only* it, the extensions!

View Source
var SupportedMimes map[Supported]MimeType

SupportedMimes maps from the support type into the MimeType info for each supported file type -- the supported MimeType may be just one of multiple that correspond to the supported type -- it should be first in list and have extensions defined

Functions

func IsMatch

func IsMatch(targ, typ Supported) bool

IsMatch returns true if given file type matches target type, which could be any of the Any options

func IsMatchList

func IsMatchList(targs []Supported, typ Supported) bool

IsMatchList returns true if given file type matches any of a list of targets if list is empty, then always returns true

func MergeAvailMimes

func MergeAvailMimes()

MergeAvailMimes merges the StdMimes and CustomMimes into AvailMimes if CustomMimes is updated, then this should be called -- initially it just has StdMimes. It also builds the ExtMimeMap to map from extension to mime type and SupportedMimes map of supported file types onto their full mime type entry

func MimeFromFile

func MimeFromFile(fname string) (mtype, ext string, err error)

MimeFromFile gets mime type from file, using Gabriel Vasile's mimetype package, mime.TypeByExtension, the chroma syntax highlighter, CustomExtMimeMap, and finally FileExtMimeMap. Use the mimetype package's extension mechanism to add further content-based matchers as needed, and set CustomExtMimeMap to your own map or call AddCustomExtMime for extension-based ones.

func MimeNoChar

func MimeNoChar(mime string) string

MimeNoChar returns the mime string without any charset encoding information, or anything else after a ;

func MimeString

func MimeString(sup Supported) string

MimeString gives the string representation of the canonical mime type associated with given supported mime type.

func MimeSub

func MimeSub(mime string) string

MimeSub returns the sub-level subtype category from mime type i.e., the thing after the / -- returns empty if no / also trims off any charset encoding stuff

func MimeTop

func MimeTop(mime string) string

MimeTop returns the top-level main type category from mime type i.e., the thing before the / -- returns empty if no /

Types

type Cat

type Cat int32

filecat.Cat is a functional category for files -- a broad functional categorization that can help decide what to do with the file.

It is computed in part from the mime type, but some types require other information.

No single categorization scheme is perfect, so any given use may require examination of the full mime type etc, but this provides a useful broad-scope categorization of file types.

const (
	// Unknown is an unknown file category
	Unknown Cat = iota

	// Folder is a folder / directory
	Folder

	// Archive is a collection of files, e.g., zip tar
	Archive

	// Backup is a backup file (# ~ etc)
	Backup

	// Code is a programming language file
	Code

	// Doc is an editable word processing file including latex, markdown, html, css, etc
	Doc

	// Sheet is a spreadsheet file (.xls etc)
	Sheet

	// Data is some kind of data format (csv, json, database, etc)
	Data

	// Text is some other kind of text file
	Text

	// Image is an image (jpeg, png, svg, etc) *including* PDF
	Image

	// Model is a 3D model
	Model

	// Audio is an audio file
	Audio

	// Video is a video file
	Video

	// Font is a font file
	Font

	// Exe is a binary executable file (scripts go in Code)
	Exe

	// Bin is some other type of binary (object files, libraries, etc)
	Bin

	CatN
)

func CatFromMime

func CatFromMime(mime string) Cat

filecat.CatFromMime returns the file category based on the mime type -- not all Cats can be inferred from file types

func SupportedCat

func SupportedCat(sup Supported) Cat

SupportedCat returns the Cat category for given supported file type

func (*Cat) FromString

func (i *Cat) FromString(s string) error

func (Cat) MarshalJSON

func (kf Cat) MarshalJSON() ([]byte, error)

func (Cat) MarshalText

func (ev Cat) MarshalText() ([]byte, error)

map keys require text marshaling:

func (Cat) String

func (i Cat) String() string

func (*Cat) UnmarshalJSON

func (kf *Cat) UnmarshalJSON(b []byte) error

func (*Cat) UnmarshalText

func (ev *Cat) UnmarshalText(b []byte) error

type MimeType

type MimeType struct {
	Mime string    `desc:"mimetype string: type/subtype"`
	Exts []string  `desc:"file extensions associated with this file type"`
	Cat  Cat       `desc:"category of file"`
	Sup  Supported `desc:"if supported, the name of the supported file type, else NoSupport"`
}

MimeType contains all the information associated with a given mime type

type Supported

type Supported int

filecat.Supported are file types that are specifically supported by GoGi and can be processed in one way or another, plus various others that we SHOULD be able to process at some point

const (
	// NoSupport = a non-supported file type
	NoSupport Supported = iota

	// Any is used when selecting a file type, if any type is OK (including NoSupport)
	// see also AnySupported and the Any options for each category
	Any

	// AnySupported is used when selecting a file type, if any Supported
	// file type is OK (excludes NoSupport) -- see Any and Any options for each category
	AnySupported

	// Folder is a folder / directory
	AnyFolder

	// Archive is a collection of files, e.g., zip tar
	AnyArchive
	Multipart
	Tar
	Zip
	GZip
	SevenZ
	Xz
	BZip
	Dmg
	Shar

	// Backup files
	AnyBackup
	Trash

	// Code is a programming language file
	AnyCode
	Ada
	Bash
	Csh
	C // includes C++
	CSharp
	D
	Diff
	Eiffel
	Erlang
	Forth
	Fortran
	FSharp
	Go
	Haskell
	Java
	JavaScript
	Lisp
	Lua
	Makefile
	Mathematica
	Matlab
	ObjC
	OCaml
	Pascal
	Perl
	Php
	Prolog
	Python
	R
	Ruby
	Rust
	Scala
	Tcl

	// Doc is an editable word processing file including latex, markdown, html, css, etc
	AnyDoc
	BibTeX
	TeX
	Texinfo
	Troff

	Html
	Css
	Markdown
	Rtf
	MSWord
	OpenText
	OpenPres
	MSPowerpoint

	EBook
	EPub

	// Sheet is a spreadsheet file (.xls etc)
	AnySheet
	MSExcel
	OpenSheet

	// Data is some kind of data format (csv, json, database, etc)
	AnyData
	Csv
	Json
	Xml
	Protobuf
	Ini
	Tsv
	Uri
	Color
	GoGi
	Yaml

	// Text is some other kind of text file
	AnyText
	PlainText // text/plain mimetype -- used for clipboard
	ICal
	VCal
	VCard

	// Image is an image (jpeg, png, svg, etc) *including* PDF
	AnyImage
	Pdf
	Postscript
	Gimp
	GraphVis
	Gif
	Jpeg
	Png
	Svg
	Tiff
	Pnm
	Pbm
	Pgm
	Ppm
	Xbm
	Xpm

	// Model is a 3D model
	AnyModel
	Vrml
	X3d

	// Audio is an audio file
	AnyAudio
	Aac
	Flac
	Mp3
	Ogg
	Midi
	Wav

	// Video is a video file
	AnyVideo
	Mpeg
	Mp4
	Mov
	Ogv
	Wmv
	Avi

	// Font is a font file
	AnyFont
	TrueType
	WebOpenFont

	// Exe is a binary executable file
	AnyExe

	// Bin is some other unrecognized binary type
	AnyBin

	SupportedN
)

These are the supported file types, organized by category

func ExtSupported

func ExtSupported(ext string) Supported

ExtSupported returns the supported type for given file extension, or NoSupport if not found or not a supported file type

func MimeSupported

func MimeSupported(mime string) Supported

MimeSupported returns the supported type for given mime key, or NoSupport if not found or not a supported file type

func SupportedByName

func SupportedByName(name string) (Supported, error)

SupportedByName looks up supported file type by caps or lowercase name

func SupportedFromFile

func SupportedFromFile(fname string) Supported

SupportedFromFile returns the supported type for given file, or NoSupport if not found or not a supported file type

func (*Supported) FromString

func (i *Supported) FromString(s string) error

func (Supported) MarshalJSON

func (kf Supported) MarshalJSON() ([]byte, error)

func (Supported) MarshalText

func (ev Supported) MarshalText() ([]byte, error)

map keys require text marshaling:

func (Supported) String

func (i Supported) String() string

func (*Supported) UnmarshalJSON

func (kf *Supported) UnmarshalJSON(b []byte) error

func (*Supported) UnmarshalText

func (ev *Supported) UnmarshalText(b []byte) error

Jump to

Keyboard shortcuts

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