add error handling for reading configuration #1

Merged
arya merged 9 commits from odyseusz/publapi:master into master 2024-11-04 20:20:30 +05:30
Showing only changes of commit 4b871b50c6 - Show all commits

View File

@ -75,7 +75,7 @@ func userData(username, usersonline, ops string) UserInfo {
}
crdstr := strings.TrimSuffix(string(crd), "\n")
filename := "/home/" + username + "/meta-info.toml"
_, error := os.Stat(filename)
filestat, error := os.Stat(filename)
if error != nil {
if os.IsNotExist(error) {
UserError("user doesn't have a meta-info.toml", username, error)
@ -95,6 +95,23 @@ func userData(username, usersonline, ops string) UserInfo {
return user
}
}
// Check file size of meta-info.toml. If it's over 100 kB, bail
if filestat.Size() > 100_000 {
log.Error("user's meta-info.toml is above the 100 kB file size limit", "user", username)
user := UserInfo{
Name: username,
Online: false,
Op: false,
}
user.Created, _ = strconv.Atoi(crdstr)
if isonline {
user.Online = true
}
if isop {
user.Op = true
}
return user
}
viper.SetConfigFile(filename)
if err := viper.ReadInConfig(); err != nil {
log.Error("message", "couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username)