possibly first working draft
This commit is contained in:
@ -1,15 +1,22 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"donniemarko/internal/note"
|
||||
"donniemarko/internal/storage"
|
||||
"os"
|
||||
// "log"
|
||||
)
|
||||
|
||||
type NotesHandler struct {
|
||||
storage storage.Storage
|
||||
}
|
||||
|
||||
func NewNotesHandler(storage storage.Storage) *NotesHandler {
|
||||
return &NotesHandler{storage: storage}
|
||||
}
|
||||
|
||||
func (h *NotesHandler) HandleCreate(path string) error {
|
||||
note, err := ParseNoteFile(path)
|
||||
if err != nil {
|
||||
@ -34,9 +41,23 @@ func ParseNoteFile(path string) (*note.Note, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get file info to get modification time
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := note.GenerateNoteID(path)
|
||||
nn := note.NewNote()
|
||||
nn.ID = id
|
||||
nn.Content = string(content)
|
||||
nn.Title = note.ExtractTitle(nn.Content)
|
||||
if nn.Title == "" {
|
||||
// Use filename as title if no heading found
|
||||
nn.Title = filepath.Base(path)
|
||||
}
|
||||
nn.UpdatedAt = fileInfo.ModTime()
|
||||
nn.CreatedAt = fileInfo.ModTime()
|
||||
return nn, nil
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
// "donniemarko/internal/note"
|
||||
)
|
||||
|
||||
type ChangeType int
|
||||
@ -32,13 +33,21 @@ type Change struct {
|
||||
|
||||
type ScannerService struct {
|
||||
RootDir string
|
||||
interval time.Duration
|
||||
lastStates map[string]time.Time
|
||||
Interval time.Duration
|
||||
LastStates map[string]time.Time
|
||||
handler ChangeHandler
|
||||
}
|
||||
|
||||
func NewScanner(path string) *ScannerService {
|
||||
return &ScannerService{RootDir: path}
|
||||
return &ScannerService{
|
||||
RootDir: path,
|
||||
Interval: 5 * time.Second,
|
||||
LastStates: make(map[string]time.Time),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ScannerService) SetHandler(handler ChangeHandler) {
|
||||
s.handler = handler
|
||||
}
|
||||
|
||||
func (s *ScannerService) FindAll() ([]string, error) {
|
||||
@ -63,6 +72,8 @@ func (s *ScannerService) FindAll() ([]string, error) {
|
||||
return notePath, err
|
||||
}
|
||||
|
||||
// Scan walks the root folder and update the states of each notes if
|
||||
// it has changed since the last time a scan occured
|
||||
func (s *ScannerService) Scan() ([]Change, error) {
|
||||
var changes []Change
|
||||
currentStates := make(map[string]time.Time)
|
||||
@ -81,8 +92,10 @@ func (s *ScannerService) Scan() ([]Change, error) {
|
||||
|
||||
currentStates[path] = info.ModTime()
|
||||
|
||||
lastMod, existed := s.lastStates[path]
|
||||
lastMod, existed := s.LastStates[path]
|
||||
if !existed {
|
||||
// create the note if it didn't exist yet
|
||||
s.handler.HandleCreate(path)
|
||||
changes = append(changes, Change{Type: Created, Path: path, ModTime: lastMod})
|
||||
} else if info.ModTime().After(lastMod) {
|
||||
changes = append(changes, Change{Type: Modified, Path: path, ModTime: info.ModTime()})
|
||||
@ -92,18 +105,20 @@ func (s *ScannerService) Scan() ([]Change, error) {
|
||||
})
|
||||
|
||||
// Check for deletions
|
||||
for path := range s.lastStates {
|
||||
for path := range s.LastStates {
|
||||
if _, exists := currentStates[path]; !exists {
|
||||
changes = append(changes, Change{Type: Deleted, Path: path})
|
||||
}
|
||||
}
|
||||
|
||||
s.lastStates = currentStates
|
||||
s.LastStates = currentStates
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
// Monitor rescan the root folder at each new tick and handle state
|
||||
// modification
|
||||
func (s *ScannerService) Monitor(ctx context.Context) error {
|
||||
ticker := time.NewTicker(s.interval)
|
||||
ticker := time.NewTicker(s.Interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user