2024-03-12 15:49:08 +01:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-11 18:36:49 +02:00
|
|
|
"encoding/json"
|
2024-03-12 15:49:08 +01:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
2024-06-11 11:27:17 +02:00
|
|
|
"path/filepath"
|
2024-03-12 15:49:08 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2024-06-11 11:27:17 +02:00
|
|
|
const simpleIconsFolder = "node_modules/simple-icons/icons/"
|
2024-06-11 18:36:49 +02:00
|
|
|
const simpleIconsInfo = "node_modules/simple-icons/_data/simple-icons.json"
|
2024-06-11 11:27:17 +02:00
|
|
|
const storageFolder = "storage/"
|
|
|
|
const iconsFolder = storageFolder + "icons/"
|
|
|
|
const bookmarkFile = storageFolder + "bookmarks.yaml"
|
2024-03-12 15:49:08 +01:00
|
|
|
const defaultConfig = `links:
|
|
|
|
- category: "Code"
|
|
|
|
entries:
|
|
|
|
- name: "Github"
|
|
|
|
url: "https://github.com"
|
|
|
|
|
|
|
|
applications:
|
|
|
|
- category: "Code"
|
|
|
|
entries:
|
2024-06-11 18:36:49 +02:00
|
|
|
- name: "GitHub"
|
|
|
|
icon: "si/github.svg"
|
|
|
|
ignore_color: true
|
|
|
|
url: "https://github.com"
|
|
|
|
- name: "Home Assistant"
|
|
|
|
icon: "si/homeassistant.svg"
|
|
|
|
url: "https://www.home-assistant.io/"`
|
2024-03-12 15:49:08 +01:00
|
|
|
|
|
|
|
func init() {
|
2024-06-11 11:27:17 +02:00
|
|
|
folders := []string{storageFolder, iconsFolder}
|
2024-03-12 15:49:08 +01:00
|
|
|
for _, path := range folders {
|
|
|
|
err := os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slog.Debug("folders created", "folders", folders)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBookmarkService() *BookmarkService {
|
|
|
|
bs := BookmarkService{}
|
|
|
|
bs.parseBookmarks()
|
2024-06-11 18:36:49 +02:00
|
|
|
bs.parseIcons()
|
2024-03-12 15:49:08 +01:00
|
|
|
return &bs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) GetAllBookmarks() *Bookmarks {
|
|
|
|
return &bs.bookmarks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) createDefaultConfigFile() {
|
|
|
|
slog.Info("Creating default config file: " + bookmarkFile)
|
|
|
|
err := os.WriteFile(bookmarkFile, []byte(defaultConfig), 0755)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) readBookmarksFile() []byte {
|
|
|
|
file, err := os.Open(bookmarkFile)
|
|
|
|
if err != nil {
|
|
|
|
bs.createDefaultConfigFile()
|
|
|
|
file, err = os.Open(bookmarkFile)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
byteValue, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return byteValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) replaceIconString() {
|
2024-06-11 18:36:49 +02:00
|
|
|
iconsByTitle := make(map[string]string)
|
|
|
|
for _, icon := range bs.SimpleIcons.Icons {
|
|
|
|
iconsByTitle[icon.Title] = icon.Hex
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range bs.bookmarks.Applications {
|
|
|
|
for j, bookmark := range v.Entries {
|
2024-06-11 11:27:17 +02:00
|
|
|
if filepath.Ext(bookmark.Icon) == ".svg" {
|
2024-06-11 18:36:49 +02:00
|
|
|
var data []byte
|
|
|
|
var err error
|
2024-06-11 11:27:17 +02:00
|
|
|
if strings.HasPrefix(bookmark.Icon, "si/") {
|
2024-06-11 18:36:49 +02:00
|
|
|
title := strings.Replace(bookmark.Icon, "si/", "", 1)
|
|
|
|
data, err = os.ReadFile(simpleIconsFolder + title)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
color, ok := iconsByTitle[bookmark.Name]
|
2024-06-11 19:42:37 +02:00
|
|
|
if bookmark.OverwriteColor != "" {
|
|
|
|
ok = true
|
|
|
|
color = bookmark.OverwriteColor
|
|
|
|
}
|
2024-06-11 18:36:49 +02:00
|
|
|
if !(bookmark.IgnoreColor || !ok || color == "") {
|
|
|
|
data = []byte(insertColor(string(data), color))
|
|
|
|
}
|
2024-06-11 11:27:17 +02:00
|
|
|
} else {
|
|
|
|
data, err = os.ReadFile(iconsFolder + bookmark.Icon)
|
2024-06-11 18:36:49 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-11 11:27:17 +02:00
|
|
|
}
|
2024-06-12 08:58:36 +02:00
|
|
|
bs.bookmarks.Applications[i].Entries[j].Icon = insertWidthHeight(string(data))
|
2024-06-11 19:08:02 +02:00
|
|
|
} else {
|
|
|
|
bs.bookmarks.Applications[i].Entries[j].Icon = "<img title=\"" + bookmark.Name + "\" src=\"/icons/" + bookmark.Icon + "\"/>"
|
2024-03-12 15:49:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) parseBookmarks() {
|
|
|
|
byteValue := bs.readBookmarksFile()
|
|
|
|
err := yaml.Unmarshal(byteValue, &bs.bookmarks)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-06-11 18:36:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BookmarkService) parseIcons() {
|
|
|
|
file, err := os.Open(simpleIconsInfo)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
byteValue, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(byteValue, &bs.SimpleIcons)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 15:49:08 +01:00
|
|
|
bs.replaceIconString()
|
|
|
|
}
|
2024-06-11 18:36:49 +02:00
|
|
|
|
|
|
|
func insertColor(svg, color string) string {
|
|
|
|
parts := strings.SplitN(svg, "<svg", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return svg
|
|
|
|
}
|
|
|
|
return parts[0] + "<svg " + `fill="#` + color + `" ` + parts[1]
|
|
|
|
}
|
2024-06-12 08:58:36 +02:00
|
|
|
|
|
|
|
func insertWidthHeight(svg string) string {
|
|
|
|
parts := strings.SplitN(svg, "<svg", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return svg
|
|
|
|
}
|
|
|
|
return parts[0] + "<svg width=\"2rem\" height=\"2rem\" " + parts[1]
|
|
|
|
}
|