godash/services/bookmark.services.go

161 lines
4 KiB
Go
Raw Normal View History

2024-03-12 15:49:08 +01:00
package services
import (
"io"
"log/slog"
"os"
2024-06-11 11:27:17 +02:00
"path/filepath"
2024-03-12 15:49:08 +01:00
"strings"
2024-09-30 20:16:29 +02:00
"gitlab.unjx.de/flohoss/godash/pkg/media"
2024-03-12 15:49:08 +01:00
"gopkg.in/yaml.v3"
)
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"
2025-01-31 12:08:03 +01:00
icon: "sh/github"
2024-06-11 18:36:49 +02:00
ignore_color: true
url: "https://github.com"
- name: "Home Assistant"
2025-01-31 12:08:03 +01:00
icon: "sh/home-assistant"
2024-06-11 18:36:49 +02:00
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-09-30 12:05:12 +02:00
bs.replaceIconStrings()
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
}
2024-09-30 12:05:12 +02:00
func (bs *BookmarkService) replaceIconStrings() {
2024-06-11 18:36:49 +02:00
for i, v := range bs.bookmarks.Applications {
for j, bookmark := range v.Entries {
2025-01-31 12:08:03 +01:00
var filePath, filePathLight string
var err error
2025-01-31 12:08:03 +01:00
if strings.HasPrefix(bookmark.Icon, "sh/") {
filePath, filePathLight, err = downloadIcons(handleSelfHostedIcons(bookmark.Icon, ".webp"))
2024-09-30 20:16:29 +02:00
if err != nil {
slog.Error(err.Error())
continue
2024-09-30 20:16:29 +02:00
}
2024-09-30 21:44:32 +02:00
} else {
2025-01-31 12:08:03 +01:00
ext := filepath.Ext(bookmark.Icon)
filePath, filePathLight = handleLocalIcons(bookmark.Icon, ext)
if filePath == "" {
slog.Warn("could not find local icon", "path", bookmark.Icon)
2024-09-30 21:44:32 +02:00
}
2024-03-12 15:49:08 +01:00
}
2025-01-31 12:08:03 +01:00
bs.bookmarks.Applications[i].Entries[j].Icon = filePath
bs.bookmarks.Applications[i].Entries[j].IconLight = filePathLight
}
}
}
2025-01-31 12:08:03 +01:00
func downloadIcons(title, url, lightTitle, lightUrl string) (string, string, error) {
path, err := downloadIcon(title, url)
if err != nil {
2025-01-31 12:08:03 +01:00
return "", "", err
}
2025-01-31 12:08:03 +01:00
lightPath, _ := downloadIcon(lightTitle, lightUrl)
return path, lightPath, nil
}
2025-01-31 12:08:03 +01:00
func downloadIcon(title, url string) (string, error) {
filePath := iconsFolder + title
2025-01-31 12:08:03 +01:00
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
filePath, err = media.DownloadSelfHostedIcon(url, title, filePath)
if err != nil {
2025-01-31 12:08:03 +01:00
return "", err
2024-03-12 15:49:08 +01:00
}
}
2025-01-31 12:08:03 +01:00
return "/" + strings.TrimPrefix(filePath, storageFolder), nil
}
func handleSelfHostedIcons(icon, ext string) (string, string, string, string) {
2025-01-31 12:08:03 +01:00
title := strings.Replace(icon, "sh/", "", 1) + ext
2024-09-30 22:03:58 +02:00
url := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + strings.TrimPrefix(ext, ".") + "/" + title
2025-01-31 12:08:03 +01:00
lightTitle := strings.Replace(title, ext, "-light"+ext, 1)
2024-09-30 22:03:58 +02:00
lightUrl := "https://cdn.jsdelivr.net/gh/selfhst/icons/" + strings.TrimPrefix(ext, ".") + "/" + lightTitle
return title, url, lightTitle, lightUrl
}
2025-01-31 12:08:03 +01:00
func handleLocalIcons(title, ext string) (string, string) {
filePath := iconsFolder + title
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
return "", ""
}
filePathLight := strings.Replace(title, ext, "-light"+ext, 1)
_, err = os.Stat(filePathLight)
if os.IsNotExist(err) {
return filePath, ""
2024-09-30 21:44:32 +02:00
}
2025-01-31 12:08:03 +01:00
return "/" + filePath, "/" + filePathLight
2024-09-30 21:44:32 +02:00
}
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
}