DefDrive implements a quota system that limits each user’s file count and total storage usage. These limits prevent resource abuse and ensure fair usage across all users. Limits are enforced at upload time and can be customized per user.
The upload process checks both file count and storage limits:
// From controllers/file.go:24-122func (fc *FileController) Upload(c *gin.Context) { // 1. Authenticate user userID, exists := c.Get("userID") // 2. Get uploaded file file, err := c.FormFile("file") // 3. Get user limits from database var user models.User fc.DB.First(&user, userID) // 4. Check file count limit // 5. Check storage limit // 6. Save file if within limits}
File count check uses >= - If a user has reached their limit (e.g., 100 files), they cannot upload additional files even if they have storage space remaining.
Storage check includes the new file: The system calculates currentStorage + file.Size to ensure the new file won’t cause the user to exceed their quota.
Values must be provided as pointers (*int, *int64) to distinguish between “not provided” and “zero”
No validation against current usage (admins can set limits below current usage)
Setting limits below current usage: If an admin sets MaxFiles = 50 for a user who already has 75 files, the user keeps their existing files but cannot upload new ones until they delete enough to get below the limit.
Retrieve limits and usage for all users in the system:
// From controllers/user.go:197-230func (uc *UserController) GetAllUsersLimits(c *gin.Context) { var users []models.User if err := uc.DB.Find(&users).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get users"}) return } var userLimits []gin.H for _, user := range users { // Get current file count for each user var currentFileCount int64 uc.DB.Model(&models.File{}).Where("user_id = ?", user.ID).Count(¤tFileCount) // Get current storage usage for each user var currentStorage int64 uc.DB.Model(&models.File{}).Where("user_id = ?", user.ID).Select("COALESCE(SUM(size), 0)").Scan(¤tStorage) userLimits = append(userLimits, gin.H{ "user_id": user.ID, "username": user.Username, "email": user.Email, "max_files": user.MaxFiles, "max_storage": user.MaxStorage, "current_files": currentFileCount, "current_storage": currentStorage, "remaining_files": user.MaxFiles - int(currentFileCount), "remaining_storage": user.MaxStorage - currentStorage, }) } c.JSON(http.StatusOK, gin.H{"users": userLimits})}
*DefDrive doesn’t currently implement “unlimited” quotas with negative values. To approximate unlimited access, set very high limits (e.g., 1,000,000 files and 1 TB storage).