| 1 | import type { Readable } from 'node:stream'; |
| 2 | |
| 3 | import { type Database, |
| 4 | import { atLeast } |
| 5 | import { Inject, Injectable |
| 6 | import { and, eq |
| 7 | |
| 8 | import { DATABASE } |
| 9 | import { isoTimestamp } |
| 10 | import { |
| 11 | isValidAssetName, |
| 12 | releaseAssetKey, |
| 13 | } from '../../lib/releases/release-assets.js'; |
| 14 | import { |
| 15 | InvalidAssetNameError, |
| 16 | ReleaseAssetExistsError, |
| 17 | ReleaseAssetNotFoundError, |
| 18 | ReleaseAssetTooLargeError, |
| 19 | ReleaseNotFoundError, |
| 20 | } from '../../lib/releases/releases.errors.js'; |
| 21 | import type { AuthorizedRepository |
| 22 | import { formatByteSize } |
| 23 | import { RESERVATION_TTL } |
| 24 | import { ContentLengthRequiredError } |
| 25 | import { |
| 26 | billedKindOf, |
| 27 | storageAccountOf, |
| 28 | } from '../../lib/storage/storage-account.js'; |
| 29 | import { RepositoryAccessService } |
| 30 | import { S3Service } |
| 31 | import { StorageQuotaService } |
| 32 | |
| 33 | type RepositoryRef |
| 34 | |
| 35 | export const releaseAssetColumns = |
| 36 | id: schema.releaseAsset |
| 37 | name: schema.releaseAsset |
| 38 | contentType: schema.releaseAsset |
| 39 | size: schema.releaseAsset |
| 40 | downloadCount: schema.releaseAsset |
| 41 | createdAt: isoTimestamp(schema |
| 42 | }; |
| 43 | |
| 44 | /** Files attached to releases. Bytes stream straight from the request to object storage and back, never held in memory. */ |
| 45 | @Injectable() |
| 46 | export class ReleaseAssetsService |
| 47 | constructor( |
| 48 | @Inject(DATABASE) |
| 49 | private readonly access |
| 50 | private readonly s3 |
| 51 | private readonly quota |
| 52 | ) {} |
| 53 | |
| 54 | /** The row is written first, as a reservation against the owner's quota, and only marked uploaded once every byte is stored. */ |
| 55 | async upload({ |
| 56 | releaseId, |
| 57 | name, |
| 58 | label, |
| 59 | type, |
| 60 | contentLength, |
| 61 | body, |
| 62 | ...target |
| 63 | }: RepositoryRef |
| 64 | requesterId: string |
| 65 | releaseId: string |
| 66 | name: string |
| 67 | label?: string |
| 68 | /** What the file is served as. */ |
| 69 | type: string |
| 70 | contentLength: string |
| 71 | /** The raw request: the caller makes sure no body parser has read it. */ |
| 72 | body: Readable |
| 73 | }) { |
| 74 | const repository = |
| 75 | ...target, |
| 76 | operation: 'write', |
| 77 | }); |
| 78 | const fileName = |
| 79 | if (!isValidAssetName(fileName |
| 80 | |
| 81 | const size = |
| 82 | if (!contentLength || |
| 83 | throw new ContentLengthRequiredError() |
| 84 | } |
| 85 | if (size > |
| 86 | throw new ReleaseAssetTooLargeError( |
| 87 | formatByteSize(this.quota |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | const [release] |
| 92 | .select({ id: |
| 93 | .from(schema.release |
| 94 | .where( |
| 95 | and( |
| 96 | eq(schema.release |
| 97 | eq(schema.release |
| 98 | ), |
| 99 | ); |
| 100 | if (!release) |
| 101 | |
| 102 | const asset = |
| 103 | storageAccountOf(repository), |
| 104 | billedKindOf(repository, ' |
| 105 | size, |
| 106 | async (tx) |
| 107 | // An upload that died with its process leaves a reservation behind; once it has lapsed it must not hold the name forever. |
| 108 | await tx |
| 109 | .delete(schema.releaseAsset |
| 110 | .where( |
| 111 | and( |
| 112 | eq(schema.releaseAsset |
| 113 | eq(schema.releaseAsset |
| 114 | eq(schema.releaseAsset |
| 115 | lt( |
| 116 | schema.releaseAsset.createdAt |
| 117 | sql`now() - ${RESERVATION_TTL} |
| 118 | ), |
| 119 | ), |
| 120 | ); |
| 121 | |
| 122 | const [row] |
| 123 | .insert(schema.releaseAsset |
| 124 | .values({ |
| 125 | releaseId: release.id |
| 126 | repositoryId: repository.id |
| 127 | name: fileName, |
| 128 | label: label?.trim |
| 129 | contentType: type || |
| 130 | size, |
| 131 | uploaderId: target.requesterId |
| 132 | }) |
| 133 | .onConflictDoNothing() |
| 134 | .returning({ id: |
| 135 | if (!row) |
| 136 | return row; |
| 137 | }, |
| 138 | ); |
| 139 | |
| 140 | const key = |
| 141 | try { |
| 142 | await this.s3 |
| 143 | Key: key, |
| 144 | Body: body, |
| 145 | ContentLength: size, |
| 146 | ContentType: 'application/octet-stream', |
| 147 | }); |
| 148 | } catch (error) |
| 149 | await this.db |
| 150 | .delete(schema.releaseAsset |
| 151 | .where(eq(schema |
| 152 | throw error; |
| 153 | } |
| 154 | |
| 155 | const [uploaded] |
| 156 | .update(schema.releaseAsset |
| 157 | .set({ state: |
| 158 | .where(eq(schema |
| 159 | .returning(releaseAssetColumns); |
| 160 | // the release was deleted while the bytes were in flight, taking the row with it |
| 161 | if (!uploaded) |
| 162 | await this.s3 |
| 163 | throw new ReleaseNotFoundError() |
| 164 | } |
| 165 | return uploaded; |
| 166 | } |
| 167 | |
| 168 | /** Counts the download, then streams the bytes. A draft's assets are only for people who can write to the repository. */ |
| 169 | async download({ |
| 170 | asset: wanted, |
| 171 | ...target |
| 172 | }: RepositoryRef |
| 173 | asset: { |
| 174 | }) { |
| 175 | const repository = |
| 176 | ...target, |
| 177 | operation: 'read', |
| 178 | }); |
| 179 | |
| 180 | const [asset] |
| 181 | .update(schema.releaseAsset |
| 182 | .set({ downloadCount: |
| 183 | .from(schema.release |
| 184 | .where( |
| 185 | and( |
| 186 | eq(schema.release |
| 187 | eq(schema.release |
| 188 | 'id' in wanted |
| 189 | ? eq(schema. |
| 190 | : and( |
| 191 | eq(schema.release |
| 192 | eq(schema.releaseAsset |
| 193 | ), |
| 194 | atLeast(repository.viewerRole |
| 195 | ? undefined |
| 196 | : eq(schema. |
| 197 | eq(schema.releaseAsset |
| 198 | ), |
| 199 | ) |
| 200 | .returning({ |
| 201 | id: schema.releaseAsset |
| 202 | releaseId: schema.releaseAsset |
| 203 | name: schema.releaseAsset |
| 204 | contentType: schema.releaseAsset |
| 205 | size: schema.releaseAsset |
| 206 | }); |
| 207 | if (!asset) |
| 208 | |
| 209 | const object = |
| 210 | Bucket: this.s3 |
| 211 | Key: releaseAssetKey(repository |
| 212 | }); |
| 213 | if (!object.Body |
| 214 | |
| 215 | return { ...asset |
| 216 | } |
| 217 | |
| 218 | /** Renames an asset or sets its label; an empty label clears it. */ |
| 219 | async update({ |
| 220 | assetId, |
| 221 | name, |
| 222 | label, |
| 223 | ...target |
| 224 | }: RepositoryRef |
| 225 | requesterId: string |
| 226 | assetId: string |
| 227 | name?: string |
| 228 | label?: string |
| 229 | }) { |
| 230 | const repository = |
| 231 | ...target, |
| 232 | operation: 'write', |
| 233 | }); |
| 234 | const fileName = |
| 235 | if (fileName !== |
| 236 | throw new InvalidAssetNameError( |
| 237 | const mine = |
| 238 | eq(schema.releaseAsset |
| 239 | eq(schema.releaseAsset |
| 240 | eq(schema.releaseAsset |
| 241 | ); |
| 242 | |
| 243 | if (fileName !== |
| 244 | const [clash] |
| 245 | .select({ id: |
| 246 | .from(schema.releaseAsset |
| 247 | .where( |
| 248 | and( |
| 249 | eq( |
| 250 | schema.releaseAsset.releaseId |
| 251 | this.db |
| 252 | .select({ releaseId: |
| 253 | .from(schema.releaseAsset |
| 254 | .where(mine), |
| 255 | ), |
| 256 | eq(schema.releaseAsset |
| 257 | ne(schema.releaseAsset |
| 258 | ), |
| 259 | ); |
| 260 | if (clash) throw |
| 261 | } |
| 262 | |
| 263 | const [updated] |
| 264 | .update(schema.releaseAsset |
| 265 | .set({ |
| 266 | // always set, so an empty edit still finds the row instead of failing to build the query |
| 267 | updatedAt: new Date |
| 268 | ...(fileName !== |
| 269 | ...(label !== |
| 270 | }) |
| 271 | .where(mine) |
| 272 | .returning(releaseAssetColumns) |
| 273 | .catch((error: |
| 274 | // A rename racing another to the same name passes the check above and loses on the unique index; drizzle wraps the driver's error as its cause. |
| 275 | const cause = |
| 276 | if (fileName !== |
| 277 | throw new ReleaseAssetExistsError( |
| 278 | throw error; |
| 279 | }); |
| 280 | if (!updated) |
| 281 | return updated; |
| 282 | } |
| 283 | |
| 284 | async delete({ |
| 285 | assetId, |
| 286 | ...target |
| 287 | }: RepositoryRef |
| 288 | const repository = |
| 289 | ...target, |
| 290 | operation: 'write', |
| 291 | }); |
| 292 | |
| 293 | const [asset] |
| 294 | .select({ |
| 295 | id: schema.releaseAsset |
| 296 | releaseId: schema.releaseAsset |
| 297 | }) |
| 298 | .from(schema.releaseAsset |
| 299 | .where( |
| 300 | and( |
| 301 | eq(schema.releaseAsset |
| 302 | eq(schema.releaseAsset |
| 303 | ), |
| 304 | ); |
| 305 | if (!asset) |
| 306 | |
| 307 | // bytes first: a row left without its object is visible and can be deleted again, an object left without its row would be invisible forever |
| 308 | await this.s3 |
| 309 | Bucket: this.s3 |
| 310 | Key: releaseAssetKey(repository |
| 311 | }); |
| 312 | await this.db |
| 313 | .delete(schema.releaseAsset |
| 314 | .where(eq(schema |
| 315 | } |
| 316 | |
| 317 | /** Every asset of a release, removed from storage before the release row takes the rows with it. */ |
| 318 | removeAll(repository: |
| 319 | return this.s3 |
| 320 | } |
| 321 | } |