fileinfo

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: BSD-3-Clause Imports: 18 Imported by: 7

Documentation

Overview

Package fileinfo manages file information and categorizes file types; it is the single, consolidated place where file info, mimetypes, and filetypes are managed in Cogent Core.

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 this Go package: github.com/h2non/filetype

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 AvailableMimes map[string]MimeType

AvailableMimes 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 Icons = map[Known]icons.Icon{}/* 127 elements not displayed */
View Source
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

View Source
var StandardMimes = []MimeType{}/* 287 elements not displayed */

StandardMimes 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

func CopyFile(dst, src string, perm os.FileMode) error

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

func Filenames(d os.File, names *[]string) (err error)

Filenames recursively adds fullpath filenames within the starting directory to the "names" slice. Directory names within the starting directory are not added.

func IsMatch

func IsMatch(targ, typ Known) bool

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

func IsMatchList

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

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

func MergeAvailableMimes

func MergeAvailableMimes()

MergeAvailableMimes 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

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(kn Known) string

MimeString gives the string representation of the canonical mime type associated with given known 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 Categories

type Categories int32 //enums:enum

Categories 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 (
	// UnknownCategory is an unknown file category
	UnknownCategory Categories = 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 CategoriesN Categories = 16

CategoriesN is the highest valid value for type Categories, plus one.

func CategoriesValues

func CategoriesValues() []Categories

CategoriesValues returns all possible values for the type Categories.

func CategoryFromMime

func CategoryFromMime(mime string) Categories

CategoryFromMime returns the file category based on the mime type; not all Categories can be inferred from file types

func (Categories) Desc

func (i Categories) Desc() string

Desc returns the description of the Categories value.

func (Categories) Int64

func (i Categories) Int64() int64

Int64 returns the Categories value as an int64.

func (Categories) MarshalText

func (i Categories) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Categories) SetInt64

func (i *Categories) SetInt64(in int64)

SetInt64 sets the Categories value from an int64.

func (*Categories) SetString

func (i *Categories) SetString(s string) error

SetString sets the Categories value from its string representation, and returns an error if the string is invalid.

func (Categories) String

func (i Categories) String() string

String returns the string representation of this Categories value.

func (*Categories) UnmarshalText

func (i *Categories) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Categories) Values

func (i Categories) Values() []enums.Enum

Values returns all possible values for the type Categories.

type FileInfo

type FileInfo struct {

	// icon for file
	Ic icons.Icon `table:"no-header"`

	// name of the file, without any path
	Name string `width:"40"`

	// size of the file
	Size datasize.Size

	// 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 `table:"-"`

	// functional category of the file, based on mime data etc
	Cat Categories `table:"-"`

	// known file type
	Known Known `table:"-"`

	// file mode bits
	Mode fs.FileMode `table:"-"`

	// time that contents (only) were last modified
	ModTime time.Time `label:"Last modified"`

	// version control system status, when enabled
	VCS vcs.FileStatus `table:"-"`

	// full path to file, including name; for file functions
	Path string `table:"-"`
}

FileInfo represents the information about a given file / directory, including icon, mimetype, etc

func NewFileInfo

func NewFileInfo(fname string) (*FileInfo, error)

NewFileInfo returns a new FileInfo for given file.

func NewFileInfoType added in v0.3.0

func NewFileInfoType(ftyp Known) *FileInfo

NewFileInfoType returns a new FileInfo representing the given file type.

func (*FileInfo) Delete

func (fi *FileInfo) Delete() error

Delete moves the file to the trash / recycling bin. On mobile and web, it deletes it directly.

func (*FileInfo) Duplicate

func (fi *FileInfo) Duplicate() (string, error)

Duplicate creates a copy of given file -- only works for regular files, not directories.

func (*FileInfo) Filenames

func (fi *FileInfo) Filenames(names *[]string) (err error)

Filenames returns a slice of file names from the starting directory and its subdirectories

func (*FileInfo) FindIcon

func (fi *FileInfo) FindIcon() (icons.Icon, bool)

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

func (fi *FileInfo) InitFile(fname string) error

