Bilder & Vorschau / Tabellen für die Satrtseite

This commit is contained in:
Simon Martens
2025-02-11 00:13:42 +01:00
parent deae787e2d
commit 0f87df8e94
13 changed files with 412 additions and 128 deletions

View File

@@ -4,49 +4,16 @@ import (
"net/http"
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/router"
"github.com/pocketbase/pocketbase/tools/types"
)
const INDEX_NAME = "index"
const BILDER_T_NAME = "bilder"
const TEXTE_T_NAME = "texte"
var bilder_fields = core.NewFieldsList(
&core.TextField{Name: "Titel", Required: true, Presentable: true},
&core.EditorField{Name: "Beschreibung", Required: false, Presentable: false},
&core.FileField{
Name: "Bilder",
Required: false,
MaxSize: 100 * 1024 * 1024,
MaxSelect: 1000,
MimeTypes: dbmodels.MUSENALM_MIME_TYPES,
Thumbs: []string{"0x300", "0x500", "0x1000", "300x0", "500x0", "1000x0"},
}, // 100 MB a file
&core.FileField{
Name: "Vorschau",
Required: false,
MaxSize: 100 * 1024 * 1024,
MaxSelect: 1000,
MimeTypes: dbmodels.MUSENALM_MIME_TYPES,
Thumbs: []string{"0x300", "0x500", "0x1000", "300x0", "500x0", "1000x0"},
}, // 100 MB a file
)
var texte_fields = core.NewFieldsList(
&core.TextField{Name: "Titel", Required: true, Presentable: true},
&core.EditorField{Name: "Abs1", Required: false, Presentable: false},
&core.EditorField{Name: "Abs2", Required: false, Presentable: false},
)
func init() {
ip := &IndexPage{
Page: pagemodels.Page{
Name: INDEX_NAME,
Name: pagemodels.P_INDEX_NAME,
},
}
app.Register(ip)
@@ -57,49 +24,13 @@ type IndexPage struct {
}
func (p *IndexPage) Up(app core.App) error {
if !p.TableExists(app, BILDER_T_NAME) {
err := p.CreateTable(app, p.bilderCollection())
if err != nil {
return err
}
}
if !p.TableExists(app, TEXTE_T_NAME) {
err := p.CreateTable(app, p.texteCollection())
if err != nil {
return err
}
}
return nil
}
func (p *IndexPage) Down(app core.App) error {
err := p.DropTable(app, BILDER_T_NAME)
if err != nil {
return err
}
err = p.DropTable(app, TEXTE_T_NAME)
if err != nil {
return err
}
return nil
}
func (p *IndexPage) bilderCollection() *core.Collection {
c := core.NewBaseCollection(BILDER_T_NAME)
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.Fields = bilder_fields
return c
}
func (p *IndexPage) texteCollection() *core.Collection {
c := core.NewBaseCollection(TEXTE_T_NAME)
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.Fields = texte_fields
return c
}
func (p *IndexPage) Setup(router *router.Router[*core.RequestEvent], app core.App, engine *templating.Engine) error {
router.GET("/{$}", func(e *core.RequestEvent) error {
return e.String(http.StatusOK, "Hello, World!")

View File

@@ -0,0 +1,86 @@
package migrations_index
import (
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/tools/types"
)
var bilder_fields = core.NewFieldsList(
&core.TextField{Name: pagemodels.F_INDEX_BILDER_TITEL, Required: true, Presentable: true},
&core.EditorField{Name: pagemodels.F_INDEX_BILDER_BESCHREIBUNG, Required: false, Presentable: false},
&core.FileField{
Name: pagemodels.F_INDEX_BILDER_BILD,
Required: true,
MaxSize: 100 * 1024 * 1024,
MaxSelect: 1000,
MimeTypes: dbmodels.MUSENALM_MIME_TYPES,
Thumbs: []string{"0x300", "0x500", "0x1000", "300x0", "500x0", "1000x0"},
}, // 100 MB a file
&core.FileField{
Name: pagemodels.F_INDEX_BILDER_VORSCHAU,
Required: true,
MaxSize: 100 * 1024 * 1024,
MaxSelect: 1000,
MimeTypes: dbmodels.MUSENALM_MIME_TYPES,
Thumbs: []string{"0x300", "0x500", "0x1000", "300x0", "500x0", "1000x0"},
}, // 100 MB a file
)
var texte_fields = core.NewFieldsList(
&core.TextField{Name: pagemodels.F_INDEX_TEXTE_TITEL, Required: true, Presentable: true},
&core.EditorField{Name: pagemodels.F_INDEX_TEXTE_ABS1, Required: false, Presentable: false},
&core.EditorField{Name: pagemodels.F_INDEX_TEXTE_ABS2, Required: false, Presentable: false},
)
func init() {
m.Register(func(app core.App) error {
collection_b := bilderCollection()
if err := app.Save(collection_b); err != nil {
return err
}
collection_t := texteCollection()
if err := app.Save(collection_t); err != nil {
return err
}
return nil
}, func(app core.App) error {
collection_b, err := app.FindCollectionByNameOrId(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_BILDER))
if err == nil && collection_b != nil {
if err := app.Delete(collection_b); err != nil {
return err
}
}
collection_t, err := app.FindCollectionByNameOrId(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_TEXTE))
if err == nil && collection_t != nil {
if err := app.Delete(collection_t); err != nil {
return err
}
}
return nil
})
}
func bilderCollection() *core.Collection {
c := core.NewBaseCollection(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_BILDER))
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.Fields = bilder_fields
return c
}
func texteCollection() *core.Collection {
c := core.NewBaseCollection(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_TEXTE))
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.Fields = texte_fields
return c
}

