Files
gobsidian/internal/models/models.go

94 lines
2.1 KiB
Go

package models
import (
"path/filepath"
"strings"
"time"
)
type FileType string
const (
FileTypeMarkdown FileType = "md"
FileTypeDirectory FileType = "dir"
FileTypeImage FileType = "image"
FileTypeText FileType = "text"
FileTypeOther FileType = "other"
)
type FileInfo struct {
Name string `json:"name"`
Path string `json:"path"`
Type FileType `json:"type"`
Size int64 `json:"size"`
ModTime time.Time `json:"mod_time"`
DisplayName string `json:"display_name"`
}
type TreeNode struct {
Name string `json:"name"`
Path string `json:"path"`
Type FileType `json:"type"`
Children []*TreeNode `json:"children,omitempty"`
}
type Breadcrumb struct {
Name string `json:"name"`
URL string `json:"url"`
}
type ImageStorageInfo struct {
StorageDir string
MarkdownPath string
}
func GetFileType(extension string, allowedImageExts, allowedFileExts []string) FileType {
ext := strings.ToLower(strings.TrimPrefix(extension, "."))
if ext == "md" || ext == "markdown" {
return FileTypeMarkdown
}
// Check if it's an allowed image extension
for _, allowedExt := range allowedImageExts {
if strings.ToLower(strings.TrimPrefix(allowedExt, ".")) == ext {
return FileTypeImage
}
}
// Check if it's an allowed file extension
for _, allowedExt := range allowedFileExts {
if strings.ToLower(strings.TrimPrefix(allowedExt, ".")) == ext {
return FileTypeText
}
}
return FileTypeOther
}
func IsImageFile(filename string, allowedImageExts []string) bool {
ext := strings.ToLower(filepath.Ext(filename))
ext = strings.TrimPrefix(ext, ".")
for _, allowedExt := range allowedImageExts {
if strings.ToLower(strings.TrimPrefix(allowedExt, ".")) == ext {
return true
}
}
return false
}
func IsAllowedFile(filename string, allowedExts []string) bool {
ext := strings.ToLower(filepath.Ext(filename))
ext = strings.TrimPrefix(ext, ".")
for _, allowedExt := range allowedExts {
if strings.ToLower(strings.TrimPrefix(allowedExt, ".")) == ext {
return true
}
}
return false
}