Add api for pinned resources

This commit is contained in:
2025-08-28 10:38:55 +08:00
parent f3b3f2bd5a
commit 77ad261670
4 changed files with 71 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package config
import (
"encoding/json"
"errors"
"nysoure/server/utils"
"os"
"path/filepath"
@@ -34,6 +35,30 @@ type ServerConfig struct {
MaxNormalUserUploadSizeInMB int `json:"max_normal_user_upload_size_in_mb"`
// Prompt for upload page
UploadPrompt string `json:"upload_prompt"`
// PinnedResources is a list of resource IDs that are pinned to the top of the page.
PinnedResources []uint `json:"pinned_resources"`
}
func (c *ServerConfig) Validate() error {
if c.MaxUploadingSizeInMB <= 0 {
return errors.New("MaxUploadingSizeInMB must be positive")
}
if c.MaxFileSizeInMB <= 0 {
return errors.New("MaxFileSizeInMB must be positive")
}
if c.MaxDownloadsPerDayForSingleIP <= 0 {
return errors.New("MaxDownloadsPerDayForSingleIP must be positive")
}
if c.ServerName == "" {
return errors.New("ServerName must not be empty")
}
if c.ServerDescription == "" {
return errors.New("ServerDescription must not be empty")
}
if len(c.PinnedResources) > 8 {
return errors.New("PinnedResources must not exceed 8 items")
}
return nil
}
func init() {
@@ -51,6 +76,7 @@ func init() {
AllowNormalUserUpload: true,
MaxNormalUserUploadSizeInMB: 16,
UploadPrompt: "You can upload your files here.",
PinnedResources: []uint{},
}
} else {
data, err := os.ReadFile(p)
@@ -68,16 +94,17 @@ func GetConfig() ServerConfig {
return *config
}
func SetConfig(newConfig ServerConfig) {
func SetConfig(newConfig ServerConfig) error {
config = &newConfig
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(err)
return err
}
p := filepath.Join(utils.GetStoragePath(), "config.json")
if err := os.WriteFile(p, data, 0644); err != nil {
panic(err)
return err
}
return nil
}
func MaxUploadingSize() int64 {
@@ -127,3 +154,7 @@ func MaxNormalUserUploadSize() int64 {
func UploadPrompt() string {
return config.UploadPrompt
}
func PinnedResources() []uint {
return config.PinnedResources
}