View File

@@ -0,0 +1,142 @@
package migrations_index
import (
"bufio"
"log"
"os"
"path/filepath"
"strings"
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
"github.com/Theodor-Springmann-Stiftung/musenalm/xmlmodels"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/tools/filesystem"
)
func init() {
m.Register(func(app core.App) error {
images := readImages(app, xmlmodels.STATIC_IMG_PATH, xmlmodels.BESCHREIBUNGEN_FN)
for _, image := range images {
if err := app.Save(image); err != nil {
app.Logger().Error("Failed to save image:", "error", err, "image", image)
}
}
return nil
}, func(app core.App) error {
collection, err := app.FindCollectionByNameOrId(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_BILDER))
if err == nil && collection != nil {
app.DB().NewQuery("DELETE FROM " + collection.TableName()).Execute()
}
return nil
})
}
func readDescriptions(collection *core.Collection, filePath string) (map[string]*pagemodels.IndexBilder, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
images := make(map[string]*pagemodels.IndexBilder)
var filename string
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "# ") {
filename = strings.TrimPrefix(line, "# ")
images[filename] = pagemodels.NewIndexBilder(core.NewRecord(collection))
} else if strings.HasPrefix(line, "## ") {
title := strings.TrimPrefix(line, "## ")
images[filename].SetTitel(title)
} else if strings.HasPrefix(line, "### ") {
beschr := strings.TrimPrefix(line, "### ")
images[filename].SetBeschreibung(beschr)
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return images, nil
}
func readImages(app core.App, path, description_fn string) []*pagemodels.IndexBilder {
ret := make([]*pagemodels.IndexBilder, 0)
collection, err := app.FindCollectionByNameOrId(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_BILDER))
if err != nil {
app.Logger().Error("Could not find Table Bilder! You need to execute table migrations first!")
return ret
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return ret
}
descriptionPath := filepath.Join(path, description_fn)
images, err := readDescriptions(collection, descriptionPath)
if err != nil {
app.Logger().Error("Failed to read descriptions file:", "error", err)
app.Logger().Info("Proceeding without descriptions")
return ret
}
e := func(path string, fileInfo os.FileInfo, inpErr error) (err error) {
name := fileInfo.Name()
titleWithoutExt := strings.TrimSuffix(name, filepath.Ext(name))
if !fileInfo.IsDir() &&
(strings.HasSuffix(name, ".png") ||
strings.HasSuffix(name, ".jpg") ||
strings.HasSuffix(name, ".jpeg")) {
if strings.HasSuffix(strings.TrimSuffix(name, filepath.Ext(name)), "-vorschau") {
return nil
}
info, exists := images[name]
if exists {
info.SetTitel(titleWithoutExt)
} else {
fn := strings.TrimSuffix(name, "-hintergrund"+filepath.Ext(name))
info, exists = images[fn]
if exists {
info.SetTitel(titleWithoutExt)
} else {
return nil
}
}
f, err := filesystem.NewFileFromPath(path)
if err != nil {
app.Logger().Error("Failed to create file from path:", "error", err)
return nil
}
info.SetBild(f)
previewName := strings.TrimSuffix(name, filepath.Ext(name)) + "-vorschau" + filepath.Ext(name)
previewPath := filepath.Join(filepath.Dir(path), previewName)
if _, err := os.Stat(previewPath); err == nil {
previewFile, err := filesystem.NewFileFromPath(previewPath)
if err != nil {
log.Println(err)
return nil
}
info.SetVorschau(previewFile)
ret = append(ret, info)
}
}
return nil
}
if err := filepath.Walk(path, e); err != nil {
app.Logger().Error("Failed to walk path:", "error", err)
}
return ret
}

View File

@@ -1 +0,0 @@
package pages