From 35604a9bcf0632ab2da9ea27b5ca2ac9b2ca4b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Str=C3=B6mberg?= Date: Fri, 18 Oct 2024 09:17:53 -0400 Subject: [PATCH] programkind: quietly skip non-file files (#529) * programkind: quietly skip non-file files * add error handling --- pkg/programkind/programkind.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/programkind/programkind.go b/pkg/programkind/programkind.go index d2f9ae51..b9b41292 100644 --- a/pkg/programkind/programkind.go +++ b/pkg/programkind/programkind.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" "strings" @@ -93,6 +94,18 @@ func makeFileType(path string, ext string, mime string) *FileType { // File detects what kind of program this file might be. func File(path string) (*FileType, error) { + st, err := os.Stat(path) + if err != nil { + return nil, fmt.Errorf("stat: %w", err) + } + + if st.IsDir() { + return nil, nil + } + if st.Mode().Type() == fs.ModeIrregular { + return nil, nil + } + // first strategy: mimetype mtype, err := mimetype.DetectFile(path) if err == nil {