2021-01-03 16:09:10 +00:00
|
|
|
package note
|
2021-01-03 13:43:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-01-09 11:17:15 +00:00
|
|
|
"path/filepath"
|
2021-01-03 13:43:27 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-01-03 16:09:10 +00:00
|
|
|
"github.com/mickael-menu/zk/core/zk"
|
2021-01-03 13:43:27 +00:00
|
|
|
"github.com/mickael-menu/zk/util"
|
|
|
|
"github.com/mickael-menu/zk/util/errors"
|
2021-01-09 12:01:41 +00:00
|
|
|
"github.com/mickael-menu/zk/util/paths"
|
2021-01-03 13:43:27 +00:00
|
|
|
"gopkg.in/djherbis/times.v1"
|
|
|
|
)
|
|
|
|
|
2021-01-03 16:09:10 +00:00
|
|
|
// Metadata holds information about a particular note.
|
|
|
|
type Metadata struct {
|
2021-01-09 11:17:15 +00:00
|
|
|
Path string
|
2021-01-03 13:43:27 +00:00
|
|
|
Title string
|
|
|
|
Body string
|
|
|
|
WordCount int
|
|
|
|
Created time.Time
|
|
|
|
Modified time.Time
|
|
|
|
Checksum string
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:29:57 +00:00
|
|
|
func (m Metadata) String() string {
|
|
|
|
return fmt.Sprintf(`note.Metadata{
|
|
|
|
Path: "%v",
|
|
|
|
Title: "%v",
|
|
|
|
Body: "%v",
|
|
|
|
WordCount: %v,
|
|
|
|
Created: "%v",
|
|
|
|
Modified: "%v",
|
|
|
|
Checksum: "%v",
|
|
|
|
}`, m.Path, m.Title, m.Body, m.WordCount, m.Created.Format(time.RFC3339), m.Modified.Format(time.RFC3339), m.Checksum)
|
|
|
|
}
|
|
|
|
|
2021-01-03 16:09:10 +00:00
|
|
|
// Indexer persists the notes index.
|
2021-01-03 13:43:27 +00:00
|
|
|
type Indexer interface {
|
2021-01-03 16:09:10 +00:00
|
|
|
// Indexed returns the list of indexed note file metadata.
|
2021-01-09 12:01:41 +00:00
|
|
|
Indexed() (<-chan paths.Metadata, error)
|
2021-01-03 16:09:10 +00:00
|
|
|
// Add indexes a new note from its metadata.
|
|
|
|
Add(metadata Metadata) error
|
|
|
|
// Update updates the metadata of an already indexed note.
|
|
|
|
Update(metadata Metadata) error
|
|
|
|
// Remove deletes a note from the index.
|
2021-01-09 11:17:15 +00:00
|
|
|
Remove(path string) error
|
2021-01-03 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index indexes the content of the notes in the given directory.
|
2021-01-16 18:37:10 +00:00
|
|
|
func Index(dir zk.Dir, parser Parser, indexer Indexer, logger util.Logger) error {
|
2021-01-03 13:43:27 +00:00
|
|
|
wrap := errors.Wrapper("indexation failed")
|
|
|
|
|
2021-01-09 12:01:41 +00:00
|
|
|
source := paths.Walk(dir.Path, dir.Config.Extension, logger)
|
2021-01-03 13:43:27 +00:00
|
|
|
target, err := indexer.Indexed()
|
|
|
|
if err != nil {
|
|
|
|
return wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:01:41 +00:00
|
|
|
err = paths.Diff(source, target, func(change paths.DiffChange) error {
|
2021-01-03 13:43:27 +00:00
|
|
|
switch change.Kind {
|
2021-01-09 12:01:41 +00:00
|
|
|
case paths.DiffAdded:
|
2021-01-16 18:37:10 +00:00
|
|
|
metadata, err := metadata(change.Path, dir.Path, parser)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err == nil {
|
|
|
|
err = indexer.Add(metadata)
|
|
|
|
}
|
|
|
|
logger.Err(err)
|
|
|
|
|
2021-01-09 12:01:41 +00:00
|
|
|
case paths.DiffModified:
|
2021-01-16 18:37:10 +00:00
|
|
|
metadata, err := metadata(change.Path, dir.Path, parser)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err == nil {
|
|
|
|
err = indexer.Update(metadata)
|
|
|
|
}
|
|
|
|
logger.Err(err)
|
|
|
|
|
2021-01-09 12:01:41 +00:00
|
|
|
case paths.DiffRemoved:
|
2021-01-03 16:09:10 +00:00
|
|
|
err := indexer.Remove(change.Path)
|
|
|
|
logger.Err(err)
|
2021-01-03 13:43:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-01-03 16:09:10 +00:00
|
|
|
|
|
|
|
return wrap(err)
|
2021-01-03 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 16:09:10 +00:00
|
|
|
// metadata retrieves note metadata for the given file.
|
2021-01-16 18:37:10 +00:00
|
|
|
func metadata(path string, basePath string, parser Parser) (Metadata, error) {
|
2021-01-03 16:09:10 +00:00
|
|
|
metadata := Metadata{
|
2021-01-03 13:43:27 +00:00
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
|
2021-01-09 11:17:15 +00:00
|
|
|
absPath := filepath.Join(basePath, path)
|
|
|
|
content, err := ioutil.ReadFile(absPath)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return metadata, err
|
|
|
|
}
|
|
|
|
contentStr := string(content)
|
2021-01-16 18:37:10 +00:00
|
|
|
contentParts, err := parser.Parse(contentStr)
|
|
|
|
if err != nil {
|
|
|
|
return metadata, err
|
|
|
|
}
|
|
|
|
metadata.Title = contentParts.Title.String()
|
|
|
|
metadata.Body = contentParts.Body.String()
|
2021-01-03 13:43:27 +00:00
|
|
|
metadata.WordCount = len(strings.Fields(contentStr))
|
|
|
|
metadata.Checksum = fmt.Sprintf("%x", sha256.Sum256(content))
|
|
|
|
|
2021-01-09 11:17:15 +00:00
|
|
|
times, err := times.Stat(absPath)
|
2021-01-03 13:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return metadata, err
|
|
|
|
}
|
|
|
|
|
2021-01-12 19:33:55 +00:00
|
|
|
metadata.Modified = times.ModTime().UTC()
|
2021-01-03 13:43:27 +00:00
|
|
|
if times.HasBirthTime() {
|
2021-01-12 19:33:55 +00:00
|
|
|
metadata.Created = times.BirthTime().UTC()
|
2021-01-03 13:43:27 +00:00
|
|
|
} else {
|
2021-01-12 19:33:55 +00:00
|
|
|
metadata.Created = time.Now().UTC()
|
2021-01-03 13:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return metadata, nil
|
|
|
|
}
|