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
|
Regex string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Tags []Tag
|
||||||
|
|
||||||
type RTag struct {
|
type RTag struct {
|
||||||
Name string
|
Name string
|
||||||
Regex *regexp.Regexp
|
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 {
|
type Path struct {
|
||||||
Name string
|
Name string
|
||||||
Abs string
|
Abs string
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/core"
|
"git.giftfish.de/ston1th/docstore/pkg/core"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/log"
|
"git.giftfish.de/ston1th/docstore/pkg/log"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/tags"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -20,9 +19,9 @@ const (
|
||||||
pathPrefix = "path/"
|
pathPrefix = "path/"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (db *DB) Reindex(path string, match []core.RTag) (err error) {
|
func (db *DB) Reindex(path string, tags core.RTags) (err error) {
|
||||||
if match == nil {
|
if tags == nil {
|
||||||
match, err = db.GetAllRTags()
|
tags, err = db.GetAllRTags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +35,7 @@ func (db *DB) Reindex(path string, match []core.RTag) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
doc.Tags = tags.Find(doc.Text, match)
|
doc.Tags = tags.Match(doc.Text)
|
||||||
err = db.Index.Reindex(id, doc)
|
err = db.Index.Reindex(id, doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
@ -53,12 +52,12 @@ func (db *DB) ReindexAll() (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
match, err := db.GetAllRTags()
|
tags, err := db.GetAllRTags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
e := db.Reindex(p, match)
|
e := db.Reindex(p, tags)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = errors.New("reindexAll had errors")
|
err = errors.New("reindexAll had errors")
|
||||||
|
|
@ -69,7 +68,7 @@ func (db *DB) ReindexAll() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) GetPaths(res []core.Result) {
|
func (db *DB) GetPaths(res core.Results) {
|
||||||
for i, v := range res {
|
for i, v := range res {
|
||||||
err := db.store.Get(pathPrefix+v.ID, &res[i].Path)
|
err := db.store.Get(pathPrefix+v.ID, &res[i].Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ const (
|
||||||
tagPrefix = "tag/"
|
tagPrefix = "tag/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultTags = []core.Tag{
|
var defaultTags = core.Tags{
|
||||||
{"invoice", `(?i)[^e]rechnung|quittung|beleg|invoice|bill|check`},
|
{"invoice", `(?i)[^e]rechnung|quittung|beleg|invoice|bill|check`},
|
||||||
{"tax", `(?i)steuer|lohn|tax|salary`},
|
{"tax", `(?i)steuer|lohn|tax|salary`},
|
||||||
{"contract", `(?i)vertrag|kündigung|vereinbarung|contract|termination|agreement`},
|
{"contract", `(?i)vertrag|kündigung|vereinbarung|contract|termination|agreement`},
|
||||||
|
|
@ -29,7 +29,7 @@ func (db *DB) addDefaultTags() (err error) {
|
||||||
return
|
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 {
|
err = db.store.ForEachPrefix(tagsPrefix, func(k string, _ []byte) error {
|
||||||
var t core.Tag
|
var t core.Tag
|
||||||
err := db.store.Get(tagsPrefix+k, &t)
|
err := db.store.Get(tagsPrefix+k, &t)
|
||||||
|
|
@ -41,7 +41,7 @@ func (db *DB) GetAllTags() (tags []core.Tag, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) GetAllRTags() (rtags []core.RTag, err error) {
|
func (db *DB) GetAllRTags() (rtags core.RTags, err error) {
|
||||||
tags, err := db.GetAllTags()
|
tags, err := db.GetAllTags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func isPDF(path string) bool {
|
func isPDF(path string) bool {
|
||||||
return checkContentType(path) || checkExtension(path)
|
// TODO
|
||||||
|
// return checkContentType(path) || checkExtension(path)
|
||||||
|
return checkContentType(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkContentType(path string) bool {
|
func checkContentType(path string) bool {
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/fs"
|
"git.giftfish.de/ston1th/docstore/pkg/fs"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/log"
|
"git.giftfish.de/ston1th/docstore/pkg/log"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/otp"
|
"git.giftfish.de/ston1th/docstore/pkg/otp"
|
||||||
"git.giftfish.de/ston1th/docstore/pkg/tags"
|
|
||||||
"git.giftfish.de/ston1th/jwt/v3"
|
"git.giftfish.de/ston1th/jwt/v3"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -187,11 +186,11 @@ func scanFile(ctx *Context, path string) (err error) {
|
||||||
l.Printf("scanner: %s: %s", path, err)
|
l.Printf("scanner: %s: %s", path, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rt, err := ctx.Srv.DB.GetAllRTags()
|
tags, err := ctx.Srv.DB.GetAllRTags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Printf("getTags: %s: %s", path, err)
|
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)
|
id, err := ctx.Srv.DB.Index.Add(txt, found)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Printf("index: %s: %s", path, err)
|
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