mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add server configuration management.
This commit is contained in:
64
server/api/config.go
Normal file
64
server/api/config.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"nysoure/server/config"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
)
|
||||
|
||||
func getServerConfig(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewRequestError("You are not logged in")
|
||||
}
|
||||
isAdmin, err := service.CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Error("Error checking user admin status: ", err)
|
||||
return model.NewInternalServerError("Error checking user admin status")
|
||||
}
|
||||
if !isAdmin {
|
||||
return model.NewUnAuthorizedError("You do not have permission to access this resource")
|
||||
}
|
||||
sc := config.GetConfig()
|
||||
return c.JSON(model.Response[config.ServerConfig]{
|
||||
Success: true,
|
||||
Data: sc,
|
||||
})
|
||||
}
|
||||
|
||||
func setServerConfig(c fiber.Ctx) error {
|
||||
uid, ok := c.Locals("uid").(uint)
|
||||
if !ok {
|
||||
return model.NewRequestError("You are not logged in")
|
||||
}
|
||||
isAdmin, err := service.CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Error("Error checking user admin status: ", err)
|
||||
return model.NewInternalServerError("Error checking user admin status")
|
||||
}
|
||||
if !isAdmin {
|
||||
return model.NewUnAuthorizedError("You do not have permission to access this resource")
|
||||
}
|
||||
|
||||
var sc config.ServerConfig
|
||||
if err := c.Bind().Body(&sc); err != nil {
|
||||
return model.NewRequestError("Invalid request parameters")
|
||||
}
|
||||
|
||||
config.SetConfig(sc)
|
||||
|
||||
return c.JSON(model.Response[any]{
|
||||
Success: true,
|
||||
})
|
||||
}
|
||||
|
||||
func AddConfigRoutes(r fiber.Router) {
|
||||
configGroup := r.Group("/config")
|
||||
{
|
||||
configGroup.Get("/", getServerConfig)
|
||||
configGroup.Post("/", setServerConfig)
|
||||
}
|
||||
}
|
@@ -2,11 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func AddFileRoutes(router fiber.Router) {
|
||||
|
76
server/config/config.go
Normal file
76
server/config/config.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"nysoure/server/utils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var config *ServerConfig
|
||||
|
||||
type ServerConfig struct {
|
||||
// MaxUploadingSizeInMB is the maximum size of files that are being uploaded at the same time.
|
||||
MaxUploadingSizeInMB int `json:"max_uploading_size_in_mb"`
|
||||
// MaxFileSizeInMB is the maximum size of a single file that can be uploaded.
|
||||
MaxFileSizeInMB int `json:"max_file_size_in_mb"`
|
||||
// MaxDownloadsForSingleIP is the maximum number of downloads allowed from a single IP address.
|
||||
MaxDownloadsPerDayForSingleIP int `json:"max_downloads_per_day_for_single_ip"`
|
||||
// AllowRegister indicates whether user registration is allowed.
|
||||
AllowRegister bool `json:"allow_register"`
|
||||
// CloudflareTurnstileSiteKey is the site key for Cloudflare Turnstile.
|
||||
CloudflareTurnstileSiteKey string `json:"cloudflare_turnstile_site_key"`
|
||||
// CloudflareTurnstileSecretKey is the secret key for Cloudflare Turnstile.
|
||||
CloudflareTurnstileSecretKey string `json:"cloudflare_turnstile_secret_key"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
filepath := filepath.Join(utils.GetStoragePath(), "config.json")
|
||||
if _, err := os.Stat(filepath); os.IsNotExist(err) {
|
||||
config = &ServerConfig{
|
||||
MaxUploadingSizeInMB: 20 * 1024, // 20GB
|
||||
MaxFileSizeInMB: 8 * 1024, // 8GB
|
||||
MaxDownloadsPerDayForSingleIP: 20,
|
||||
AllowRegister: true,
|
||||
CloudflareTurnstileSiteKey: "",
|
||||
CloudflareTurnstileSecretKey: "",
|
||||
}
|
||||
} else {
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
config = &ServerConfig{}
|
||||
if err := json.Unmarshal(data, config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetConfig() ServerConfig {
|
||||
return *config
|
||||
}
|
||||
|
||||
func SetConfig(newConfig ServerConfig) {
|
||||
config = &newConfig
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
filepath := filepath.Join(utils.GetStoragePath(), "config.json")
|
||||
if err := os.WriteFile(filepath, data, 0644); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func MaxUploadingSize() int64 {
|
||||
return int64(config.MaxUploadingSizeInMB) * 1024 * 1024
|
||||
}
|
||||
|
||||
func MaxFileSize() int64 {
|
||||
return int64(config.MaxFileSizeInMB) * 1024 * 1024
|
||||
}
|
||||
|
||||
func AllowRegister() bool {
|
||||
return config.AllowRegister
|
||||
}
|
@@ -1,8 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/google/uuid"
|
||||
"nysoure/server/config"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/storage"
|
||||
@@ -11,17 +10,15 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
blockSize = 4 * 1024 * 1024 // 4MB
|
||||
)
|
||||
|
||||
var (
|
||||
maxUploadingSize = int64(1024 * 1024 * 1024 * 20) // TODO: make this configurable
|
||||
maxFileSize = int64(1024 * 1024 * 1024 * 8) // TODO: make this configurable
|
||||
)
|
||||
|
||||
func getUploadingSize(uid uint) int64 {
|
||||
return dao.GetStatistic("uploading_size")
|
||||
}
|
||||
@@ -83,12 +80,12 @@ func CreateUploadingFile(uid uint, filename string, description string, fileSize
|
||||
return nil, model.NewUnAuthorizedError("user cannot upload file")
|
||||
}
|
||||
|
||||
if fileSize > maxFileSize {
|
||||
if fileSize > config.MaxFileSize() {
|
||||
return nil, model.NewRequestError("file size exceeds the limit")
|
||||
}
|
||||
|
||||
currentUploadingSize := getUploadingSize(uid)
|
||||
if currentUploadingSize+fileSize > maxUploadingSize {
|
||||
if currentUploadingSize+fileSize > config.MaxUploadingSize() {
|
||||
log.Info("A new uploading file is rejected due to max uploading size limit")
|
||||
return nil, model.NewRequestError("server is busy, please try again later")
|
||||
}
|
||||
@@ -305,7 +302,7 @@ func DeleteFile(uid uint, fid string) error {
|
||||
return model.NewNotFoundError("file not found")
|
||||
}
|
||||
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Error("failed to check user permission: ", err)
|
||||
return model.NewInternalServerError("failed to check user permission")
|
||||
@@ -342,7 +339,7 @@ func UpdateFile(uid uint, fid string, filename string, description string) (*mod
|
||||
return nil, model.NewNotFoundError("file not found")
|
||||
}
|
||||
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Error("failed to check user permission: ", err)
|
||||
return nil, model.NewInternalServerError("failed to check user permission")
|
||||
|
@@ -89,7 +89,7 @@ func SearchResource(keyword string, page int) ([]model.ResourceView, int, error)
|
||||
}
|
||||
|
||||
func DeleteResource(uid, id uint) error {
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/storage"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
)
|
||||
|
||||
type CreateS3StorageParams struct {
|
||||
@@ -18,7 +19,7 @@ type CreateS3StorageParams struct {
|
||||
}
|
||||
|
||||
func CreateS3Storage(uid uint, params CreateS3StorageParams) error {
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Errorf("check user is admin failed: %s", err)
|
||||
return model.NewInternalServerError("check user is admin failed")
|
||||
@@ -49,7 +50,7 @@ type CreateLocalStorageParams struct {
|
||||
}
|
||||
|
||||
func CreateLocalStorage(uid uint, params CreateLocalStorageParams) error {
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Errorf("check user is admin failed: %s", err)
|
||||
return model.NewInternalServerError("check user is admin failed")
|
||||
@@ -88,7 +89,7 @@ func ListStorages() ([]model.StorageView, error) {
|
||||
}
|
||||
|
||||
func DeleteStorage(uid, id uint) error {
|
||||
isAdmin, err := checkUserIsAdmin(uid)
|
||||
isAdmin, err := CheckUserIsAdmin(uid)
|
||||
if err != nil {
|
||||
log.Errorf("check user is admin failed: %s", err)
|
||||
return model.NewInternalServerError("check user is admin failed")
|
||||
|
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"nysoure/server/config"
|
||||
"nysoure/server/dao"
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/static"
|
||||
@@ -18,6 +19,9 @@ const (
|
||||
)
|
||||
|
||||
func CreateUser(username, password string) (model.UserViewWithToken, error) {
|
||||
if !config.AllowRegister() {
|
||||
return model.UserViewWithToken{}, model.NewRequestError("User registration is not allowed")
|
||||
}
|
||||
if len(username) < 3 || len(username) > 20 {
|
||||
return model.UserViewWithToken{}, model.NewRequestError("Username must be between 3 and 20 characters")
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@ func checkUserCanUpload(uid uint) (bool, error) {
|
||||
return user.IsAdmin || user.CanUpload, nil
|
||||
}
|
||||
|
||||
func checkUserIsAdmin(uid uint) (bool, error) {
|
||||
func CheckUserIsAdmin(uid uint) (bool, error) {
|
||||
user, err := dao.GetUserByID(uid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
Reference in New Issue
Block a user