| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { and, eq |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import type { Executor |
| 8 | import { |
| 9 | DEFAULT_RELEASE_ASSET_MAX_BYTES, |
| 10 | RELEASE_ASSET_CEILING, |
| 11 | } from '../../lib/releases/release-assets.js'; |
| 12 | import { parseByteSize } |
| 13 | import { |
| 14 | billedTo, |
| 15 | type StorageAccount, |
| 16 | storageAccountKey, |
| 17 | } from '../../lib/storage/storage-account.js'; |
| 18 | import { StorageQuotaExceededError } |
| 19 | |
| 20 | /** How long an `uploading` reservation counts before it is taken for an upload that died with its process. */ |
| 21 | const RESERVATION_TTL = sql |
| 22 | |
| 23 | /** |
| 24 | * Bytes an account may keep in object storage. `STORAGE_QUOTA_BYTES` sets one limit for every account; unset, nothing is limited, which is what a self-hosted instance gets by default. |
| 25 | * |
| 26 | * Usage is summed from the rows that hold the files, never kept as a counter, so it cannot drift from what is stored and follows a repository when it is transferred. |
| 27 | */ |
| 28 | @Injectable() |
| 29 | export class StorageQuotaService |
| 30 | private readonly quota |
| 31 | /** Largest single release asset, from `RELEASE_ASSET_MAX_BYTES`; 2 GB unless set, and never past what one upload can carry. */ |
| 32 | readonly maxAssetBytes: |
| 33 | |
| 34 | constructor( |
| 35 | @Inject(DATABASE) |
| 36 | config: ConfigService |
| 37 | ) { |
| 38 | this.quota = |
| 39 | config.get<string |
| 40 | 'STORAGE_QUOTA_BYTES', |
| 41 | ); |
| 42 | this.maxAssetBytes = |
| 43 | parseByteSize( |
| 44 | config.get<string |
| 45 | 'RELEASE_ASSET_MAX_BYTES', |
| 46 | ) ?? DEFAULT_RELEASE_ASSET_MAX_BYTES, |
| 47 | RELEASE_ASSET_CEILING, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** The limit for `account`, or null for none. Per-account overrides, such as a paid plan, belong here. */ |
| 52 | quotaOf(_account: |
| 53 | return this.quota |
| 54 | } |
| 55 | |
| 56 | async usageOf(account |
| 57 | const [row] |
| 58 | .select({ |
| 59 | used: sql<string |
| 60 | }) |
| 61 | .from(schema.releaseAsset |
| 62 | .innerJoin( |
| 63 | schema.repository, |
| 64 | eq(schema.repository |
| 65 | ) |
| 66 | .where( |
| 67 | and( |
| 68 | billedTo(account), |
| 69 | or( |
| 70 | eq(schema.releaseAsset |
| 71 | gt(schema.releaseAsset |
| 72 | ), |
| 73 | ), |
| 74 | ); |
| 75 | return Number(row |
| 76 | } |
| 77 | |
| 78 | /** Runs `insert`, which must write the row that holds `bytes`, only if the account has room for them. Reservations for one account are serialized, so two uploads cannot both fit into the last free space. */ |
| 79 | reserve<T> |
| 80 | account: StorageAccount |
| 81 | bytes: number, |
| 82 | insert: (tx |
| 83 | ) { |
| 84 | return this.db |
| 85 | const quota = |
| 86 | if (quota !== |
| 87 | await tx.execute |
| 88 | sql`select pg_advisory_xact_lock(hashtext(${storageAccountKey( |
| 89 | ); |
| 90 | const used = |
| 91 | if (used + |
| 92 | throw new StorageQuotaExceededError( |
| 93 | } |
| 94 | } |
| 95 | return insert(tx |
| 96 | }); |
| 97 | } |
| 98 | } |