OnlineLibrary
75 строк · 1.3 Кб
1package localstorage
2
3import (
4"errors"
5"os"
6
7"OnlineLibrary/internal/config"
8"OnlineLibrary/internal/content"
9
10"github.com/leonelquinteros/gotext"
11"gitverse.ru/kvark128/dodp"
12)
13
14type LocalStorage struct {
15path string
16conf *config.Config
17}
18
19func NewLocalStorage(conf *config.Config) *LocalStorage {
20return &LocalStorage{
21path: config.UserData(),
22conf: conf,
23}
24}
25
26func (s *LocalStorage) ContentList(string) (*content.List, error) {
27entrys, err := os.ReadDir(s.path)
28if err != nil {
29return nil, err
30}
31
32lst := &content.List{
33ID: dodp.Issued,
34Name: gotext.Get("Local books"),
35}
36
37for _, e := range entrys {
38if e.IsDir() {
39item := NewContentItem(s, e.Name())
40lst.Items = append(lst.Items, item)
41}
42}
43
44return lst, nil
45}
46
47func (s *LocalStorage) LastContentListID() (string, error) {
48return dodp.Issued, nil
49}
50
51func (s *LocalStorage) ContentItem(id string) (content.Item, error) {
52lst, err := s.ContentList("")
53if err != nil {
54return nil, err
55}
56
57for _, item := range lst.Items {
58if item.ID() == id {
59return item, nil
60}
61}
62return nil, errors.New("content item not found")
63}
64
65func (s *LocalStorage) LastContentItemID() (string, error) {
66book, err := s.conf.LocalBooks.LastBook()
67if err != nil {
68return "", err
69}
70return book.ID, nil
71}
72
73func (s *LocalStorage) Tidy(ids []string) {
74s.conf.LocalBooks.Tidy(ids)
75}
76