mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add api for pinned resources
This commit is contained in:
@@ -48,7 +48,13 @@ func setServerConfig(c fiber.Ctx) error {
|
|||||||
return model.NewRequestError("Invalid request parameters")
|
return model.NewRequestError("Invalid request parameters")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.SetConfig(sc)
|
if err := config.SetConfig(sc); err != nil {
|
||||||
|
return model.NewInternalServerError("Failed to save configuration")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := sc.Validate(); err != nil {
|
||||||
|
return model.NewRequestError(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return c.JSON(model.Response[any]{
|
return c.JSON(model.Response[any]{
|
||||||
Success: true,
|
Success: true,
|
||||||
|
@@ -267,6 +267,21 @@ func handleGetRandomResource(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetPinnedResources(c fiber.Ctx) error {
|
||||||
|
views, err := service.GetPinnedResources()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if views == nil {
|
||||||
|
views = []model.ResourceView{}
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(model.Response[[]model.ResourceView]{
|
||||||
|
Success: true,
|
||||||
|
Data: views,
|
||||||
|
Message: "Pinned resources retrieved successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func AddResourceRoutes(api fiber.Router) {
|
func AddResourceRoutes(api fiber.Router) {
|
||||||
resource := api.Group("/resource")
|
resource := api.Group("/resource")
|
||||||
{
|
{
|
||||||
@@ -279,5 +294,6 @@ func AddResourceRoutes(api fiber.Router) {
|
|||||||
resource.Get("/tag/:tag", handleListResourcesWithTag)
|
resource.Get("/tag/:tag", handleListResourcesWithTag)
|
||||||
resource.Get("/user/:username", handleGetResourcesWithUser)
|
resource.Get("/user/:username", handleGetResourcesWithUser)
|
||||||
resource.Post("/:id", handleUpdateResource)
|
resource.Post("/:id", handleUpdateResource)
|
||||||
|
resource.Get("/pinned", handleGetPinnedResources)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"nysoure/server/utils"
|
"nysoure/server/utils"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -34,6 +35,30 @@ type ServerConfig struct {
|
|||||||
MaxNormalUserUploadSizeInMB int `json:"max_normal_user_upload_size_in_mb"`
|
MaxNormalUserUploadSizeInMB int `json:"max_normal_user_upload_size_in_mb"`
|
||||||
// Prompt for upload page
|
// Prompt for upload page
|
||||||
UploadPrompt string `json:"upload_prompt"`
|
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() {
|
func init() {
|
||||||
@@ -51,6 +76,7 @@ func init() {
|
|||||||
AllowNormalUserUpload: true,
|
AllowNormalUserUpload: true,
|
||||||
MaxNormalUserUploadSizeInMB: 16,
|
MaxNormalUserUploadSizeInMB: 16,
|
||||||
UploadPrompt: "You can upload your files here.",
|
UploadPrompt: "You can upload your files here.",
|
||||||
|
PinnedResources: []uint{},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
data, err := os.ReadFile(p)
|
data, err := os.ReadFile(p)
|
||||||
@@ -68,16 +94,17 @@ func GetConfig() ServerConfig {
|
|||||||
return *config
|
return *config
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetConfig(newConfig ServerConfig) {
|
func SetConfig(newConfig ServerConfig) error {
|
||||||
config = &newConfig
|
config = &newConfig
|
||||||
data, err := json.MarshalIndent(config, "", " ")
|
data, err := json.MarshalIndent(config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
p := filepath.Join(utils.GetStoragePath(), "config.json")
|
p := filepath.Join(utils.GetStoragePath(), "config.json")
|
||||||
if err := os.WriteFile(p, data, 0644); err != nil {
|
if err := os.WriteFile(p, data, 0644); err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MaxUploadingSize() int64 {
|
func MaxUploadingSize() int64 {
|
||||||
@@ -127,3 +154,7 @@ func MaxNormalUserUploadSize() int64 {
|
|||||||
func UploadPrompt() string {
|
func UploadPrompt() string {
|
||||||
return config.UploadPrompt
|
return config.UploadPrompt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PinnedResources() []uint {
|
||||||
|
return config.PinnedResources
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"nysoure/server/config"
|
||||||
"nysoure/server/dao"
|
"nysoure/server/dao"
|
||||||
"nysoure/server/model"
|
"nysoure/server/model"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -286,3 +287,16 @@ func RandomResource(host string) (*model.ResourceDetailView, error) {
|
|||||||
}
|
}
|
||||||
return &v, nil
|
return &v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPinnedResources() ([]model.ResourceView, error) {
|
||||||
|
ids := config.PinnedResources()
|
||||||
|
var views []model.ResourceView
|
||||||
|
for _, id := range ids {
|
||||||
|
r, err := dao.GetResourceByID(id)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
views = append(views, r.ToView())
|
||||||
|
}
|
||||||
|
return views, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user