forked from WithoutPants/stash-plugin-duplicate-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
77 lines (60 loc) · 1.29 KB
/
db.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
package main
import (
"image/jpeg"
"io/ioutil"
"os"
"sort"
"stash-plugin-duplicate-finder/internal/plugin/common/log"
"github.com/rivo/duplo"
)
func storeDB(store *duplo.Store, filename string) error {
data, err := store.GobEncode()
if err != nil {
return err
}
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
return err
}
return nil
}
func readDB(store *duplo.Store, filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
// assume no file
log.Info("Assuming no existing db file. Starting from scratch...")
}
err = store.GobDecode(data)
if err != nil {
return err
}
log.Infof("Read store from file: %d hashes loaded", store.Size())
return nil
}
func getImageHash(fn string) (*duplo.Hash, error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
img, err := jpeg.Decode(f)
if err != nil {
return nil, err
}
hash, _ := duplo.CreateHash(img)
return &hash, nil
}
func getHashMatches(store *duplo.Store, checksum string, hash duplo.Hash, threshold int) duplo.Matches {
ret := duplo.Matches{}
matches := store.Query(hash)
sort.Sort(matches)
for _, m := range matches {
// exclude same id
if checksum == m.ID {
continue
}
if m.Score <= float64(-threshold) {
ret = append(ret, m)
}
}
return ret
}