Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileinfo: only get mimeinfo and generated from regular files #1402

Merged
merged 7 commits into from
Dec 25, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions base/fileinfo/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func NewFileInfoType(ftyp Known) *FileInfo {
// but file info will be updated based on the filename even if
// the file does not exist.
func (fi *FileInfo) InitFile(fname string) error {
fi.Cat = UnknownCategory
fi.Known = Unknown
fi.Generated = false
fi.Kind = ""
var errs []error
path, err := filepath.Abs(fname)
if err == nil {
Expand All @@ -111,7 +115,6 @@ func (fi *FileInfo) InitFile(fname string) error {
fi.Path = fname
}
_, fi.Name = filepath.Split(path)
fi.SetMimeInfo()
info, err := os.Stat(fi.Path)
if err != nil {
errs = append(errs, err)
Expand All @@ -126,10 +129,13 @@ func (fi *FileInfo) InitFile(fname string) error {
// but file info will be updated based on the filename even if
// the file does not exist.
func (fi *FileInfo) InitFileFS(fsys fs.FS, fname string) error {
fi.Cat = UnknownCategory
fi.Known = Unknown
fi.Generated = false
fi.Kind = ""
var errs []error
fi.Path = fname
_, fi.Name = path.Split(fname)
fi.SetMimeInfo()
info, err := fs.Stat(fsys, fi.Path)
if err != nil {
errs = append(errs, err)
Expand All @@ -145,10 +151,7 @@ func (fi *FileInfo) SetMimeInfo() error {
if fi.Path == "" || fi.Path == "." || fi.IsDir() {
return nil
}
fi.Cat = UnknownCategory
fi.Known = Unknown
fi.Generated = IsGeneratedFile(fi.Path)
fi.Kind = ""
mtyp, _, err := MimeFromFile(fi.Path)
if err != nil {
return err
Expand Down Expand Up @@ -177,6 +180,9 @@ func (fi *FileInfo) SetFileInfo(info fs.FileInfo) {
fi.Cat = Folder
fi.Known = AnyFolder
} else {
if fi.Mode.IsRegular() {
fi.SetMimeInfo()
}
if fi.Cat == UnknownCategory {
if fi.IsExec() {
fi.Cat = Exe
Expand Down
Loading