InitFile initializes a FileInfo for os file based on a filename, which is updated to full path using filepath.Abs. Returns error from filepath.Abs and / or fs.Stat error on the given file, but file info will be updated based on the filename even if the file does not exist.

func (*FileInfo) InitFileFS added in v0.3.3

func (fi *FileInfo) InitFileFS(fsys fs.FS, fname string) error

InitFileFS initializes a FileInfo based on filename in given fs.FS. Returns error from fs.Stat error on the given file, but file info will be updated based on the filename even if the file does not exist.

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

IsDir returns true if file is a directory (folder)

func (*FileInfo) IsExec

func (fi *FileInfo) IsExec() bool

IsExec returns true if file is an executable file

func (*FileInfo) IsHidden

func (fi *FileInfo) IsHidden() bool

IsHidden returns true if file name starts with . or _ which are typically hidden

func (fi *FileInfo) IsSymlink() bool

IsSymLink returns true if file is a symbolic link

func (*FileInfo) Rename

func (fi *FileInfo) Rename(path string) (newpath string, err error)

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

func (fi *FileInfo) RenamePath(path string) (newpath string, err error)

RenamePath returns the proposed path or the new full path. Does not actually do the renaming -- see Rename method.

func (*FileInfo) SetFileInfo added in v0.3.3

func (fi *FileInfo) SetFileInfo(info fs.FileInfo)

SetFileInfo updates from given fs.FileInfo

func (*FileInfo) SetMimeInfo added in v0.3.3

func (fi *FileInfo) SetMimeInfo() error

SetMimeInfo parses the file name to set mime type, which then drives Kind and Icon.

func (*FileInfo) SetType added in v0.3.0

func (fi *FileInfo) SetType(ftyp Known)

SetType sets file type information for given Known file type

type Known

type Known int32 //enums:enum

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
	Cosh
	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
	Yaml
	Toml
	// special support for data fs
	Number
	String
	Tensor
	Table

	// 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
	Obj

	// 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 = 131

KnownN is the highest valid value for type Known, plus one.

func ExtKnown

func ExtKnown(ext string) Known

ExtKnown returns the known type for given file extension, or Unknown if not found or not a known file type

func KnownByName

func KnownByName(name string) (Known, error)

KnownByName looks up known file type by caps or lowercase name

func KnownFromFile

func KnownFromFile(fname string) Known

KnownFromFile returns the known type for given file, or Unknown if not found or not a known file type

func KnownValues

func KnownValues() []Known

KnownValues returns all possible values for the type Known.

func MimeKnown

func MimeKnown(mime string) Known

MimeKnown returns the known type for given mime key, or Unknown if not found or not a known file type

func (Known) Cat

func (kn Known) Cat() Categories

Cat returns the Cat category for given known file type

func (Known) Desc

func (i Known) Desc() string

Desc returns the description of the Known value.

func (Known) Int64

func (i Known) Int64() int64

Int64 returns the Known value as an int64.

func (Known) MarshalText

func (i Known) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Known) SetInt64

func (i *Known) SetInt64(in int64)

SetInt64 sets the Known value from an int64.

func (*Known) SetString

func (i *Known) SetString(s string) error

SetString sets the Known value from its string representation, and returns an error if the string is invalid.

func (Known) String

func (i Known) String() string

String returns the string representation of this Known value.

func (*Known) UnmarshalText

func (i *Known) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Known) Values

func (i Known) Values() []enums.Enum

Values returns all possible values for the type Known.

type MimeType

type MimeType struct {

	// mimetype string: type/subtype
	Mime string

	// file extensions associated with this file type
	Exts []string

	// category of file
	Cat Categories

	// if known, the name of the known file type, else NoSupporUnknown
	Known Known
}

MimeType contains all the information associated with a given mime type

func MimeFromKnown added in v0.3.0

func MimeFromKnown(ftyp Known) MimeType

MimeFromKnown returns MimeType info for given known file type.

Directories

Path Synopsis
Package mimedata defines MIME data support used in clipboard and drag-and-drop functions in the Cogent Core GUI.
Package mimedata defines MIME data support used in clipboard and drag-and-drop functions in the Cogent Core GUI.

Jump to

Keyboard shortcuts

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