Files
why/filetypes/filetypes.go
2025-06-04 10:40:21 -04:00

136 lines
3.5 KiB
Go

package filetypes
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
tk "modernc.org/tk9.0"
)
type FileDescription struct {
TkTypeName string
MacExtensions []string
IconFile string
MIMETypes []string
TypeName string
OSTypes []string
ItemContentTypes string
}
var Valid = []FileDescription{
{
TkTypeName: "BMP",
MacExtensions: []string{"bmp", "BMP"},
IconFile: "bmp.icns",
MIMETypes: []string{"image/bmp"},
TypeName: "Windows Bitmap Image",
OSTypes: []string{"BMP ", "BMPf"},
ItemContentTypes: "com.microsoft.bmp",
},
{
TkTypeName: "JPEG",
MacExtensions: []string{"jpg", "JPG", "jpeg", "JPEG"},
IconFile: "jpeg.icns",
MIMETypes: []string{"image/jpeg"},
TypeName: "JPEG Image",
OSTypes: []string{"JPEG"},
ItemContentTypes: "public.jpeg",
},
{
TkTypeName: "GIF",
MacExtensions: []string{"gif", "GIF"},
IconFile: "gif.icns",
MIMETypes: []string{"image/gif"},
TypeName: "Graphics Interchange Format Image",
OSTypes: []string{"GIFf"},
ItemContentTypes: "com.compuserve.gif",
},
{
TkTypeName: "PNG",
MacExtensions: []string{"PNG", "png"},
IconFile: "png.icns",
MIMETypes: []string{"image/png"},
TypeName: "Portable Network Graphics Image",
OSTypes: []string{"PNGf"},
ItemContentTypes: "public.png",
},
{
TkTypeName: "TGA",
MacExtensions: []string{"tga", "TGA"},
IconFile: "tga.icns",
MIMETypes: []string{"image/targa", "image/tga", "application/tga"},
TypeName: "Targa Image",
OSTypes: []string{"TPIC"},
ItemContentTypes: "com.truevision.tga-image",
},
{
TkTypeName: "TIFF",
MacExtensions: []string{"tiff", "TIFF", "tif", "TIF"},
IconFile: "tiff.icns",
MIMETypes: []string{"image/tiff"},
TypeName: "TIFF Image",
OSTypes: []string{"TIFF"},
ItemContentTypes: "public.tiff",
},
{
TkTypeName: "WEBP",
MacExtensions: []string{"webp", "WEBP"},
IconFile: "webp.icns",
MIMETypes: []string{"image/webp"},
TypeName: "WebP Image",
OSTypes: []string{"WEBP"},
ItemContentTypes: "org.webmproject.webp",
},
{
TkTypeName: "CBZ",
MacExtensions: []string{"cbz", "CBZ"},
IconFile: "cbz.icns",
MIMETypes: []string{"application/vnd.comicbook+zip", "application/x-cbz"},
TypeName: "Comic Book Zip Archive",
OSTypes: []string{"CBZ"},
ItemContentTypes: "in.yetaga.why.cbz",
},
{
TkTypeName: "CBR",
MacExtensions: []string{"cbr", "CBR"},
IconFile: "cbr.icns",
MIMETypes: []string{"application/vnd.comicbook-rar", "application/x-cbr"},
TypeName: "Comic Book Rar Archive",
OSTypes: []string{"CBR"},
ItemContentTypes: "in.yetaga.why.cbr",
},
}
func GetTkTypes(fds []FileDescription) []tk.FileType {
fts := make([]tk.FileType, len(fds))
for i, fd := range fds {
fts[i] = tk.FileType{
TypeName: fd.TkTypeName,
Extensions: make([]string, len(fd.MacExtensions)),
}
for j, ex := range fd.MacExtensions {
fts[i].Extensions[j] = fmt.Sprintf(".%s", ex)
}
}
return fts
}
func IsImage(entry os.DirEntry) bool {
if entry.IsDir() {
return false
}
ext := filepath.Ext(entry.Name())
if ext == "" {
return false
}
for _, ft := range Valid {
if slices.Contains(ft.MacExtensions, strings.ToLower(ext[1:])) {
return true
}
}
return false
}