Skip to content

Commit

Permalink
finally got the logic right about fileinfo and fake names (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Jan 15, 2025
1 parent 63d4dcb commit df480d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
10 changes: 5 additions & 5 deletions base/fileinfo/enumgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions base/fileinfo/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (fi *FileInfo) InitFile(fname string) error {
info, err := os.Stat(fi.Path)
if err != nil {
errs = append(errs, err)
fi.MimeFromFilename()
} else {
fi.SetFileInfo(info)
}
Expand All @@ -139,26 +140,44 @@ func (fi *FileInfo) InitFileFS(fsys fs.FS, fname string) error {
info, err := fs.Stat(fsys, fi.Path)
if err != nil {
errs = append(errs, err)
fi.MimeFromFilename()
} else {
fi.SetFileInfo(info)
}
return errors.Join(errs...)
}

// SetMimeInfo parses the file name to set mime type,
// which then drives Kind and Icon.
func (fi *FileInfo) SetMimeInfo() error {
// MimeFromFilename sets the mime data based only on the filename
// without attempting to open the file.
func (fi *FileInfo) MimeFromFilename() error {
ext := strings.ToLower(filepath.Ext(fi.Path))
if mtype, has := ExtMimeMap[ext]; has { // only use our filename ext map
fi.SetMimeFromType(mtype)
return nil
}
return errors.New("FileInfo MimeFromFilename: Filename extension not known: " + ext)
}

// MimeFromFile sets the mime data for a valid file (i.e., os.Stat works).
// Use MimeFromFilename to only examine the filename.
func (fi *FileInfo) MimeFromFile() error {
if fi.Path == "" || fi.Path == "." || fi.IsDir() {
return nil
}
fi.Generated = IsGeneratedFile(fi.Path)
mtyp, _, err := MimeFromFile(fi.Path)
mtype, _, err := MimeFromFile(fi.Path)
if err != nil {
return err
}
fi.Mime = mtyp
fi.Cat = CategoryFromMime(fi.Mime)
fi.Known = MimeKnown(fi.Mime)
fi.SetMimeFromType(mtype)
return nil
}

// SetMimeType sets file info fields from given mime type string.
func (fi *FileInfo) SetMimeFromType(mtype string) {
fi.Mime = mtype
fi.Cat = CategoryFromMime(mtype)
fi.Known = MimeKnown(mtype)
if fi.Cat != UnknownCategory {
fi.Kind = fi.Cat.String() + ": "
}
Expand All @@ -167,7 +186,6 @@ func (fi *FileInfo) SetMimeInfo() error {
} else {
fi.Kind += MimeSub(fi.Mime)
}
return nil
}

// SetFileInfo updates from given [fs.FileInfo]
Expand All @@ -181,7 +199,7 @@ func (fi *FileInfo) SetFileInfo(info fs.FileInfo) {
fi.Known = AnyFolder
} else {
if fi.Mode.IsRegular() {
fi.SetMimeInfo()
fi.MimeFromFile()
}
if fi.Cat == UnknownCategory {
if fi.IsExec() {
Expand Down
2 changes: 2 additions & 0 deletions base/fileinfo/known.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const (
Fortran
FSharp
Go
Goal
Haskell
Java
JavaScript
Expand All @@ -192,6 +193,7 @@ const (
Ruby
Rust
Scala
SQL
Tcl

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

0 comments on commit df480d5

Please sign in to comment.