OnlineLibrary

Форк
0
75 строк · 1.3 Кб
1
package localstorage
2

3
import (
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

14
type LocalStorage struct {
15
	path string
16
	conf *config.Config
17
}
18

19
func NewLocalStorage(conf *config.Config) *LocalStorage {
20
	return &LocalStorage{
21
		path: config.UserData(),
22
		conf: conf,
23
	}
24
}
25

26
func (s *LocalStorage) ContentList(string) (*content.List, error) {
27
	entrys, err := os.ReadDir(s.path)
28
	if err != nil {
29
		return nil, err
30
	}
31

32
	lst := &content.List{
33
		ID:   dodp.Issued,
34
		Name: gotext.Get("Local books"),
35
	}
36

37
	for _, e := range entrys {
38
		if e.IsDir() {
39
			item := NewContentItem(s, e.Name())
40
			lst.Items = append(lst.Items, item)
41
		}
42
	}
43

44
	return lst, nil
45
}
46

47
func (s *LocalStorage) LastContentListID() (string, error) {
48
	return dodp.Issued, nil
49
}
50

51
func (s *LocalStorage) ContentItem(id string) (content.Item, error) {
52
	lst, err := s.ContentList("")
53
	if err != nil {
54
		return nil, err
55
	}
56

57
	for _, item := range lst.Items {
58
		if item.ID() == id {
59
			return item, nil
60
		}
61
	}
62
	return nil, errors.New("content item not found")
63
}
64

65
func (s *LocalStorage) LastContentItemID() (string, error) {
66
	book, err := s.conf.LocalBooks.LastBook()
67
	if err != nil {
68
		return "", err
69
	}
70
	return book.ID, nil
71
}
72

73
func (s *LocalStorage) Tidy(ids []string) {
74
	s.conf.LocalBooks.Tidy(ids)
75
}
76

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.