-
Notifications
You must be signed in to change notification settings - Fork 2k
/
data.go
118 lines (109 loc) · 2.44 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package nativesetu
import (
"bytes"
"image"
"io/fs"
"os"
"strings"
"sync"
"time"
"github.com/corona10/goimagehash"
"github.com/sirupsen/logrus"
_ "golang.org/x/image/webp" // import webp decoding
"github.com/FloatTech/floatbox/file"
sql "github.com/FloatTech/sqlite"
)
// setuclass holds setus in a folder, which is the class name.
type setuclass struct {
ImgID int64 `db:"imgid"` // ImgID 图片唯一 id (dhash)
Name string `db:"name"` // Name 图片名
Path string `db:"path"` // Path 图片路径
}
var ns = &nsetu{db: &sql.Sqlite{}}
type nsetu struct {
db *sql.Sqlite
mu sync.RWMutex
}
func (n *nsetu) List() (l []string) {
if file.IsExist(n.db.DBPath) {
var err error
l, err = n.db.ListTables()
if err != nil {
logrus.Errorln("[nsetu]", err)
}
}
return
}
func (n *nsetu) scanall(path string) error {
model := &setuclass{}
root := os.DirFS(path)
_ = n.db.Close()
_ = os.Remove(n.db.DBPath)
err := n.db.Open(time.Hour * 24)
if err != nil {
return err
}
return fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
clsn := d.Name()
if clsn != "." {
n.mu.Lock()
err = n.db.Create(clsn, model)
n.mu.Unlock()
if err == nil {
err = n.scanclass(root, path, clsn)
if err != nil {
logrus.Errorln("[nsetu]", err)
return err
}
}
}
}
return nil
})
}
func (n *nsetu) scanclass(root fs.FS, path, clsn string) error {
ds, err := fs.ReadDir(root, path)
if err != nil {
return err
}
n.mu.Lock()
_ = n.db.Drop(clsn)
_ = n.db.Create(clsn, &setuclass{})
n.mu.Unlock()
for _, d := range ds {
nm := d.Name()
ln := strings.ToLower(nm)
if !d.IsDir() &&
(strings.HasSuffix(ln, ".jpg") || strings.HasSuffix(ln, ".jpeg") ||
strings.HasSuffix(ln, ".png") || strings.HasSuffix(ln, ".gif") || strings.HasSuffix(ln, ".webp")) {
relpath := path + "/" + nm
logrus.Debugln("[nsetu] read", relpath)
f, e := fs.ReadFile(root, relpath)
if e != nil {
return e
}
b := bytes.NewReader(f)
img, _, e := image.Decode(b)
if e != nil {
return e
}
dh, e := goimagehash.DifferenceHash(img)
if e != nil {
return e
}
dhi := int64(dh.GetHash())
logrus.Debugln("[nsetu] insert", nm, "with id", dhi, "into", clsn)
n.mu.Lock()
err = n.db.Insert(clsn, &setuclass{ImgID: dhi, Name: nm, Path: relpath})
n.mu.Unlock()
if err != nil {
return err
}
}
}
return nil
}