Manage display of book covers

This commit is contained in:
2025-10-28 18:35:36 +01:00
parent 8b8eee8210
commit b4df375e4c
20 changed files with 257 additions and 353 deletions

View File

@@ -0,0 +1,42 @@
package fileutils
import (
"mime/multipart"
"path/filepath"
"strconv"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/model"
)
func SaveStaticFile(ac *appcontext.AppContext, file *multipart.FileHeader) (model.StaticFile, error) {
filename := file.Filename
filepath := computePathFromName(ac, filename)
err := ac.C.SaveUploadedFile(file, ac.Config.ImageFolderPath+"/"+filepath)
if err != nil {
return model.StaticFile{}, err
}
staticFile := model.StaticFile{
Name: filename,
Path: filepath,
}
ac.Db.Save(&staticFile)
return staticFile, nil
}
func computePathFromName(ac *appcontext.AppContext, filename string) string {
var existingFiles []model.StaticFile
ac.Db.Where("name = ?", filename).Find(&existingFiles)
l := len(existingFiles)
if l == 0 {
return filename
} else {
extension := filepath.Ext(filename)
basename := filename[:len(filename)-len(extension)]
return basename + "-" + strconv.Itoa(l) + extension
}
}
func GetWsLinkPrefix() string {
return "/bookcover/"
}