simplify tags
This commit is contained in:
parent
7686dfa474
commit
3c5be25cb3
6 changed files with 28 additions and 31 deletions
|
|
@ -28,11 +28,24 @@ type Tag struct {
|
|||
Regex string
|
||||
}
|
||||
|
||||
type Tags []Tag
|
||||
|
||||
type RTag struct {
|
||||
Name string
|
||||
Regex *regexp.Regexp
|
||||
}
|
||||
|
||||
type RTags []RTag
|
||||
|
||||
func (rt RTags) Match(text string) (found []string) {
|
||||
for _, t := range rt {
|
||||
if t.Regex.MatchString(text) {
|
||||
found = append(found, t.Name)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Path struct {
|
||||
Name string
|
||||
Abs string
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"errors"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/log"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/tags"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -20,9 +19,9 @@ const (
|
|||
pathPrefix = "path/"
|
||||
)
|
||||
|
||||
func (db *DB) Reindex(path string, match []core.RTag) (err error) {
|
||||
if match == nil {
|
||||
match, err = db.GetAllRTags()
|
||||
func (db *DB) Reindex(path string, tags core.RTags) (err error) {
|
||||
if tags == nil {
|
||||
tags, err = db.GetAllRTags()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -36,7 +35,7 @@ func (db *DB) Reindex(path string, match []core.RTag) (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
doc.Tags = tags.Find(doc.Text, match)
|
||||
doc.Tags = tags.Match(doc.Text)
|
||||
err = db.Index.Reindex(id, doc)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -53,12 +52,12 @@ func (db *DB) ReindexAll() (err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
match, err := db.GetAllRTags()
|
||||
tags, err := db.GetAllRTags()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, p := range paths {
|
||||
e := db.Reindex(p, match)
|
||||
e := db.Reindex(p, tags)
|
||||
if e != nil {
|
||||
if err == nil {
|
||||
err = errors.New("reindexAll had errors")
|
||||
|
|
@ -69,7 +68,7 @@ func (db *DB) ReindexAll() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (db *DB) GetPaths(res []core.Result) {
|
||||
func (db *DB) GetPaths(res core.Results) {
|
||||
for i, v := range res {
|
||||
err := db.store.Get(pathPrefix+v.ID, &res[i].Path)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ const (
|
|||
tagPrefix = "tag/"
|
||||
)
|
||||
|
||||
var defaultTags = []core.Tag{
|
||||
var defaultTags = core.Tags{
|
||||
{"invoice", `(?i)[^e]rechnung|quittung|beleg|invoice|bill|check`},
|
||||
{"tax", `(?i)steuer|lohn|tax|salary`},
|
||||
{"contract", `(?i)vertrag|kündigung|vereinbarung|contract|termination|agreement`},
|
||||
|
|
@ -29,7 +29,7 @@ func (db *DB) addDefaultTags() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (db *DB) GetAllTags() (tags []core.Tag, err error) {
|
||||
func (db *DB) GetAllTags() (tags core.Tags, err error) {
|
||||
err = db.store.ForEachPrefix(tagsPrefix, func(k string, _ []byte) error {
|
||||
var t core.Tag
|
||||
err := db.store.Get(tagsPrefix+k, &t)
|
||||
|
|
@ -41,7 +41,7 @@ func (db *DB) GetAllTags() (tags []core.Tag, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (db *DB) GetAllRTags() (rtags []core.RTag, err error) {
|
||||
func (db *DB) GetAllRTags() (rtags core.RTags, err error) {
|
||||
tags, err := db.GetAllTags()
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -15,7 +15,9 @@ const (
|
|||
)
|
||||
|
||||
func isPDF(path string) bool {
|
||||
return checkContentType(path) || checkExtension(path)
|
||||
// TODO
|
||||
// return checkContentType(path) || checkExtension(path)
|
||||
return checkContentType(path)
|
||||
}
|
||||
|
||||
func checkContentType(path string) bool {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"git.giftfish.de/ston1th/docstore/pkg/fs"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/log"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/otp"
|
||||
"git.giftfish.de/ston1th/docstore/pkg/tags"
|
||||
"git.giftfish.de/ston1th/jwt/v3"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
|
@ -187,11 +186,11 @@ func scanFile(ctx *Context, path string) (err error) {
|
|||
l.Printf("scanner: %s: %s", path, err)
|
||||
return
|
||||
}
|
||||
rt, err := ctx.Srv.DB.GetAllRTags()
|
||||
tags, err := ctx.Srv.DB.GetAllRTags()
|
||||
if err != nil {
|
||||
l.Printf("getTags: %s: %s", path, err)
|
||||
}
|
||||
found := tags.Find(txt, rt)
|
||||
found := tags.Match(txt)
|
||||
id, err := ctx.Srv.DB.Index.Add(txt, found)
|
||||
if err != nil {
|
||||
l.Printf("index: %s: %s", path, err)
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package tags
|
||||
|
||||
import (
|
||||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||
)
|
||||
|
||||
func Find(text string, tags []core.RTag) (found []string) {
|
||||
for _, t := range tags {
|
||||
if t.Regex.MatchString(text) {
|
||||
found = append(found, t.Name)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue