make notes api return any file (so images and etc can be sent)

This commit is contained in:
2026-01-13 12:13:20 +00:00
parent e756e160d7
commit 18c87c170b
3 changed files with 15 additions and 32 deletions

View File

@@ -5,19 +5,12 @@ import (
"os"
"path/filepath"
"strings"
"time"
)
type NotesConfig struct {
Dir string
}
type Note struct {
Filename string `json:"title"`
Contents string `json:"contents"`
LastEdited time.Time `json:"last_edited"`
}
type Notes struct {
Config NotesConfig
}
@@ -28,39 +21,26 @@ func InitNotes(config *NotesConfig) *Notes {
}
}
var ErrPathTraversal = errors.New("invalid path")
func (notes *Notes) ParsePath(path string) (string, error) {
if path == "" {
path = "Index.md"
}
func (notes *Notes) GetNote(path string) (*Note, error) {
baseDir, err := filepath.Abs(notes.Config.Dir)
if err != nil {
return nil, err
return "", err
}
fullPath := filepath.Join(baseDir, path)
fullPath, err = filepath.Abs(fullPath)
if err != nil {
return nil, err
return "", err
}
// Enforce directory boundary
if !strings.HasPrefix(fullPath, baseDir+string(os.PathSeparator)) {
return nil, ErrPathTraversal
return "", errors.New("Invalid path")
}
fullPath += ".md"
data, err := os.ReadFile(fullPath)
if err != nil {
return nil, err
}
info, err := os.Stat(fullPath)
if err != nil {
return nil, err
}
return &Note{
Filename: info.Name(),
Contents: string(data),
LastEdited: info.ModTime(),
}, nil
return fullPath, nil
}