mirror of
https://github.com/haiwen/seafile-server.git
synced 2025-09-19 10:03:40 +00:00
load default quota when start (#472)
* load default quota when start * improve code
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -37,6 +38,14 @@ var seafileDB, ccnetDB *sql.DB
|
|||||||
// when SQLite is used, user and group db are separated.
|
// when SQLite is used, user and group db are separated.
|
||||||
var userDB, groupDB *sql.DB
|
var userDB, groupDB *sql.DB
|
||||||
|
|
||||||
|
// Storage unit.
|
||||||
|
const (
|
||||||
|
KB = 1000
|
||||||
|
MB = 1000000
|
||||||
|
GB = 1000000000
|
||||||
|
TB = 1000000000000
|
||||||
|
)
|
||||||
|
|
||||||
type fileServerOptions struct {
|
type fileServerOptions struct {
|
||||||
host string
|
host string
|
||||||
port uint32
|
port uint32
|
||||||
@@ -52,6 +61,7 @@ type fileServerOptions struct {
|
|||||||
windowsEncoding string
|
windowsEncoding string
|
||||||
// Timeout for fs-id-list requests.
|
// Timeout for fs-id-list requests.
|
||||||
fsIDListRequestTimeout uint32
|
fsIDListRequestTimeout uint32
|
||||||
|
defaultQuota int64
|
||||||
}
|
}
|
||||||
|
|
||||||
var options fileServerOptions
|
var options fileServerOptions
|
||||||
@@ -132,8 +142,7 @@ func loadCcnetDB() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadSeafileDB() {
|
func loadSeafileDB() {
|
||||||
var seafileConfPath string
|
seafileConfPath := filepath.Join(centralDir, "seafile.conf")
|
||||||
seafileConfPath = filepath.Join(centralDir, "seafile.conf")
|
|
||||||
|
|
||||||
config, err := ini.Load(seafileConfPath)
|
config, err := ini.Load(seafileConfPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -203,9 +212,50 @@ func loadSeafileDB() {
|
|||||||
dbType = dbEngine
|
dbType = dbEngine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseQuota(quotaStr string) int64 {
|
||||||
|
var quota int64
|
||||||
|
var multiplier int64 = GB
|
||||||
|
if end := strings.Index(quotaStr, "kb"); end > 0 {
|
||||||
|
multiplier = KB
|
||||||
|
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return InfiniteQuota
|
||||||
|
}
|
||||||
|
quota = quotaInt * multiplier
|
||||||
|
} else if end := strings.Index(quotaStr, "mb"); end > 0 {
|
||||||
|
multiplier = MB
|
||||||
|
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return InfiniteQuota
|
||||||
|
}
|
||||||
|
quota = quotaInt * multiplier
|
||||||
|
} else if end := strings.Index(quotaStr, "gb"); end > 0 {
|
||||||
|
multiplier = GB
|
||||||
|
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return InfiniteQuota
|
||||||
|
}
|
||||||
|
quota = quotaInt * multiplier
|
||||||
|
} else if end := strings.Index(quotaStr, "tb"); end > 0 {
|
||||||
|
multiplier = TB
|
||||||
|
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return InfiniteQuota
|
||||||
|
}
|
||||||
|
quota = quotaInt * multiplier
|
||||||
|
} else {
|
||||||
|
quotaInt, err := strconv.ParseInt(quotaStr, 10, 0)
|
||||||
|
if err != nil {
|
||||||
|
return InfiniteQuota
|
||||||
|
}
|
||||||
|
quota = quotaInt * multiplier
|
||||||
|
}
|
||||||
|
|
||||||
|
return quota
|
||||||
|
}
|
||||||
|
|
||||||
func loadFileServerOptions() {
|
func loadFileServerOptions() {
|
||||||
var seafileConfPath string
|
seafileConfPath := filepath.Join(centralDir, "seafile.conf")
|
||||||
seafileConfPath = filepath.Join(centralDir, "seafile.conf")
|
|
||||||
|
|
||||||
config, err := ini.Load(seafileConfPath)
|
config, err := ini.Load(seafileConfPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -225,6 +275,13 @@ func loadFileServerOptions() {
|
|||||||
parseFileServerSection(section)
|
parseFileServerSection(section)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if section, err := config.GetSection("quota"); err == nil {
|
||||||
|
if key, err := section.GetKey("default"); err == nil {
|
||||||
|
quotaStr := key.String()
|
||||||
|
options.defaultQuota = parseQuota(quotaStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ccnetConfPath := filepath.Join(centralDir, "ccnet.conf")
|
ccnetConfPath := filepath.Join(centralDir, "ccnet.conf")
|
||||||
config, err = ini.Load(ccnetConfPath)
|
config, err = ini.Load(ccnetConfPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -282,6 +339,7 @@ func initDefaultOptions() {
|
|||||||
options.maxIndexingThreads = 1
|
options.maxIndexingThreads = 1
|
||||||
options.webTokenExpireTime = 7200
|
options.webTokenExpireTime = 7200
|
||||||
options.clusterSharedTempFileMode = 0600
|
options.clusterSharedTempFileMode = 0600
|
||||||
|
options.defaultQuota = InfiniteQuota
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@@ -3,11 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/haiwen/seafile-server/fileserver/repomgr"
|
"github.com/haiwen/seafile-server/fileserver/repomgr"
|
||||||
"gopkg.in/ini.v1"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// InfiniteQuota indicates that the quota is unlimited.
|
// InfiniteQuota indicates that the quota is unlimited.
|
||||||
@@ -73,83 +70,12 @@ func getUserQuota(user string) (int64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if quota <= 0 {
|
if quota <= 0 {
|
||||||
quota = getDefaultQuota()
|
quota = options.defaultQuota
|
||||||
}
|
}
|
||||||
|
|
||||||
return quota, nil
|
return quota, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Storage unit.
|
|
||||||
const (
|
|
||||||
KB = 1000
|
|
||||||
MB = 1000000
|
|
||||||
GB = 1000000000
|
|
||||||
TB = 1000000000000
|
|
||||||
)
|
|
||||||
|
|
||||||
func getDefaultQuota() int64 {
|
|
||||||
seafileConfPath := filepath.Join(absDataDir, "seafile.conf")
|
|
||||||
config, err := ini.Load(seafileConfPath)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
var quota int64
|
|
||||||
section, err := config.GetSection("quota")
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
key, err := section.GetKey("default")
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quotaStr := key.String()
|
|
||||||
quota = parseQuota(quotaStr)
|
|
||||||
|
|
||||||
return quota
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseQuota(quotaStr string) int64 {
|
|
||||||
var quota int64
|
|
||||||
var multiplier int64 = GB
|
|
||||||
if end := strings.Index(quotaStr, "kb"); end > 0 {
|
|
||||||
multiplier = KB
|
|
||||||
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quota = quotaInt * multiplier
|
|
||||||
} else if end := strings.Index(quotaStr, "mb"); end > 0 {
|
|
||||||
multiplier = MB
|
|
||||||
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quota = quotaInt * multiplier
|
|
||||||
} else if end := strings.Index(quotaStr, "gb"); end > 0 {
|
|
||||||
multiplier = GB
|
|
||||||
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quota = quotaInt * multiplier
|
|
||||||
} else if end := strings.Index(quotaStr, "tb"); end > 0 {
|
|
||||||
multiplier = TB
|
|
||||||
quotaInt, err := strconv.ParseInt(quotaStr[:end], 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quota = quotaInt * multiplier
|
|
||||||
} else {
|
|
||||||
quotaInt, err := strconv.ParseInt(quotaStr, 10, 0)
|
|
||||||
if err != nil {
|
|
||||||
return InfiniteQuota
|
|
||||||
}
|
|
||||||
quota = quotaInt * multiplier
|
|
||||||
}
|
|
||||||
|
|
||||||
return quota
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUserUsage(user string) (int64, error) {
|
func getUserUsage(user string) (int64, error) {
|
||||||
var usage sql.NullInt64
|
var usage sql.NullInt64
|
||||||
sqlStr := "SELECT SUM(size) FROM " +
|
sqlStr := "SELECT SUM(size) FROM " +
|
||||||
|
Reference in New Issue
Block a user