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
- Variables
- func CopyFile(dst, src string, perm os.FileMode) error
- func FileNames(d os.File, names *[]string) (err error)
- func IsMatch(targ, typ Known) bool
- func IsMatchList(targs []Known, typ Known) bool
- func MergeAvailMimes()
- func MimeFromFile(fname string) (mtype, ext string, err error)
- func MimeNoChar(mime string) string
- func MimeString(kn Known) string
- func MimeSub(mime string) string
- func MimeTop(mime string) string
- type Cat
- func (i Cat) Desc() string
- func (i Cat) Int64() int64
- func (i Cat) IsValid() bool
- func (i Cat) MarshalText() ([]byte, error)
- func (i *Cat) SetInt64(in int64)
- func (i *Cat) SetString(s string) error
- func (i Cat) String() string
- func (i *Cat) UnmarshalText(text []byte) error
- func (i Cat) Values() []enums.Enum
- type FileInfo
- func (fi *FileInfo) Delete() error
- func (fi *FileInfo) Duplicate() (string, error)
- func (fi *FileInfo) FileNames(names *[]string) (err error)
- func (fi *FileInfo) FindIcon() (icons.Icon, bool)
- func (fi *FileInfo) InitFile(fname string) error
- func (fi *FileInfo) IsDir() bool
- func (fi *FileInfo) IsExec() bool
- func (fi *FileInfo) IsHidden() bool
- func (fi *FileInfo) IsSymlink() bool
- func (fi *FileInfo) Rename(path string) (newpath string, err error)
- func (fi *FileInfo) RenamePath(path string) (newpath string, err error)
- func (fi *FileInfo) Stat() error
- type FileSize
- type FileTime
- func (ft *FileTime) FromInt(val int64)
- func (ft FileTime) Int() int64
- func (ft FileTime) MarshalBinary() ([]byte, error)
- func (ft FileTime) MarshalJSON() ([]byte, error)
- func (ft FileTime) MarshalText() ([]byte, error)
- func (ft FileTime) String() string
- func (ft *FileTime) UnmarshalBinary(data []byte) error
- func (ft *FileTime) UnmarshalJSON(data []byte) error
- func (ft *FileTime) UnmarshalText(data []byte) error
- type Known
- func (kn Known) Cat() Cat
- func (i Known) Desc() string
- func (i Known) Int64() int64
- func (i Known) IsValid() bool
- func (i Known) MarshalText() ([]byte, error)
- func (i *Known) SetInt64(in int64)
- func (i *Known) SetString(s string) error
- func (i Known) String() string
- func (i *Known) UnmarshalText(text []byte) error
- func (i Known) Values() []enums.Enum
- type MimeType
Constants ¶
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
const ( // Version is the version of this package being used Version = "v0.1.13" // GitCommit is the commit just before the latest version commit GitCommit = "9590feb" // VersionDate is the date-time of the latest version commit in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04') VersionDate = "2023-12-13 00:07" )
Variables ¶
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
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
var ExtMimeMap = map[string]string{}
ExtMimeMap is the map from extension to mime type, built from AvailMimes
var KnownMimes map[Known]MimeType
KnownMimes maps from the known type into the MimeType info for each known file type -- the known MimeType may be just one of multiple that correspond to the known type -- it should be first in list and have extensions defined
var StdMimes = []MimeType{}/* 284 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!
Functions ¶
func CopyFile ¶
CopyFile copies the contents from src to dst atomically. If dst does not exist, CopyFile creates it with permissions perm. If the copy fails, CopyFile aborts and dst is preserved.
func FileNames ¶
FileNames recursively adds fullpath filenames within the starting directory to the "names" slice. Directory names within the starting directory are not added.
func IsMatch ¶
IsMatch returns true if given file type matches target type, which could be any of the Any options
func IsMatchList ¶
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 KnownMimes map of known file types onto their full mime type entry
func MimeFromFile ¶
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 ¶
MimeNoChar returns the mime string without any charset encoding information, or anything else after a ;
func MimeString ¶
MimeString gives the string representation of the canonical mime type associated with given known mime type.
Types ¶
type Cat ¶
type Cat int32 //enums:enum
fi.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 ( // UnknownCat is an unknown file category UnknownCat 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 )
const CatN Cat = 16
CatN is the highest valid value for type Cat, plus one.
func CatFromMime ¶
fi.CatFromMime returns the file category based on the mime type -- not all Cats can be inferred from file types
func (Cat) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Cat) SetString ¶
SetString sets the Cat value from its string representation, and returns an error if the string is invalid.
func (*Cat) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type FileInfo ¶
type FileInfo struct { // icon for file Ic icons.Icon `tableview:"no-header"` // name of the file, without any path Name string `width:"40"` // size of the file in bytes Size FileSize // type of file / directory -- shorter, more user-friendly version of mime type, based on category Kind string `width:"20" max-width:"20"` // full official mime type of the contents Mime string `tableview:"-"` // functional category of the file, based on mime data etc Cat Cat `tableview:"-"` // known file type Known Known `tableview:"-"` // file mode bits Mode os.FileMode // time that contents (only) were last modified ModTime FileTime // version control system status, when enabled Vcs vci.FileStatus `tableview:"-"` // full path to file, including name -- for file functions Path string `tableview:"-"` }
FileInfo represents the information about a given file / directory, including icon, mimetype, etc
func NewFileInfo ¶
func (*FileInfo) Delete ¶
Delete deletes the file or if a directory the directory and all files and subdirectories
func (*FileInfo) Duplicate ¶
Duplicate creates a copy of given file -- only works for regular files, not directories.
func (*FileInfo) FileNames ¶
FileNames returns a slice of file names from the starting directory and its subdirectories
func (*FileInfo) FindIcon ¶
FindIcon uses file info to find an appropriate icon for this file -- uses Kind string first to find a correspondingly-named icon, and then tries the extension. Returns true on success.
func (*FileInfo) InitFile ¶
InitFile initializes a FileInfo based on a filename -- directly returns filepath.Abs or os.Stat error on the given file. filename can be anything that works given current directory -- Path will contain the full filepath.Abs path, and Name will be just the filename.
func (*FileInfo) IsHidden ¶
IsHidden returns true if file name starts with . or _ which are typically hidden
func (*FileInfo) Rename ¶
Rename renames (moves) this file to given new path name Updates the FileInfo setting to the new name, although it might be out of scope if it moved into a new path
func (*FileInfo) RenamePath ¶
RenamePath returns the proposed path or the new full path. Does not actually do the renaming -- see Rename method.
type FileSize ¶
type FileTime ¶
FileTime provides a default String format for file modification times, and other useful methods -- will plug into Value with date / time editor.
func (FileTime) MarshalBinary ¶
func (FileTime) MarshalJSON ¶
func (FileTime) MarshalText ¶
func (*FileTime) UnmarshalBinary ¶
func (*FileTime) UnmarshalJSON ¶
func (*FileTime) UnmarshalText ¶
type Known ¶ added in v0.1.7
type Known int32 //enums:enum
fi.Known is an enumerated list of known file types, for which appropriate actions can be taken etc.
const ( // Unknown = a non-known file type Unknown Known = iota // Any is used when selecting a file type, if any type is OK (including Unknown) // see also AnyKnown and the Any options for each category Any // AnyKnown is used when selecting a file type, if any Known // file type is OK (excludes Unknown) -- see Any and Any options for each category AnyKnown // 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 Bmp Heic Heif // 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 )
These are the known file types, organized by category
const KnownN Known = 125
KnownN is the highest valid value for type Known, plus one.
func ExtKnown ¶ added in v0.1.7
ExtKnown returns the known type for given file extension, or Unknown if not found or not a known file type
func KnownByName ¶ added in v0.1.7
KnownByName looks up known file type by caps or lowercase name
func KnownFromFile ¶ added in v0.1.7
KnownFromFile returns the known type for given file, or Unknown if not found or not a known file type
func KnownValues ¶ added in v0.1.7
func KnownValues() []Known
KnownValues returns all possible values for the type Known.
func MimeKnown ¶ added in v0.1.7
MimeKnown returns the known type for given mime key, or Unknown if not found or not a known file type
func (Known) IsValid ¶ added in v0.1.7
IsValid returns whether the value is a valid option for type Known.
func (Known) MarshalText ¶ added in v0.1.7
MarshalText implements the encoding.TextMarshaler interface.
func (*Known) SetString ¶ added in v0.1.7
SetString sets the Known value from its string representation, and returns an error if the string is invalid.
func (*Known) UnmarshalText ¶ added in v0.1.7
UnmarshalText implements the encoding.TextUnmarshaler interface.
type MimeType ¶
type MimeType struct { // mimetype string: type/subtype Mime string // file extensions associated with this file type Exts []string // category of file Cat Cat // if known, the name of the known file type, else NoSupporUnknown Sup Known }
MimeType contains all the information associated with a given mime type