-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
323 lines (288 loc) · 6.88 KB
/
common.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package util
import (
"encoding/json"
"fmt"
"github.com/samber/lo"
bolt "go.etcd.io/bbolt"
"log"
"os"
"path/filepath"
"strings"
)
func PrintVersionExit(ok bool) {
if ok {
fmt.Println(RepoName)
fmt.Println(RepoURL)
fmt.Println("Version: 2024-05-07")
os.Exit(0)
}
}
func PrintWhereExit(ok bool) {
if ok {
fmt.Println(GetExePath())
os.Exit(0)
}
}
func FolderMustEmpty(folder string) {
if folder == "" || folder == "." {
folder = GetCwd()
}
if lo.Must(DirIsNotEmpty(folder)) {
log.Fatalln("資料夾不為空:", folder)
}
}
func MakeFolders(verbose bool) {
for _, folder := range Folders {
if verbose {
fmt.Println("Create folder:", folder)
}
lo.Must0(MkdirIfNotExists(folder))
}
}
func ExecutableDir() string {
return filepath.Dir(GetExePath())
}
func InitFileChecked() {
m := make(map[int]int)
fmt.Println("Create", FileCheckedPath)
_ = lo.Must(WriteJSON(m, FileCheckedPath))
}
func ReadFileChecked(root string) (fcMap map[string]*FileChecked, err error) {
fileCheckedPath := filepath.Join(root, FileCheckedPath)
if PathNotExists(fileCheckedPath) {
return
}
data := lo.Must(os.ReadFile(fileCheckedPath))
err = json.Unmarshal(data, &fcMap)
return
}
func ReadProjectInfo(root string) (info ProjectInfo) {
infoPath := filepath.Join(root, ProjectInfoPath)
data := lo.Must(os.ReadFile(infoPath))
lo.Must0(json.Unmarshal(data, &info))
return
}
func WriteProjectInfo(info ProjectInfo) error {
_, err := WriteJSON(info, ProjectInfoPath)
return err
}
func MustInWuliu() {
if PathNotExists(ProjectInfoPath) {
log.Fatalln("找不到 project.json")
}
info := ReadProjectInfo(".")
if info.RepoName != RepoName {
log.Fatalf("RepoName (%s) != '%s'", info.RepoName, RepoName)
}
}
func CheckNotAllowInBackup() {
projInfo := ReadProjectInfo(".")
if err := notAllowInBackup(projInfo.IsBackup); err != nil {
log.Fatalln(err)
}
}
func notAllowInBackup(isBackup bool) error {
if isBackup {
return fmt.Errorf("這是備份專案, 不可使用該功能")
}
return nil
}
func FindOrphans() (fileOrphans, metaOrphans []string, err error) {
files, e1 := NamesInFiles()
metas, e2 := namesInMetadataTrim()
if err = WrapErrors(e1, e2); err != nil {
return
}
fileOrphans, metaOrphans = lo.Difference(files, metas)
return
}
// NewFilesFrom 把档案名 (names) 转换为 files, 此时假设档案在 folder 资料夹内。
func NewFilesFrom(names []string, folder string) (files []*File, err error) {
for _, name := range names {
filePath := filepath.Join(folder, name)
info, err := os.Lstat(filePath)
if err != nil {
return nil, err
}
checksum, err := FileSum512(filePath)
if err != nil {
return nil, err
}
f := NewFile(name)
f.Checksum = checksum
f.Size = info.Size()
f.Type = TypeByFilename(name)
f.Keywords = []string{}
f.Collections = []string{}
f.Albums = []string{}
files = append(files, f)
}
return
}
func NamesInFiles() ([]string, error) {
return GetFilenamesBase(FILES)
}
func NamesInBuffer() ([]string, error) {
return GetFilenamesBase(BUFFER)
}
func NamesInInput() ([]string, error) {
return GetFilenamesBase(INPUT)
}
func namesInMetadata() ([]string, error) {
return GetFilenamesBase(METADATA)
}
func namesInMetadataTrim() ([]string, error) {
names, err := namesInMetadata()
if err != nil {
return nil, err
}
trimmed := lo.Map(names, func(name string, _ int) string {
return strings.TrimSuffix(name, ".json")
})
return trimmed, nil
}
// deleteFileByName 尝试删除档案,例如 name=abc.txt,
// 则尝试删除 files/abc.txt 和 metadata/abc.txt.json。
// 注意,这里说的删除是将档案移动到专案根目录的 recyclebin 中,
// 如果 recyclebin 里有同名档案则直接覆盖。
func deleteFileByName(name string) {
f := filepath.Join(FILES, name)
m := filepath.Join(METADATA, name+".json")
for _, oldpath := range []string{f, m} {
if PathNotExists(oldpath) {
fmt.Println("NotFound =>", oldpath)
} else {
newpath := filepath.Join(RECYCLEBIN, filepath.Base(oldpath))
fmt.Println("move =>", newpath)
if err := os.Rename(oldpath, newpath); err != nil {
fmt.Println(err)
}
}
}
}
func deleteFilesByName(names []string, db *bolt.DB) error {
for _, name := range names {
deleteFileByName(name)
}
ids := NamesToIds(names)
return DeleteInDB(ids, db)
}
// DeleteFilesByID 尝试删除档案,包括档案本身, metadata 以及数据库条目。
// 注意,这里说的删除是将档案移动到专案根目录的 recyclebin 中,
// 如果 recyclebin 里有同名档案则直接覆盖。
func DeleteFilesByID(ids []string, db *bolt.DB) error {
names, err := IdsToNames(ids, db)
if err != nil {
return err
}
for _, name := range names {
deleteFileByName(name)
}
return DeleteInDB(ids, db)
}
func PrintFilesSimple(files []*File) {
for _, f := range files {
size := FileSizeToString(float64(f.Size), 0)
size = fmt.Sprintf("(%s)", size)
size = PaddingRight(size, " ", 9)
fmt.Printf("%s\t%s %s\n", f.ID, size, f.Filename)
}
fmt.Println()
}
func PrintFilesIdList(files []*File) {
var ids []string
for _, f := range files {
ids = append(ids, fmt.Sprintf("\"%s\"", f.ID))
}
fmt.Println(strings.Join(ids, ", "))
}
func PrintFilesMore(files []*File) {
for _, f := range files {
size := FileSizeToString(float64(f.Size), 0)
size = fmt.Sprintf("(%s)", size)
size = PaddingRight(size, " ", 9)
fmt.Printf("%s\t%s %s\n", f.ID, size, f.Filename)
printLike(f.Like)
printLabel(f.Label)
printNotes(f.Notes)
if f.Like != 0 || f.Label+f.Notes != "" {
fmt.Println()
}
printSlice(f.Keywords, "Keywords")
printSlice(f.Collections, "Collections")
printSlice(f.Albums, "Albums")
fmt.Println()
}
}
func printLike(like int) {
for i := 0; i < like; i++ {
fmt.Print("♥")
}
if like > 0 {
fmt.Print(" ")
}
}
func printLabel(s string) {
if s == "" {
return
}
fmt.Printf("[%s] ", s)
}
func printNotes(s string) {
if s == "" {
return
}
fmt.Printf("%s", s)
}
func printSlice(s []string, name string) {
if len(s) == 0 {
return
}
joined := strings.Join(s, ", ")
fmt.Printf("%s: %s\n", name, joined)
}
func PaddingRight(s, char string, length int) string {
for {
if len(s) >= length {
return s
}
s += char
}
}
func AddToFileChecked(files []*File) error {
fcMap, err := ReadFileChecked(".")
if err != nil {
return err
}
for _, f := range files {
fc := &FileChecked{f.ID, f.CTime, false}
fcMap[f.ID] = fc
}
_, err = WriteJSON(fcMap, FileCheckedPath)
return err
}
func DeleteFromFileChecked(ids []string) error {
fcMap, err := ReadFileChecked(".")
if err != nil {
return err
}
for _, id := range ids {
delete(fcMap, id)
}
_, err = WriteJSON(fcMap, FileCheckedPath)
return err
}
func DamagedOfFileChecked(fcMap map[string]*FileChecked) (ids []string) {
for _, fc := range fcMap {
if fc.Damaged == true {
ids = append(ids, fc.ID)
}
}
return
}
func AscOrDesc(descending bool) string {
if descending {
return "descending"
}
return "ascending"
}