forked from pgaskin/BookBrowser
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbook.go
More file actions
237 lines (209 loc) · 4.62 KB
/
book.go
File metadata and controls
237 lines (209 loc) · 4.62 KB
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
package main
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"time"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/gnur/booksing/epub"
"github.com/kennygrant/sanitize"
)
var yearRemove = regexp.MustCompile(`\((1|2)[0-9]{3}\)`)
var drukRemove = regexp.MustCompile(`(?i)/ druk [0-9]+`)
var filenameSafe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
type StorageLocation string
var ErrFileAlreadyExists = errors.New("Target file already exists")
var ErrCoverWriteFailed = errors.New("Failed to write cover")
const (
FileStorage StorageLocation = "FILE"
)
// Book represents a book record in the database, regular "book" data with extra metadata
type Book struct {
Hash string
Title string
Author string
Language string
Description string
Added time.Time
Path string
Size int64
HasCover bool
CoverPath string
Publisher string
ISBN string
Series string
PublishDate time.Time
SeriesIndex float64
}
type FileLocation struct {
Path string
}
// NewBookFromFile creates a book object from a file
func NewBookFromFile(bookpath string, baseDir string) (bk *Book, err error) {
epub, cover, err := epub.ParseFile(bookpath)
if err != nil {
fmt.Println(cover)
return nil, err
}
book := Book{
Title: epub.Title,
Author: epub.Author,
Language: epub.Language,
Description: epub.Description,
HasCover: epub.HasCover,
Publisher: epub.Publisher,
ISBN: epub.ISBN,
Series: epub.Series,
PublishDate: epub.PublishDate,
SeriesIndex: epub.SeriesIndex,
}
f, err := os.Open(bookpath)
if err != nil {
return nil, err
}
fp := bookpath
fi, err := f.Stat()
if err != nil {
f.Close()
return nil, err
}
book.Added = fi.ModTime()
book.Size = fi.Size()
book.Title = Fix(book.Title, true, false)
book.Author = Fix(book.Author, true, true)
book.Language = FixLang(book.Language)
book.Description = sanitize.HTML(book.Description)
book.Hash = HashBook(book.Author, book.Title)
newBookPath := path.Join(baseDir, GetBookPath(book.Title, book.Author)+".epub")
if _, err := os.Stat(newBookPath); err == nil {
return &book, ErrFileAlreadyExists
}
baseDir = filepath.Dir(newBookPath)
err = os.MkdirAll(baseDir, 0755)
if err == nil {
_ = os.Rename(bookpath, newBookPath)
fp = newBookPath
}
book.Path = fp
if book.HasCover {
book.CoverPath = strings.Replace(fp, ".epub", ".jpg", 1)
err = os.WriteFile(book.CoverPath, cover, 0644)
if err != nil {
return &book, ErrCoverWriteFailed
}
}
return &book, nil
}
func GetBookPath(title, author string) string {
author = filenameSafe.ReplaceAllString(author, "")
title = filenameSafe.ReplaceAllString(title, "")
if len(title) > 35 {
title = title[:30]
}
title = strings.TrimSpace(title)
author = strings.TrimSpace(author)
if len(author) == 0 {
author = "unknown"
}
if len(title) == 0 {
author = "unknown"
}
parts := strings.Split(author, " ")
firstChar := parts[len(parts)-1][0:1]
formatted := fmt.Sprintf("%s/%s/%s-%s", firstChar, author, author, title)
formatted = strings.Replace(formatted, " ", "_", -1)
formatted = strings.Replace(formatted, "__", "_", -1)
return formatted
}
func FixLang(s string) string {
s = strings.ToLower(s)
switch s {
case "nld":
s = "nl"
case "dutch":
s = "nl"
case "nederlands":
s = "nl"
case "nederland":
s = "nl"
case "nl-nl":
s = "nl"
case "nl_nl":
s = "nl"
case "dut":
s = "nl"
case "deutsch":
s = "de"
case "deutsche":
s = "de"
case "duits":
s = "de"
case "german":
s = "de"
case "ger":
s = "de"
case "de-de":
s = "de"
case "de_de":
s = "de"
case "english":
s = "en"
case "engels":
s = "en"
case "eng":
s = "en"
case "uk":
s = "en"
case "en-us":
s = "en"
case "en-gb":
s = "en"
case "en-en":
s = "en"
case "en_us":
s = "en"
case "en_gb":
s = "en"
case "en_en":
s = "en"
case "us":
s = "en"
}
return s
}
func Fix(s string, capitalize, correctOrder bool) string {
if s == "" {
return "Unknown"
}
if capitalize {
c := cases.Title(language.Und, cases.NoLower)
s = c.String(s)
}
if correctOrder && strings.Contains(s, ",") {
sParts := strings.Split(s, ",")
if len(sParts) == 2 {
s = strings.TrimSpace(sParts[1]) + " " + strings.TrimSpace(sParts[0])
}
}
s = yearRemove.ReplaceAllString(s, "")
s = drukRemove.ReplaceAllString(s, "")
s = strings.Replace(s, ".", " ", -1)
s = strings.Replace(s, " ", " ", -1)
s = strings.TrimSpace(s)
return strings.Map(func(in rune) rune {
switch in {
case '“', '‹', '”', '›':
return '"'
case '_':
return ' '
case '‘', '’':
return '\''
}
return in
}, s)
}