| 1 | import type { Readable } from 'node:stream'; |
| 2 | import { type Database, |
| 3 | import { atLeast } |
| 4 | import { Inject, Injectable |
| 5 | import { and, eq |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import type { |
| 9 | AuthorizedRepository, |
| 10 | RepositoryOperation, |
| 11 | } from '../../lib/git/repository-access/repository-access.js'; |
| 12 | import { |
| 13 | isValidAssetName, |
| 14 | releaseAssetKey, |
| 15 | } from '../../lib/releases/release-assets.js'; |
| 16 | import { formatByteSize } |
| 17 | import { storageAccountOf } |
| 18 | import { RepositoryAccessService } |
| 19 | import { S3Service } |
| 20 | import { StorageQuotaService } |
| 21 | import { isoTimestamp } |
| 22 | import { |
| 23 | ContentLengthRequiredError, |
| 24 | InvalidAssetNameError, |
| 25 | ReleaseAssetExistsError, |
| 26 | ReleaseAssetNotFoundError, |
| 27 | ReleaseAssetTooLargeError, |
| 28 | ReleaseNotFoundError, |
| 29 | UploadNotOctetStreamError, |
| 30 | } from './releases.errors.js'; |
| 31 | |
| 32 | type RepositoryRef |
| 33 | |
| 34 | export const releaseAssetColumns = |
| 35 | id: schema.releaseAsset |
| 36 | name: schema.releaseAsset |
| 37 | contentType: schema.releaseAsset |
| 38 | size: schema.releaseAsset |
| 39 | downloadCount: schema.releaseAsset |
| 40 | createdAt: isoTimestamp(schema |
| 41 | }; |
| 42 | |
| 43 | /** Files attached to releases. Bytes stream straight from the request to object storage and back, never held in memory. */ |
| 44 | @Injectable() |
| 45 | export class ReleaseAssetsService |
| 46 | constructor( |
| 47 | @Inject(DATABASE) |
| 48 | private readonly access |
| 49 | private readonly s3 |
| 50 | private readonly quota |
| 51 | ) {} |
| 52 | |
| 53 | /** The row is written first, as a reservation against the owner's quota, and only marked uploaded once every byte is stored. */ |
| 54 | async upload({ |
| 55 | releaseId, |
| 56 | name, |
| 57 | type, |
| 58 | bodyType, |
| 59 | contentLength, |
| 60 | body, |
| 61 | ...target |
| 62 | }: RepositoryRef |
| 63 | requesterId: string |
| 64 | releaseId: string |
| 65 | name: string |
| 66 | /** What the file is served as. */ |
| 67 | type: string |
| 68 | /** The request's own `Content-Type`. */ |
| 69 | bodyType: string |
| 70 | contentLength: string |
| 71 | body: Readable |
| 72 | }) { |
| 73 | const repository = |
| 74 | // JSON and form bodies are parsed before any handler runs, so their bytes are gone by the time they could be streamed |
| 75 | if (bodyType?.split |
| 76 | throw new UploadNotOctetStreamError() |
| 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 | size, |
| 105 | async (tx) |
| 106 | // An upload that died with its process leaves a reservation behind; once it has lapsed it must not hold the name forever. |
| 107 | await tx |
| 108 | .delete(schema.releaseAsset |
| 109 | .where( |
| 110 | and( |
| 111 | eq(schema.releaseAsset |
| 112 | eq(schema.releaseAsset |
| 113 | eq(schema.releaseAsset |
| 114 | lt(schema.releaseAsset |
| 115 | ), |
| 116 | ); |
| 117 | |
| 118 | const [row] |
| 119 | .insert(schema.releaseAsset |
| 120 | .values({ |
| 121 | releaseId: release.id |
| 122 | repositoryId: repository.id |
| 123 | name: fileName, |
| 124 | contentType: type || |
| 125 | size, |
| 126 | uploaderId: target.requesterId |
| 127 | }) |
| 128 | .onConflictDoNothing() |
| 129 | .returning({ id: |
| 130 | if (!row) |
| 131 | return row; |
| 132 | }, |
| 133 | ); |
| 134 | |
| 135 | const key = |
| 136 | try { |
| 137 | await this.s3 |
| 138 | Key: key, |
| 139 | Body: body, |
| 140 | ContentLength: size, |
| 141 | ContentType: 'application/octet-stream', |
| 142 | }); |
| 143 | } catch (error) |
| 144 | await this.db |
| 145 | .delete(schema.releaseAsset |
| 146 | .where(eq(schema |
| 147 | throw error; |
| 148 | } |
| 149 | |
| 150 | const [uploaded] |
| 151 | .update(schema.releaseAsset |
| 152 | .set({ state: |
| 153 | .where(eq(schema |
| 154 | .returning(releaseAssetColumns); |
| 155 | // the release was deleted while the bytes were in flight, taking the row with it |
| 156 | if (!uploaded) |
| 157 | await this.s3 |
| 158 | throw new ReleaseNotFoundError() |
| 159 | } |
| 160 | return uploaded; |
| 161 | } |
| 162 | |
| 163 | /** Counts the download, then streams the bytes. A draft's assets are only for people who can write to the repository. */ |
| 164 | async download({ |
| 165 | tagName, |
| 166 | name, |
| 167 | ...target |
| 168 | }: RepositoryRef |
| 169 | const repository = |
| 170 | |
| 171 | const [asset] |
| 172 | .update(schema.releaseAsset |
| 173 | .set({ downloadCount: |
| 174 | .from(schema.release |
| 175 | .where( |
| 176 | and( |
| 177 | eq(schema.release |
| 178 | eq(schema.release |
| 179 | eq(schema.release |
| 180 | atLeast(repository.viewerRole |
| 181 | ? undefined |
| 182 | : eq(schema. |
| 183 | eq(schema.releaseAsset |
| 184 | eq(schema.releaseAsset |
| 185 | ), |
| 186 | ) |
| 187 | .returning({ |
| 188 | id: schema.releaseAsset |
| 189 | releaseId: schema.releaseAsset |
| 190 | name: schema.releaseAsset |
| 191 | contentType: schema.releaseAsset |
| 192 | size: schema.releaseAsset |
| 193 | }); |
| 194 | if (!asset) |
| 195 | |
| 196 | const object = |
| 197 | Bucket: this.s3 |
| 198 | Key: releaseAssetKey(repository |
| 199 | }); |
| 200 | if (!object.Body |
| 201 | |
| 202 | return { ...asset |
| 203 | } |
| 204 | |
| 205 | async delete({ |
| 206 | assetId, |
| 207 | ...target |
| 208 | }: RepositoryRef |
| 209 | const repository = |
| 210 | |
| 211 | const [asset] |
| 212 | .select({ |
| 213 | id: schema.releaseAsset |
| 214 | releaseId: schema.releaseAsset |
| 215 | }) |
| 216 | .from(schema.releaseAsset |
| 217 | .where( |
| 218 | and( |
| 219 | eq(schema.releaseAsset |
| 220 | eq(schema.releaseAsset |
| 221 | ), |
| 222 | ); |
| 223 | if (!asset) |
| 224 | |
| 225 | // 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 |
| 226 | await this.s3 |
| 227 | Bucket: this.s3 |
| 228 | Key: releaseAssetKey(repository |
| 229 | }); |
| 230 | await this.db |
| 231 | .delete(schema.releaseAsset |
| 232 | .where(eq(schema |
| 233 | } |
| 234 | |
| 235 | /** Every asset of a release, removed from storage before the release row takes the rows with it. */ |
| 236 | removeAll(repository: |
| 237 | return this.s3 |
| 238 | } |
| 239 | |
| 240 | private authorize( |
| 241 | { username, repo |
| 242 | operation: RepositoryOperation |
| 243 | ) { |
| 244 | return this.access |
| 245 | username, |
| 246 | repo, |
| 247 | actor: requesterId ? |
| 248 | operation, |
| 249 | }); |
| 250 | } |
| 251 | } |