Get real mailbox size from dovecot

- Add mail.GetQuota() using doveadm quota get
- Fix UserWithQuota response struct
- Fix frontend NaN display with Unknown fallback
- Update field names to camelCase (usedQuota)
This commit is contained in:
Christoph Haas 2026-03-23 00:24:40 +01:00
parent 16b6d9df60
commit c5a9149220
3 changed files with 68 additions and 5 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/imc-vibe/backend/internal/db"
"github.com/imc-vibe/backend/internal/mail"
)
type UserHandler struct {
@ -30,6 +31,13 @@ type UpdateUserRequest struct {
Quota int64 `json:"quota,omitempty"`
}
type UserWithQuota struct {
ID uint `json:"id"`
Email string `json:"email"`
Quota int64 `json:"quota"`
UsedQuota *int64 `json:"usedQuota"`
}
func (h *UserHandler) List(c *gin.Context) {
domainName := c.Param("name")
if domainName == "" {
@ -61,7 +69,22 @@ func (h *UserHandler) List(c *gin.Context) {
return
}
Success(c, users)
result := make([]UserWithQuota, len(users))
for i, user := range users {
result[i] = UserWithQuota{
ID: user.ID,
Email: user.Email,
Quota: user.Quota,
}
quota, err := mail.GetQuota(user.Email)
if err == nil && quota != nil {
result[i].Quota = quota.Limit
result[i].UsedQuota = &quota.Used
}
}
Success(c, result)
}
func (h *UserHandler) Get(c *gin.Context) {

View file

@ -0,0 +1,40 @@
package mail
import (
"bufio"
"os/exec"
"regexp"
"strconv"
"strings"
)
type Quota struct {
Used int64 `json:"used"` // Used storage in bytes
Limit int64 `json:"limit"` // Storage limit in bytes (-1 = unlimited)
}
func GetQuota(email string) (*Quota, error) {
cmd := exec.Command("/usr/bin/doveadm", "quota", "get", "-u", email)
output, err := cmd.Output()
if err != nil {
return nil, err
}
return parseDoveadmQuota(output)
}
var quotaLineRegex = regexp.MustCompile(`STORAGE\s+(\d+)\s+(\d+)`)
func parseDoveadmQuota(output []byte) (*Quota, error) {
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
line := scanner.Text()
matches := quotaLineRegex.FindStringSubmatch(line)
if len(matches) == 3 {
used, _ := strconv.ParseInt(matches[1], 10, 64)
limit, _ := strconv.ParseInt(matches[2], 10, 64)
return &Quota{Used: used, Limit: limit}, nil
}
}
return nil, nil
}