rui_orig/resources.go

433 lines
9.8 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"embed"
"io"
"net/http"
"os"
"path/filepath"
2022-04-23 18:13:35 +03:00
"sort"
2021-09-07 17:36:50 +03:00
"strconv"
"strings"
)
const (
imageDir = "images"
themeDir = "themes"
viewDir = "views"
rawDir = "raw"
stringsDir = "strings"
)
type scaledImage struct {
path string
scale float64
}
type imagePath struct {
path string
fs *embed.FS
}
type resourceManager struct {
embedFS []*embed.FS
themes map[string]Theme
2021-09-07 17:36:50 +03:00
images map[string]imagePath
imageSrcSets map[string][]scaledImage
path string
}
var resources = resourceManager{
embedFS: []*embed.FS{},
themes: map[string]Theme{},
2021-09-07 17:36:50 +03:00
images: map[string]imagePath{},
imageSrcSets: map[string][]scaledImage{},
}
func AddEmbedResources(fs *embed.FS) {
resources.embedFS = append(resources.embedFS, fs)
2024-05-20 16:51:30 +03:00
rootDirs := resources.embedRootDirs(fs)
2021-09-07 17:36:50 +03:00
for _, dir := range rootDirs {
switch dir {
case imageDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedImagesDir(fs, dir, "")
2021-09-07 17:36:50 +03:00
case themeDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedThemesDir(fs, dir)
2021-09-07 17:36:50 +03:00
case stringsDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedStringsDir(fs, dir)
2021-09-07 17:36:50 +03:00
case viewDir, rawDir:
// do nothing
default:
if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files {
if file.IsDir() {
switch file.Name() {
case imageDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedImagesDir(fs, dir+"/"+imageDir, "")
2021-09-07 17:36:50 +03:00
case themeDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedThemesDir(fs, dir+"/"+themeDir)
2021-09-07 17:36:50 +03:00
case stringsDir:
2024-05-20 16:51:30 +03:00
resources.scanEmbedStringsDir(fs, dir+"/"+stringsDir)
2021-09-07 17:36:50 +03:00
case viewDir, rawDir:
// do nothing
}
}
}
}
}
}
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) embedRootDirs(fs *embed.FS) []string {
2021-09-07 17:36:50 +03:00
result := []string{}
if files, err := fs.ReadDir("."); err == nil {
for _, file := range files {
if file.IsDir() {
result = append(result, file.Name())
}
}
}
return result
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) scanEmbedThemesDir(fs *embed.FS, dir string) {
2021-09-07 17:36:50 +03:00
if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files {
name := file.Name()
path := dir + "/" + name
if file.IsDir() {
2024-05-20 16:51:30 +03:00
resources.scanEmbedThemesDir(fs, path)
2021-09-07 17:36:50 +03:00
} else if strings.ToLower(filepath.Ext(name)) == ".rui" {
if data, err := fs.ReadFile(path); err == nil {
2024-05-20 16:51:30 +03:00
resources.registerThemeText(string(data))
2021-09-07 17:36:50 +03:00
}
}
}
}
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) scanEmbedImagesDir(fs *embed.FS, dir, prefix string) {
2021-09-07 17:36:50 +03:00
if files, err := fs.ReadDir(dir); err == nil {
for _, file := range files {
name := file.Name()
path := dir + "/" + name
if file.IsDir() {
2024-05-20 16:51:30 +03:00
resources.scanEmbedImagesDir(fs, path, prefix+name+"/")
2021-09-07 17:36:50 +03:00
} else {
ext := strings.ToLower(filepath.Ext(name))
switch ext {
2023-04-20 09:16:16 +03:00
case ".png", ".jpg", ".jpeg", ".svg", ".gif", ".bmp", ".webp":
2024-05-20 16:51:30 +03:00
resources.registerImage(fs, path, prefix+name)
2021-09-07 17:36:50 +03:00
}
}
}
}
}
func invalidImageFileFormat(filename string) {
ErrorLog(`Invalid image file name parameters: "` + filename +
`". Image file name format: name[@x-param].ext (examples: icon.png, icon@1.5x.png)`)
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) registerImage(fs *embed.FS, path, filename string) {
2021-09-07 17:36:50 +03:00
resources.images[filename] = imagePath{fs: fs, path: path}
start := strings.LastIndex(filename, "@")
if start < 0 {
return
}
ext := strings.LastIndex(filename, ".")
if start > ext || filename[ext-1] != 'x' {
invalidImageFileFormat(path)
return
}
if scale, err := strconv.ParseFloat(filename[start+1:ext-1], 32); err == nil {
key := filename[:start] + filename[ext:]
images, ok := resources.imageSrcSets[key]
if ok {
for _, image := range images {
if image.scale == scale {
return
}
}
} else {
images = []scaledImage{}
}
resources.imageSrcSets[key] = append(images, scaledImage{path: filename, scale: scale})
} else {
invalidImageFileFormat(path)
return
}
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) scanImagesDirectory(path, filePrefix string) {
2022-08-07 18:59:56 +03:00
if files, err := os.ReadDir(path); err == nil {
2021-09-07 17:36:50 +03:00
for _, file := range files {
filename := file.Name()
if filename[0] != '.' {
newPath := path + `/` + filename
if !file.IsDir() {
2024-05-20 16:51:30 +03:00
resources.registerImage(nil, newPath, filePrefix+filename)
2021-09-07 17:36:50 +03:00
} else {
2024-05-20 16:51:30 +03:00
resources.scanImagesDirectory(newPath, filePrefix+filename+"/")
2021-09-07 17:36:50 +03:00
}
}
}
} else {
ErrorLog(err.Error())
}
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) scanThemesDir(path string) {
2022-08-07 18:59:56 +03:00
if files, err := os.ReadDir(path); err == nil {
2021-09-07 17:36:50 +03:00
for _, file := range files {
filename := file.Name()
if filename[0] != '.' {
newPath := path + `/` + filename
if file.IsDir() {
2024-05-20 16:51:30 +03:00
resources.scanThemesDir(newPath)
2021-09-07 17:36:50 +03:00
} else if strings.ToLower(filepath.Ext(newPath)) == ".rui" {
2022-08-07 18:59:56 +03:00
if data, err := os.ReadFile(newPath); err == nil {
2024-05-20 16:51:30 +03:00
resources.registerThemeText(string(data))
2021-09-07 17:36:50 +03:00
} else {
ErrorLog(err.Error())
}
}
}
}
} else {
ErrorLog(err.Error())
}
}
// SetResourcePath set path of the resource directory
func SetResourcePath(path string) {
resources.path = path
pathLen := len(path)
if pathLen > 0 && path[pathLen-1] != '/' {
resources.path += "/"
}
2024-05-20 16:51:30 +03:00
resources.scanImagesDirectory(resources.path+imageDir, "")
resources.scanThemesDir(resources.path + themeDir)
resources.scanStringsDir(resources.path + stringsDir)
2021-09-07 17:36:50 +03:00
}
2024-05-20 16:51:30 +03:00
func (resources *resourceManager) scanDefaultResourcePath() {
if exe, err := os.Executable(); err == nil {
path := filepath.Dir(exe) + "/resources/"
resources.scanImagesDirectory(path+imageDir, "")
resources.scanThemesDir(path + themeDir)
resources.scanStringsDir(path + stringsDir)
}
}
func (resources *resourceManager) registerThemeText(text string) bool {
theme, ok := CreateThemeFromText(text)
if !ok {
2021-09-07 17:36:50 +03:00
return false
}
name := theme.Name()
if name == "" {
defaultTheme.Append(theme)
} else if t, ok := resources.themes[name]; ok {
t.Append(theme)
2021-09-07 17:36:50 +03:00
} else {
resources.themes[name] = theme
2021-09-07 17:36:50 +03:00
}
return true
}
func serveResourceFile(filename string, w http.ResponseWriter, r *http.Request) bool {
serveEmbed := func(fs *embed.FS, path string) bool {
if file, err := fs.Open(path); err == nil {
if stat, err := file.Stat(); err == nil {
http.ServeContent(w, r, filename, stat.ModTime(), file.(io.ReadSeeker))
return true
}
}
return false
}
if image, ok := resources.images[filename]; ok {
if image.fs != nil {
if serveEmbed(image.fs, image.path) {
return true
}
} else {
if _, err := os.Stat(image.path); err == nil {
http.ServeFile(w, r, image.path)
return true
}
}
}
for _, fs := range resources.embedFS {
if serveEmbed(fs, filename) {
return true
}
2024-05-20 16:51:30 +03:00
for _, dir := range resources.embedRootDirs(fs) {
2021-09-07 17:36:50 +03:00
if serveEmbed(fs, dir+"/"+filename) {
return true
}
2023-04-20 09:16:16 +03:00
if subDirs, err := fs.ReadDir(dir); err == nil {
for _, subdir := range subDirs {
2021-09-07 17:36:50 +03:00
if subdir.IsDir() {
if serveEmbed(fs, dir+"/"+subdir.Name()+"/"+filename) {
return true
}
}
}
}
}
}
serve := func(path, filename string) bool {
filepath := path + filename
if _, err := os.Stat(filepath); err == nil {
http.ServeFile(w, r, filepath)
return true
}
filepath = path + imageDir + "/" + filename
if _, err := os.Stat(filepath); err == nil {
http.ServeFile(w, r, filepath)
return true
}
return false
}
if resources.path != "" && serve(resources.path, filename) {
return true
}
if exe, err := os.Executable(); err == nil {
path := filepath.Dir(exe) + "/resources/"
if serve(path, filename) {
return true
}
}
return false
}
2024-05-20 16:51:30 +03:00
// ReadRawResource returns the contents of the raw resource with the specified name
2021-09-07 17:36:50 +03:00
func ReadRawResource(filename string) []byte {
for _, fs := range resources.embedFS {
2024-05-20 16:51:30 +03:00
rootDirs := resources.embedRootDirs(fs)
2021-09-07 17:36:50 +03:00
for _, dir := range rootDirs {
switch dir {
case imageDir, themeDir, viewDir:
// do nothing
case rawDir:
if data, err := fs.ReadFile(dir + "/" + filename); err == nil {
return data
}
default:
if data, err := fs.ReadFile(dir + "/" + rawDir + "/" + filename); err == nil {
return data
}
}
}
}
if resources.path != "" {
2022-07-19 18:22:19 +03:00
if data, err := os.ReadFile(resources.path + rawDir + "/" + filename); err == nil {
2021-09-07 17:36:50 +03:00
return data
}
}
if exe, err := os.Executable(); err == nil {
2022-07-19 18:22:19 +03:00
if data, err := os.ReadFile(filepath.Dir(exe) + "/resources/" + rawDir + "/" + filename); err == nil {
2021-09-07 17:36:50 +03:00
return data
}
}
2022-07-19 18:22:19 +03:00
ErrorLogF(`The "%s" raw file don't found`, filename)
2021-09-07 17:36:50 +03:00
return nil
}
2024-05-20 16:51:30 +03:00
// AllRawResources returns the list of all raw resouces
2021-09-07 17:36:50 +03:00
func AllRawResources() []string {
result := []string{}
for _, fs := range resources.embedFS {
2024-05-20 16:51:30 +03:00
rootDirs := resources.embedRootDirs(fs)
2021-09-07 17:36:50 +03:00
for _, dir := range rootDirs {
switch dir {
case imageDir, themeDir, viewDir:
// do nothing
case rawDir:
if files, err := fs.ReadDir(rawDir); err == nil {
for _, file := range files {
result = append(result, file.Name())
}
}
default:
if files, err := fs.ReadDir(dir + "/" + rawDir); err == nil {
for _, file := range files {
result = append(result, file.Name())
}
}
}
}
}
if resources.path != "" {
2022-08-07 18:59:56 +03:00
if files, err := os.ReadDir(resources.path + rawDir); err == nil {
2021-09-07 17:36:50 +03:00
for _, file := range files {
result = append(result, file.Name())
}
}
}
if exe, err := os.Executable(); err == nil {
2022-08-07 18:59:56 +03:00
if files, err := os.ReadDir(filepath.Dir(exe) + "/resources/" + rawDir); err == nil {
2021-09-07 17:36:50 +03:00
for _, file := range files {
result = append(result, file.Name())
}
}
}
return result
}
2022-04-21 18:22:17 +03:00
2024-05-20 16:51:30 +03:00
// AllImageResources returns the list of all image resouces
2022-04-21 18:22:17 +03:00
func AllImageResources() []string {
result := make([]string, 0, len(resources.images))
for image := range resources.images {
result = append(result, image)
}
2022-04-23 18:13:35 +03:00
sort.Strings(result)
2022-04-21 18:22:17 +03:00
return result
}
func AddTheme(theme Theme) {
if theme != nil {
name := theme.Name()
if name == "" {
defaultTheme.Append(theme)
} else if t, ok := resources.themes[name]; ok {
t.Append(theme)
} else {
resources.themes[name] = theme
}
}
}