( userId: string, additionalBytes: number )
| 166 | * Always allows uploads when billing is disabled |
| 167 | */ |
| 168 | export async function checkStorageQuota( |
| 169 | userId: string, |
| 170 | additionalBytes: number |
| 171 | ): Promise<{ allowed: boolean; currentUsage: number; limit: number; error?: string }> { |
| 172 | if (!isBillingEnabled) { |
| 173 | return { |
| 174 | allowed: true, |
| 175 | currentUsage: 0, |
| 176 | limit: Number.MAX_SAFE_INTEGER, |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | try { |
| 181 | const [currentUsage, limit] = await Promise.all([ |
| 182 | getUserStorageUsage(userId), |
| 183 | getUserStorageLimit(userId), |
| 184 | ]) |
| 185 | |
| 186 | const newUsage = currentUsage + additionalBytes |
| 187 | const allowed = newUsage <= limit |
| 188 | |
| 189 | return { |
| 190 | allowed, |
| 191 | currentUsage, |
| 192 | limit, |
| 193 | error: allowed |
| 194 | ? undefined |
| 195 | : `Storage limit exceeded. Used: ${(newUsage / (1024 * 1024 * 1024)).toFixed(2)}GB, Limit: ${(limit / (1024 * 1024 * 1024)).toFixed(0)}GB`, |
| 196 | } |
| 197 | } catch (error) { |
| 198 | logger.error('Error checking storage quota:', error) |
| 199 | return { |
| 200 | allowed: false, |
| 201 | currentUsage: 0, |
| 202 | limit: 0, |
| 203 | error: 'Failed to check storage quota', |
| 204 | } |
| 205 | } |
| 206 | } |
no test coverage detected