| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import type { StorageAccount |
| 7 | import { UserNotFoundError } |
| 8 | import { StorageQuotaService } |
| 9 | import type { StorageUsageDTO |
| 10 | |
| 11 | @Injectable() |
| 12 | export class StorageService |
| 13 | constructor( |
| 14 | @Inject(DATABASE) |
| 15 | private readonly quota |
| 16 | ) {} |
| 17 | |
| 18 | async usage(owner |
| 19 | const account = |
| 20 | return { |
| 21 | usedBytes: await this |
| 22 | quotaBytes: this.quota |
| 23 | maxAssetBytes: this.quota |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | /** The requester's own account, or an organization they belong to. Anything else reads as absent, so usage never tells a stranger an account exists. */ |
| 28 | private async accountNamed |
| 29 | owner: string, |
| 30 | requesterId: string, |
| 31 | ): Promise<StorageAccount |
| 32 | const [organization] |
| 33 | .select({ id: |
| 34 | .from(schema.organization |
| 35 | .innerJoin( |
| 36 | schema.member, |
| 37 | and( |
| 38 | eq(schema.member |
| 39 | eq(schema.member |
| 40 | ), |
| 41 | ) |
| 42 | .where(eq(schema |
| 43 | if (organization) return |
| 44 | |
| 45 | const [user] |
| 46 | .select({ id: |
| 47 | .from(schema.user |
| 48 | .where( |
| 49 | and(eq(schema |
| 50 | ); |
| 51 | if (user) return |
| 52 | |
| 53 | throw new UserNotFoundError( |
| 54 | } |
| 55 | } |