2023-06-14 21:53:27 +02:00
|
|
|
package bookmarks
|
|
|
|
|
|
|
|
import (
|
2023-06-21 17:19:21 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-06-14 21:53:27 +02:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
folderCreate "github.com/unjx-de/go-folder"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const StorageDir = "storage/"
|
|
|
|
const IconsDir = StorageDir + "icons/"
|
2023-06-21 17:19:21 +02:00
|
|
|
const bookmarksFolder = "internal/bookmarks/"
|
2023-06-14 21:53:27 +02:00
|
|
|
const configFile = "config.yaml"
|
|
|
|
|
2023-06-21 17:19:21 +02:00
|
|
|
func NewBookmarkService() *Config {
|
|
|
|
c := Config{}
|
2023-06-14 21:53:27 +02:00
|
|
|
c.createFolderStructure()
|
|
|
|
c.copyDefaultConfigIfNotExisting()
|
|
|
|
c.parseBookmarks()
|
|
|
|
go c.watchBookmarks()
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) createFolderStructure() {
|
|
|
|
folders := []string{StorageDir, IconsDir}
|
|
|
|
err := folderCreate.CreateFolders(folders, 0755)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Fatal("cannot create folder", zap.Error(err))
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Debug("folders created", zap.Strings("folders", folders))
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) copyDefaultConfigIfNotExisting() {
|
|
|
|
_, err := os.Open(StorageDir + configFile)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Debug(configFile + " not existing, creating...")
|
2023-06-14 21:53:27 +02:00
|
|
|
source, _ := os.Open(bookmarksFolder + configFile)
|
|
|
|
defer source.Close()
|
|
|
|
destination, err := os.Create(StorageDir + configFile)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
return
|
|
|
|
}
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Debug(configFile + " created")
|
2023-06-14 21:53:27 +02:00
|
|
|
} else {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Debug(configFile + " existing, skipping creation")
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) readBookmarksFile() []byte {
|
|
|
|
file, err := os.Open(StorageDir + configFile)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
byteValue, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return byteValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) replaceIconString() {
|
|
|
|
for _, v := range c.Parsed.Applications {
|
|
|
|
for i, bookmark := range v.Entries {
|
|
|
|
if !strings.Contains(bookmark.Icon, "http") {
|
|
|
|
v.Entries[i].Icon = "/" + IconsDir + bookmark.Icon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) parseBookmarks() {
|
|
|
|
byteValue := c.readBookmarksFile()
|
|
|
|
err := yaml.Unmarshal(byteValue, &c.Parsed)
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.replaceIconString()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) watchBookmarks() {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _ = <-watcher.Events:
|
|
|
|
c.parseBookmarks()
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Debug("bookmarks changed", zap.Int("applications", len(c.Parsed.Applications)), zap.Int("links", len(c.Parsed.Links)))
|
2023-06-14 21:53:27 +02:00
|
|
|
case err := <-watcher.Errors:
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Error(err.Error())
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := watcher.Add(StorageDir + configFile); err != nil {
|
2023-06-21 17:19:21 +02:00
|
|
|
zap.L().Fatal("cannot add watcher")
|
2023-06-14 21:53:27 +02:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
}
|