mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-28 04:27:24 +00:00
Add server configuration management.
This commit is contained in:
@@ -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