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:
parent
16b6d9df60
commit
c5a9149220
3 changed files with 68 additions and 5 deletions
40
backend/internal/mail/quota.go
Normal file
40
backend/internal/mail/quota.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue