-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.go
134 lines (111 loc) · 3.2 KB
/
dict.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"time"
d "github.com/runik-3/builder/dict"
c "github.com/runik-3/core/core"
)
type File struct {
Name string
Display string
Extension string
Size int64
Modified time.Time
}
func (a *App) GetLocalDictionaries() c.Response[[]File] {
files, err := getDictFilesFromPath(a.dictionaryDir)
if err != nil {
return c.Response[[]File]{Data: []File{}, Error: err.Error()}
}
localDicts := []File{}
for _, file := range files {
if file.Extension == "json" {
localDicts = append(localDicts, file)
}
}
return c.Response[[]File]{Data: localDicts, Error: ""}
}
func (a *App) GetDeviceDictionaries() c.Response[[]File] {
// if no device connected, do no work and return empty with no error
if a.devicePath == "" {
return c.Response[[]File]{Data: []File{}, Error: ""}
}
// TODO: This is a kobo specific solution. Generalize when we have
// support for other readers.
koboDictDir, _ := c.FindKoboDictDir(a.devicePath)
files, err := getDictFilesFromPath(koboDictDir)
if err != nil {
return c.Response[[]File]{Data: []File{}, Error: err.Error()}
}
deviceDicts := []File{}
for _, file := range files {
if file.Extension == "zip" {
deviceDicts = append(deviceDicts, file)
}
}
return c.Response[[]File]{Data: deviceDicts, Error: ""}
}
func (a *App) ReadLocalDictionary(name string) c.Response[d.Dict] {
path := filepath.Join(a.dictionaryDir, name)
data, err := os.ReadFile(path)
if err != nil {
return c.Response[d.Dict]{Data: d.Dict{}, Error: err.Error()}
}
var dict d.Dict
err = json.Unmarshal(data, &dict)
return c.Response[d.Dict]{Data: dict, Error: ""}
}
func (a *App) WriteLocalDictionary(dict d.Dict) c.Response[string] {
path, err := dict.Write(a.dictionaryDir, "json")
if err != nil {
return c.Response[string]{Data: "", Error: err.Error()}
}
return c.Response[string]{Data: path, Error: ""}
}
func (a *App) DeleteLocalDictFile(name string) c.Response[string] {
dictFilePath := filepath.Join(a.dictionaryDir, name)
err := os.Remove(dictFilePath)
if err != nil {
return c.Response[string]{Data: "", Error: err.Error()}
}
return c.Response[string]{Data: "", Error: ""}
}
func (a *App) DeleteDeviceDictFile(name string) c.Response[string] {
if a.devicePath == "" {
return c.Response[string]{Data: "", Error: "No device connected"}
}
// TODO: This is a kobo specific solution. Generalize when we have
// support for other readers.
koboDictDir, _ := c.FindKoboDictDir(a.devicePath)
dictFilePath := filepath.Join(koboDictDir, name)
err := os.Remove(dictFilePath)
if err != nil {
return c.Response[string]{Data: "", Error: err.Error()}
}
return c.Response[string]{Data: "", Error: ""}
}
func getDictFilesFromPath(path string) ([]File, error) {
files := []File{}
fileEntries, err := os.ReadDir(path)
if err != nil {
return []File{}, err
}
for _, entry := range fileEntries {
info, err := entry.Info()
if err != nil {
return []File{}, err
}
fileParts := strings.Split(entry.Name(), ".")
files = append(files, File{
Name: entry.Name(),
Display: fileParts[0],
Extension: fileParts[1],
Size: info.Size(),
Modified: info.ModTime(),
})
}
return files, nil
}