Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] Add locker usage endpoint

Neeraj Gupta8f23a91a06f9
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index f501e3ddc1..4ad4d98f14 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -672,6 +672,7 @@ func main() {
privateAPI.GET("/users/families-token", userHandler.GetFamiliesToken)
privateAPI.GET("/users/accounts-token", userHandler.GetAccountsToken)
privateAPI.GET("/users/details/v2", userHandler.GetDetailsV2)
+ privateAPI.GET("/users/locker-usage", userHandler.GetLockerUsage)
privateAPI.POST("/users/change-email", userHandler.ChangeEmail)
privateAPI.GET("/users/sessions", userHandler.GetActiveSessions)
privateAPI.DELETE("/users/session", userHandler.TerminateSession)
diff --git a/server/ente/details/lockerusage.go b/server/ente/details/lockerusage.go
new file mode 100644
index 0000000000..c64dcea9cd
--- /dev/null
+++ b/server/ente/details/lockerusage.go
@@ -0,0 +1,17 @@
+package details
+
+// LockerUsageResponse describes locker usage against the applicable locker tier
+// limits. For family users, used values reflect the combined family totals
+// because that is what locker upload enforcement checks against.
+type LockerUsageResponse struct {
+ IsPaid bool `json:"isPaid"`
+ IsFamily bool `json:"isFamily"`
+ UsedFileCount int64 `json:"usedFileCount"`
+ FileLimit int64 `json:"fileLimit"`
+ RemainingFileCount int64 `json:"remainingFileCount"`
+ UsedStorage int64 `json:"usedStorage"`
+ StorageLimit int64 `json:"storageLimit"`
+ RemainingStorage int64 `json:"remainingStorage"`
+ UserFileCount int64 `json:"userFileCount"`
+ UserStorage int64 `json:"userStorage"`
+}
diff --git a/server/pkg/api/user.go b/server/pkg/api/user.go
index 48ec68b616..28aacc3b6c 100644
--- a/server/pkg/api/user.go
+++ b/server/pkg/api/user.go
@@ -74,6 +74,18 @@ func (h *UserHandler) GetDetailsV2(c *gin.Context) {
c.JSON(http.StatusOK, details)
}
+// GetLockerUsage returns locker usage details for the requesting user.
+func (h *UserHandler) GetLockerUsage(c *gin.Context) {
+ userID := auth.GetUserID(c.Request.Header)
+
+ lockerUsage, err := h.UserController.GetLockerUsage(c, userID)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ c.JSON(http.StatusOK, lockerUsage)
+}
+
// SetAttributes sets the attributes for a user
func (h *UserHandler) SetAttributes(c *gin.Context) {
userID := auth.GetUserID(c.Request.Header)
diff --git a/server/pkg/controller/usage.go b/server/pkg/controller/usage.go
index b833571bd7..d4ba2f5ff3 100644
--- a/server/pkg/controller/usage.go
+++ b/server/pkg/controller/usage.go
@@ -35,6 +35,25 @@ const lockerPaidStorageLimit = 10 * 1024 * 1024 * 1024 // 10 GiB
const hundredMBInBytes = 100 * 1024 * 1024
+type LockerLimits struct {
+ IsPaid bool
+ FileLimit int64
+ StorageLimit int64
+}
+
+func GetLockerLimitsForTier(isPaid bool) LockerLimits {
+ limits := LockerLimits{
+ IsPaid: isPaid,
+ FileLimit: int64(lockerFreeFileLimit),
+ StorageLimit: int64(lockerFreeStorageLimit),
+ }
+ if isPaid {
+ limits.FileLimit = int64(lockerPaidFileLimit)
+ limits.StorageLimit = int64(lockerPaidStorageLimit)
+ }
+ return limits
+}
+
// CanUploadFile returns error if the file of given size (with StorageOverflowAboveSubscriptionLimit buffer) can be
// uploaded or not. If size is not passed, it validates if current usage is less than subscription storage.
func (c *UsageController) CanUploadFile(ctx context.Context, userID int64, size *int64, app ente.App) error {
@@ -124,16 +143,10 @@ func (c *UsageController) canUploadFile(ctx context.Context, userID int64, size
isPaidUser = true
}
- // Apply tiered file and storage limits
- maxFiles := int64(lockerFreeFileLimit)
- maxStorage := int64(lockerFreeStorageLimit)
- if isPaidUser {
- maxFiles = int64(lockerPaidFileLimit)
- maxStorage = int64(lockerPaidStorageLimit)
- }
+ limits := GetLockerLimitsForTier(isPaidUser)
// Check file count limit
- if lockerUsage.TotalFileCount >= maxFiles {
+ if lockerUsage.TotalFileCount >= limits.FileLimit {
return stacktrace.Propagate(&ente.ErrFileLimitReached, "")
}
@@ -142,9 +155,9 @@ func (c *UsageController) canUploadFile(ctx context.Context, userID int64, size
if size != nil {
projectedLockerUsage += *size
}
- if projectedLockerUsage >= maxStorage {
+ if projectedLockerUsage >= limits.StorageLimit {
return stacktrace.Propagate(ente.ErrStorageLimitExceeded,
- fmt.Sprintf("locker storage limit exceeded (limit %d, usage %d)", maxStorage, projectedLockerUsage))
+ fmt.Sprintf("locker storage limit exceeded (limit %d, usage %d)", limits.StorageLimit, projectedLockerUsage))
}
// Locker uploads should not be blocked by Photos subscription limits.
return nil
diff --git a/server/pkg/controller/usage_test.go b/server/pkg/controller/usage_test.go
new file mode 100644
index 0000000000..e64d3b34b4
--- /dev/null
+++ b/server/pkg/controller/usage_test.go
@@ -0,0 +1,27 @@
+package controller
+
+import "testing"
+
+func TestGetLockerLimitsForTier(t *testing.T) {
+ freeLimits := GetLockerLimitsForTier(false)
+ if freeLimits.IsPaid {
+ t.Fatal("expected free locker limits to report unpaid tier")
+ }
+ if freeLimits.FileLimit != lockerFreeFileLimit {
+ t.Fatalf("unexpected free locker file limit: %d", freeLimits.FileLimit)
+ }
+ if freeLimits.StorageLimit != lockerFreeStorageLimit {
+ t.Fatalf("unexpected free locker storage limit: %d", freeLimits.StorageLimit)
+ }
+
+ paidLimits := GetLockerLimitsForTier(true)
+ if !paidLimits.IsPaid {
+ t.Fatal("expected paid locker limits to report paid tier")
+ }
+ if paidLimits.FileLimit != lockerPaidFileLimit {
+ t.Fatalf("unexpected paid locker file limit: %d", paidLimits.FileLimit)
+ }
+ if paidLimits.StorageLimit != lockerPaidStorageLimit {
+ t.Fatalf("unexpected paid locker storage limit: %d", paidLimits.StorageLimit)
+ }
+}
diff --git a/server/pkg/controller/user/locker_usage.go b/server/pkg/controller/user/locker_usage.go
new file mode 100644
index 0000000000..8c1554f679
--- /dev/null
+++ b/server/pkg/controller/user/locker_usage.go
@@ -0,0 +1,72 @@
+package user
+
+import (
+ "github.com/ente-io/museum/ente/details"
+ "github.com/ente-io/museum/pkg/controller"
+ "github.com/ente-io/stacktrace"
+ "github.com/gin-gonic/gin"
+)
+
+func (c *UserController) GetLockerUsage(ctx *gin.Context, userID int64) (details.LockerUsageResponse, error) {
+ user, err := c.GetUser(userID)
+ if err != nil {
+ return details.LockerUsageResponse{}, stacktrace.Propagate(err, "failed to get user")
+ }
+
+ subscriptionAdminID := userID
+ subscriptionUserIDs := []int64{userID}
+ isFamily := false
+ if user.FamilyAdminID != nil {
+ isFamily = true
+ subscriptionAdminID = *user.FamilyAdminID
+
+ familyData, err := c.FamilyController.FetchMembersForAdminID(ctx, *user.FamilyAdminID)
+ if err != nil {
+ return details.LockerUsageResponse{}, stacktrace.Propagate(err, "failed to fetch family usage scope")
+ }
+
+ subscriptionUserIDs = make([]int64, 0, len(familyData.Members))
+ for _, familyMember := range familyData.Members {
+ subscriptionUserIDs = append(subscriptionUserIDs, familyMember.MemberUserID)
+ }
+ }
+
+ lockerUsage, err := c.UsageRepo.GetLockerUsage(ctx, subscriptionUserIDs)
+ if err != nil {
+ return details.LockerUsageResponse{}, stacktrace.Propagate(err, "failed to fetch locker usage")
+ }
+
+ isPaid := false
+ if err := c.BillingController.HasActiveSelfOrFamilySubscription(subscriptionAdminID, true); err == nil {
+ isPaid = true
+ }
+ limits := controller.GetLockerLimitsForTier(isPaid)
+
+ result := details.LockerUsageResponse{
+ IsPaid: limits.IsPaid,
+ IsFamily: isFamily,
+ UsedFileCount: lockerUsage.TotalFileCount,
+ FileLimit: limits.FileLimit,
+ RemainingFileCount: maxInt64(limits.FileLimit-lockerUsage.TotalFileCount, 0),
+ UsedStorage: lockerUsage.TotalUsage,
+ StorageLimit: limits.StorageLimit,
+ RemainingStorage: maxInt64(limits.StorageLimit-lockerUsage.TotalUsage, 0),
+ }
+ for _, userLockerUsage := range lockerUsage.Users {
+ if userLockerUsage.UserID != userID {
+ continue
+ }
+ result.UserFileCount = userLockerUsage.FileCount
+ result.UserStorage = userLockerUsage.Usage
+ break
+ }
+
+ return result, nil
+}
+
+func maxInt64(a, b int64) int64 {
+ if a > b {
+ return a
+ }
+ return b
+}