@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/README.md +2 -1
  2. package/dist/bin/server.mjs +1 -1
  3. package/dist/bin/server.mjs.map +1 -1
  4. package/dist/bin/services.mjs +1 -1
  5. package/dist/bin/services.mjs.map +1 -1
  6. package/dist/client.d.ts +296 -27
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +649 -295
  9. package/dist/client.js.map +1 -1
  10. package/dist/contract.d.ts +2 -2
  11. package/dist/contract.js +2 -2
  12. package/dist/contract2.js +331 -21
  13. package/dist/contract2.js.map +1 -1
  14. package/dist/email.d.ts +1773 -32
  15. package/dist/email.d.ts.map +1 -1
  16. package/dist/email.js +3478 -288
  17. package/dist/email.js.map +1 -1
  18. package/dist/errors.d.ts +2 -0
  19. package/dist/errors.js +48 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +466 -13
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index2.d.ts +35 -0
  24. package/dist/index2.d.ts.map +1 -0
  25. package/dist/listen.mjs +447 -290
  26. package/dist/listen.mjs.map +1 -1
  27. package/dist/runtime-cloudflare.d.ts +89 -0
  28. package/dist/runtime-cloudflare.d.ts.map +1 -0
  29. package/dist/runtime-cloudflare.js +15667 -0
  30. package/dist/runtime-cloudflare.js.map +1 -0
  31. package/dist/runtime-node.d.mts +2963 -294
  32. package/dist/runtime-node.d.mts.map +1 -1
  33. package/dist/server.d.ts +3473 -842
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +12187 -8244
  36. package/dist/server.js.map +1 -1
  37. package/dist/server.mjs +7739 -3548
  38. package/dist/server.mjs.map +1 -1
  39. package/dist/server2.mjs +5 -5
  40. package/dist/server2.mjs.map +1 -1
  41. package/package.json +22 -5
  42. package/src/admin/audit/service.ts +2 -2
  43. package/src/admin/documents/service.ts +76 -6
  44. package/src/admin/reports/service.ts +7 -1
  45. package/src/client/api.ts +23 -471
  46. package/src/client/auth/client.ts +22 -3
  47. package/src/client/billing.ts +80 -0
  48. package/src/client/community.ts +40 -0
  49. package/src/client/discovery.ts +35 -9
  50. package/src/client/documents.ts +160 -0
  51. package/src/client/index.ts +7 -0
  52. package/src/client/portal/account.ts +54 -2
  53. package/src/client/portal/request.ts +2 -0
  54. package/src/client/response.ts +21 -0
  55. package/src/client/schema.ts +8 -0
  56. package/src/client/services.ts +23 -18
  57. package/src/client/sharing.ts +262 -0
  58. package/src/client/versions.ts +104 -0
  59. package/src/contract/account.ts +50 -0
  60. package/src/contract/billing.ts +166 -0
  61. package/src/contract/collaboration.ts +5 -0
  62. package/src/contract/community.ts +60 -0
  63. package/src/contract/discovery.ts +63 -2
  64. package/src/contract/entitlements.ts +10 -0
  65. package/src/contract/index.ts +54 -5
  66. package/src/contract/links.ts +57 -4
  67. package/src/contract/locale.ts +27 -0
  68. package/src/contract/portal.ts +12 -1
  69. package/src/contract/routes.ts +10 -0
  70. package/src/contract/services.ts +18 -16
  71. package/src/email/i18n.ts +174 -0
  72. package/src/email/index.ts +13 -2
  73. package/src/email/locales/ar.json +141 -0
  74. package/src/email/locales/de.json +141 -0
  75. package/src/email/locales/es.json +141 -0
  76. package/src/email/locales/fr.json +141 -0
  77. package/src/email/locales/it.json +141 -0
  78. package/src/email/locales/ja.json +141 -0
  79. package/src/email/locales/pl.json +141 -0
  80. package/src/email/locales/pt-br.json +141 -0
  81. package/src/email/locales/ru.json +141 -0
  82. package/src/email/locales/zh-cn.json +141 -0
  83. package/src/email/messages.ts +200 -0
  84. package/src/email/render.ts +142 -276
  85. package/src/email/samples.ts +115 -0
  86. package/src/email/schema.ts +70 -6
  87. package/src/email/templates/BrandEmail.vue +54 -0
  88. package/src/email/templates/DocumentBlocked.vue +45 -0
  89. package/src/email/templates/DocumentInvitation.vue +24 -0
  90. package/src/email/templates/DocumentReport.vue +32 -0
  91. package/src/email/templates/DocumentReportDecided.vue +36 -0
  92. package/src/email/templates/DocumentReportReceived.vue +18 -0
  93. package/src/email/templates/DocumentRestored.vue +24 -0
  94. package/src/email/templates/EmailChangeRequested.vue +24 -0
  95. package/src/email/templates/EmailRoot.vue +19 -0
  96. package/src/email/templates/EmailText.vue +9 -0
  97. package/src/email/templates/EmailVerification.vue +38 -0
  98. package/src/email/templates/EnrollmentNotification.vue +24 -0
  99. package/src/email/templates/EnrollmentUpdate.vue +52 -0
  100. package/src/email/templates/NewSignIn.vue +37 -0
  101. package/src/email/templates/PasswordChanged.vue +22 -0
  102. package/src/email/templates/PasswordReset.vue +23 -0
  103. package/src/email/templates/StorageLimitReached.vue +28 -0
  104. package/src/email/templates/TwoStepChanged.vue +32 -0
  105. package/src/email/templates/WaitlistConfirmation.vue +29 -0
  106. package/src/email/templates/WorkspaceInvitation.vue +28 -0
  107. package/src/email/templates/brand.ts +9 -61
  108. package/src/email/templates/localization.ts +17 -0
  109. package/src/errors/index.ts +6 -0
  110. package/src/global.d.ts +7 -0
  111. package/src/runtime/cloudflare/relay/index.ts +7 -1
  112. package/src/runtime/cloudflare/tsconfig.json +2 -1
  113. package/src/runtime/cloudflare/worker.ts +8 -1
  114. package/src/runtime/node/cleanup.ts +5 -1
  115. package/src/runtime/node/email/transport.ts +13 -10
  116. package/src/runtime/node/main.ts +1 -1
  117. package/src/runtime/node/server.ts +12 -2
  118. package/src/runtime/node/services/main.ts +1 -1
  119. package/src/runtime/posthog/errors.ts +2 -1
  120. package/src/runtime/s3/objects.ts +17 -0
  121. package/src/runtime/site/community.ts +101 -0
  122. package/src/runtime/site/server.ts +17 -1
  123. package/src/server/account/deletion.ts +2 -0
  124. package/src/server/account/routes.ts +271 -240
  125. package/src/server/account/tokens.ts +57 -0
  126. package/src/server/api/router.ts +23 -0
  127. package/src/server/app/checks.ts +84 -0
  128. package/src/server/app/editors.ts +20 -0
  129. package/src/server/app/index.ts +128 -0
  130. package/src/server/app/indexing.ts +40 -0
  131. package/src/server/app/limiters.ts +53 -0
  132. package/src/server/app/routes.ts +114 -0
  133. package/src/server/app/services.ts +185 -0
  134. package/src/server/auth/access-tokens.ts +62 -0
  135. package/src/server/auth/adapter.ts +21 -1
  136. package/src/server/auth/email.ts +4 -1
  137. package/src/server/auth/enrollment.ts +116 -0
  138. package/src/server/auth/factory.ts +88 -131
  139. package/src/server/auth/logger.ts +23 -0
  140. package/src/server/auth/mfa-assurance.ts +51 -0
  141. package/src/server/auth/passwords.ts +43 -0
  142. package/src/server/auth/session.ts +2 -0
  143. package/src/server/billing/index.ts +6 -0
  144. package/src/server/billing/routes.ts +27 -0
  145. package/src/server/billing/tokens.ts +37 -0
  146. package/src/server/cleanup/configured.ts +16 -3
  147. package/src/server/cleanup/worker.ts +10 -1
  148. package/src/server/collaboration/relay/hub.ts +25 -142
  149. package/src/server/collaboration/relay/room.ts +154 -0
  150. package/src/server/collaboration/service.ts +21 -2
  151. package/src/server/community/index.ts +2 -0
  152. package/src/server/community/routes.ts +93 -0
  153. package/src/server/community/service.ts +134 -0
  154. package/src/server/config/deployment.ts +17 -5
  155. package/src/server/config/index.ts +1 -1
  156. package/src/server/config/public.ts +7 -1
  157. package/src/server/config/schema.ts +40 -292
  158. package/src/server/config/toml/schema.ts +307 -0
  159. package/src/server/db/index.ts +1 -0
  160. package/src/server/db/migrate.ts +13 -1
  161. package/src/server/db/migrations/009_access_tokens.ts +47 -0
  162. package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
  163. package/src/server/db/migrations/011_account_ids.ts +30 -0
  164. package/src/server/db/migrations/012_email_locales.ts +34 -0
  165. package/src/server/db/migrations/013_document_block_emails.ts +40 -0
  166. package/src/server/db/migrations/014_report_replies.ts +28 -0
  167. package/src/server/db/migrations/schema.ts +26 -1
  168. package/src/server/db/tables.ts +30 -1
  169. package/src/server/db/users.ts +22 -0
  170. package/src/server/documents/index.ts +1 -1
  171. package/src/server/documents/limits.ts +7 -1
  172. package/src/server/documents/service.ts +12 -402
  173. package/src/server/documents/uploads.ts +450 -0
  174. package/src/server/documents/versions/service.ts +81 -24
  175. package/src/server/email/invitations.ts +1 -0
  176. package/src/server/email/service.ts +31 -4
  177. package/src/server/email/types.ts +12 -0
  178. package/src/server/errors/index.ts +1 -9
  179. package/src/server/index.ts +12 -5
  180. package/src/server/invitations/types.ts +5 -1
  181. package/src/server/objects/types.ts +5 -0
  182. package/src/server/policy/configured.ts +9 -3
  183. package/src/server/policy/database.ts +13 -2
  184. package/src/server/policy/policy.ts +39 -12
  185. package/src/server/policy/service.ts +71 -52
  186. package/src/server/policy/storage.ts +33 -0
  187. package/src/server/quota/index.ts +6 -0
  188. package/src/server/quota/notices.ts +121 -0
  189. package/src/server/quota/storage.ts +5 -0
  190. package/src/server/rate-limit/middleware.ts +4 -0
  191. package/src/server/reports/index.ts +1 -0
  192. package/src/server/reports/replies.ts +55 -0
  193. package/src/server/reports/routes.ts +3 -1
  194. package/src/server/reports/service.ts +24 -2
  195. package/src/server/sharing/capabilities/service.ts +2 -3
  196. package/src/server/sharing/invitations/service.ts +14 -4
  197. package/src/server/validation.ts +3 -0
  198. package/src/server/workspaces/members.ts +7 -19
  199. package/src/services/app.ts +1 -3
  200. package/src/services/config.ts +1 -0
  201. package/src/services/usage.ts +6 -14
  202. package/src/email/templates/document-invitation.ts +0 -35
  203. package/src/email/templates/enrollment.ts +0 -27
  204. package/src/email/templates/layout.ts +0 -18
  205. package/src/server/app.ts +0 -453
  206. /package/src/{server/errors → errors}/posthog.ts +0 -0
@@ -1,6 +1,10 @@
1
1
  import type { EnrollmentStatus } from '#cloud/admin/enrollment/service'
2
2
  import type { CloudSignInMethod, DocumentPermission, WorkspaceRole } from '#cloud/contract'
3
- import type { TransactionalEmailKind, TransactionalEmailPayloadByKind } from '#cloud/email'
3
+ import type {
4
+ EmailLocale,
5
+ TransactionalEmailKind,
6
+ TransactionalEmailPayloadByKind
7
+ } from '#cloud/email'
4
8
  import type { Account, Session } from 'better-auth'
5
9
  import type { ColumnType, Generated, Insertable, Selectable, Updateable } from 'kysely'
6
10
 
@@ -115,6 +119,8 @@ export interface DocumentReportTable {
115
119
  reporterUserId: string | null
116
120
  reporterGuestId: string | null
117
121
  replyEmail: string | null
122
+ /** The language to reply in when the reporter has no account that chose one. */
123
+ replyLocale: Generated<EmailLocale | null>
118
124
  reason: 'spam-phishing' | 'illegal' | 'copyright' | 'harassment' | 'other'
119
125
  description: string
120
126
  status: Generated<'open' | 'dismissed' | 'actioned'>
@@ -206,10 +212,23 @@ export interface AuthUserTable {
206
212
  banExpires: TimestampColumn | null
207
213
  /** Unset on accounts that never turned two-step sign-in on. */
208
214
  twoFactorEnabled: Generated<boolean | null>
215
+ /** The language Cloud writes to the account in; unset is English. */
216
+ locale: Generated<EmailLocale | null>
209
217
  createdAt: TimestampColumn
210
218
  updatedAt: TimestampColumn
211
219
  }
212
220
 
221
+ /** Better Auth's API keys, which are Cloud's personal access tokens; the plugin owns the rest. */
222
+ export interface AccessTokenTable {
223
+ id: string
224
+ referenceId: string
225
+ name: string | null
226
+ start: string | null
227
+ expiresAt: TimestampColumn | null
228
+ lastRequest: TimestampColumn | null
229
+ createdAt: TimestampColumn
230
+ }
231
+
213
232
  export interface TwoFactorTable {
214
233
  id: string
215
234
  secret: string
@@ -312,6 +331,12 @@ export interface AccountEntitlementTable {
312
331
  updatedAt: TimestampColumn
313
332
  }
314
333
 
334
+ /** An account told its storage is full; see `createStorageLimitNoticeService`. */
335
+ export interface StorageLimitNoticeTable {
336
+ userId: string
337
+ notifiedAt: TimestampColumn
338
+ }
339
+
315
340
  export interface WorkspaceStorageUsageTable {
316
341
  workspaceId: string
317
342
  committedBytes: number
@@ -337,6 +362,8 @@ export interface TransactionalEmailTable {
337
362
  kind: TransactionalEmailKind
338
363
  recipientEmailNormalized: string
339
364
  payloadEncrypted: string | null
365
+ /** The language the email is written in. */
366
+ locale: Generated<EmailLocale>
340
367
  status: TransactionalEmailStatus
341
368
  attemptCount: Generated<number>
342
369
  nextAttemptAt: TimestampColumn
@@ -406,6 +433,7 @@ export interface CloudDatabase {
406
433
  signIn: SignInTable
407
434
  userPicture: UserPictureTable
408
435
  user: AuthUserTable
436
+ apikey: AccessTokenTable
409
437
  authSchemaVersion: AuthSchemaVersionTable
410
438
  enrollment: EnrollmentTable
411
439
  apiRateLimit: APIRateLimitTable
@@ -428,6 +456,7 @@ export interface CloudDatabase {
428
456
  accountEntitlement: AccountEntitlementTable
429
457
  termsAcceptance: TermsAcceptanceTable
430
458
  workspaceStorageUsage: WorkspaceStorageUsageTable
459
+ storageLimitNotice: StorageLimitNoticeTable
431
460
  transactionalEmail: TransactionalEmailTable
432
461
  uploadStorageReservation: UploadStorageReservationTable
433
462
  upload: UploadTable
@@ -0,0 +1,22 @@
1
+ import type { ExpressionBuilder } from 'kysely'
2
+
3
+ import type { CloudDatabase } from './tables'
4
+
5
+ /**
6
+ * `user.id` as text, the form every other table keeps account IDs in, such as a document's
7
+ * `createdBy`, so the two can be compared.
8
+ */
9
+ export function userIdText(expression: ExpressionBuilder<CloudDatabase, 'user'>) {
10
+ return expression.cast<string>(expression.ref('user.id'), 'text')
11
+ }
12
+
13
+ /** Whether the user row in scope is banned, by a ban that has not run out. */
14
+ export function isBannedNow(expression: ExpressionBuilder<CloudDatabase, 'user'>) {
15
+ return expression.and([
16
+ expression('user.banned', '=', true),
17
+ expression.or([
18
+ expression('user.banExpires', 'is', null),
19
+ expression('user.banExpires', '>', new Date())
20
+ ])
21
+ ])
22
+ }
@@ -4,7 +4,7 @@ export {
4
4
  resolveDocumentAccess,
5
5
  type AccessDatabase
6
6
  } from './access'
7
- export { createDocumentRoutes, type DocumentRouteEnvironment } from './routes'
7
+ export { createDocumentRoutes, documentDomainError, type DocumentRouteEnvironment } from './routes'
8
8
  export {
9
9
  DocumentConflictError,
10
10
  DocumentForbiddenError,
@@ -4,12 +4,15 @@ import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/doc
4
4
  import type { DocumentServiceOptions } from '#cloud/server/documents/service'
5
5
  import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
6
6
  import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
7
+ import { storageLimitReached } from '#cloud/server/policy/storage'
7
8
  import { workspacePolicyContext } from '#cloud/server/policy/workspace'
9
+ import { StorageQuotaExceededError } from '#cloud/server/quota'
8
10
  import type { Kysely } from 'kysely'
9
11
 
10
12
  /**
11
13
  * The document `userId` may write to, and how large one file and its owner's storage may be for
12
- * this save: technical limits, tightened by the owner's plan.
14
+ * this save: technical limits, tightened by the owner's plan. An owner who holds their whole
15
+ * storage allowance or more is read-only, so nothing of theirs takes a save, however small.
13
16
  */
14
17
  export async function writableDocument(
15
18
  database: Kysely<CloudDatabase>,
@@ -30,6 +33,9 @@ export async function writableDocument(
30
33
  { actorId: userId, deploymentMode: 'self-hosted' }
31
34
  )
32
35
  if (!policyContext) throw new DocumentNotFoundError()
36
+ if (await storageLimitReached(database, options.policy, { documentId })) {
37
+ throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
38
+ }
33
39
  const technicalMaximum = options.technicalMaximumUploadBytes ?? Number.MAX_SAFE_INTEGER
34
40
  const maximumFileBytes = Math.min(
35
41
  technicalMaximum,
@@ -1,12 +1,7 @@
1
1
  import type {
2
- CommitImageUploadsInput,
3
- CommitUploadInput,
4
2
  CreateDocumentInput,
5
- CreateUploadInput,
6
3
  DocumentDownload,
7
4
  DocumentSummary,
8
- ImageUploadOffer,
9
- RequestImageUploadsInput,
10
5
  SharedDocument
11
6
  } from '#cloud/contract'
12
7
  import { workspaceRoleCan } from '#cloud/server/auth/access-control'
@@ -17,19 +12,13 @@ import {
17
12
  canEditDocument,
18
13
  resolveDocumentAccess
19
14
  } from '#cloud/server/documents/access'
20
- import {
21
- DocumentConflictError,
22
- DocumentForbiddenError,
23
- DocumentNotFoundError,
24
- UploadInvalidError
25
- } from '#cloud/server/documents/errors'
15
+ import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
26
16
  import {
27
17
  createDocumentImages,
28
18
  currentImageUsage,
29
19
  documentImageLinks,
30
20
  revisionImageDownloads
31
21
  } from '#cloud/server/documents/images'
32
- import { writableDocument } from '#cloud/server/documents/limits'
33
22
  import {
34
23
  findDocument,
35
24
  insertDocument,
@@ -43,19 +32,15 @@ import {
43
32
  DEFAULT_TRASH_OPTIONS,
44
33
  type DocumentTrashOptions
45
34
  } from '#cloud/server/documents/trash'
46
- import type { CreateDocumentUploadResult } from '#cloud/server/documents/types'
47
- import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
35
+ import { createDocumentUploads } from '#cloud/server/documents/uploads'
48
36
  import type { CloudErrorReporter } from '#cloud/server/errors'
49
- import type { ObjectStore, StoredObject } from '#cloud/server/objects'
37
+ import type { ObjectStore } from '#cloud/server/objects'
50
38
  import type { CloudPolicy } from '#cloud/server/policy/policy'
51
- import { createStorageQuotaService } from '#cloud/server/quota'
52
- import { isUndefined, limitAsync, omitBy, uniq, uniqBy } from 'es-toolkit'
53
- import type { Kysely, Transaction } from 'kysely'
39
+ import { storageLimitReached } from '#cloud/server/policy/storage'
40
+ import { createStorageQuotaService, StorageQuotaExceededError } from '#cloud/server/quota'
41
+ import type { Kysely } from 'kysely'
54
42
 
55
43
  const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
56
- const UPLOAD_LIFETIME_MS = 15 * 60 * 1000
57
- /** How many of a save's images are checked and landed at once. */
58
- const IMAGE_COMMIT_CONCURRENCY = 8
59
44
 
60
45
  export type DocumentServiceOptions = {
61
46
  policy?: CloudPolicy
@@ -68,18 +53,6 @@ export type DocumentServiceOptions = {
68
53
  reportError?: CloudErrorReporter
69
54
  }
70
55
 
71
- /** Whether the stored object is the file the upload declared, which the commit names again. */
72
- function matchesUpload(
73
- stored: { checksum: string; byteSize: number; contentType: string },
74
- upload: { checksum: string; byteSize: number | string | bigint; contentType: string }
75
- ): boolean {
76
- return (
77
- stored.checksum === upload.checksum &&
78
- stored.byteSize === Number(upload.byteSize) &&
79
- stored.contentType === upload.contentType
80
- )
81
- }
82
-
83
56
  export function createDocumentService(
84
57
  database: Kysely<CloudDatabase>,
85
58
  objects: ObjectStore,
@@ -89,109 +62,7 @@ export function createDocumentService(
89
62
  const quota = createStorageQuotaService(database)
90
63
  const images = createDocumentImages(database, objects, quota)
91
64
  const trash = createDocumentTrash(database, objects, options.trash ?? DEFAULT_TRASH_OPTIONS)
92
-
93
- /**
94
- * Makes a committed upload the document's current revision, unless it already was, and records
95
- * the checkpoint the save adds, if any. Whether the history gained one.
96
- */
97
- async function landRevision(
98
- transaction: Transaction<CloudDatabase>,
99
- {
100
- userId,
101
- uploadId,
102
- upload,
103
- stored,
104
- revisionId,
105
- storageObjectId,
106
- images: revisionImages,
107
- name,
108
- kind
109
- }: {
110
- userId: string
111
- uploadId: string
112
- upload: {
113
- documentId: string
114
- workspaceId: string
115
- baseRevisionId: string | null
116
- objectKey: string
117
- }
118
- stored: { checksum: string; byteSize: number; contentType: string }
119
- revisionId: string
120
- storageObjectId: string
121
- images: CommitUploadInput['images']
122
- name: CommitUploadInput['name']
123
- kind: CommitUploadInput['kind']
124
- }
125
- ): Promise<boolean> {
126
- const lockedUpload = await transaction
127
- .selectFrom('upload')
128
- .select('status')
129
- .where('id', '=', uploadId)
130
- .forUpdate()
131
- .executeTakeFirstOrThrow()
132
- if (lockedUpload.status === 'committed') return false
133
- if (lockedUpload.status !== 'finalizing') throw new UploadInvalidError()
134
- const document = await transaction
135
- .selectFrom('document')
136
- .select(['currentRevisionId'])
137
- .where('id', '=', upload.documentId)
138
- .forUpdate()
139
- .executeTakeFirstOrThrow()
140
- if (document.currentRevisionId !== upload.baseRevisionId) {
141
- throw new DocumentConflictError()
142
- }
143
- await transaction
144
- .insertInto('storageObject')
145
- .values({
146
- id: storageObjectId,
147
- objectKey: upload.objectKey,
148
- checksum: stored.checksum,
149
- byteSize: stored.byteSize,
150
- contentType: stored.contentType
151
- })
152
- .execute()
153
- await transaction
154
- .insertInto('documentRevision')
155
- .values({
156
- id: revisionId,
157
- documentId: upload.documentId,
158
- parentRevisionId: upload.baseRevisionId,
159
- storageObjectId,
160
- createdBy: userId
161
- })
162
- .execute()
163
- const attached = await images.attachInTransaction(transaction, {
164
- workspaceId: upload.workspaceId,
165
- revisionId,
166
- images: revisionImages
167
- })
168
- if (!attached) throw new UploadInvalidError()
169
- await transaction
170
- .updateTable('document')
171
- .set({
172
- // A revision may also retitle its document, or say the file it holds is a board.
173
- ...omitBy({ name, kind }, isUndefined),
174
- currentRevisionId: revisionId,
175
- version: (expression) => expression('version', '+', 1)
176
- })
177
- .where('id', '=', upload.documentId)
178
- .execute()
179
- const checkpointed = await recordCheckpoint(transaction, {
180
- documentId: upload.documentId,
181
- previousRevisionId: document.currentRevisionId,
182
- revisionId,
183
- userId,
184
- now: new Date()
185
- })
186
- await transaction
187
- .updateTable('upload')
188
- .set({ status: 'committed', finalizationStartedAt: null })
189
- .where('id', '=', uploadId)
190
- .execute()
191
- await quota.commitInTransaction(transaction, uploadId)
192
- return checkpointed
193
- }
194
-
65
+ const uploads = createDocumentUploads(database, objects, { quota, images, options })
195
66
  /** Links for images a live room names, by checksum. */
196
67
  async function imageLinksOf(documentId: string, checksums: string[]) {
197
68
  const expiresAt = new Date(Date.now() + DOWNLOAD_LIFETIME_MS)
@@ -230,130 +101,6 @@ export function createDocumentService(
230
101
  }
231
102
  }
232
103
 
233
- /** What committing an upload reads of it and its document. */
234
- function selectUploads() {
235
- return database
236
- .selectFrom('upload')
237
- .innerJoin('document', 'document.id', 'upload.documentId')
238
- .select([
239
- 'upload.id',
240
- 'upload.documentId',
241
- 'upload.kind',
242
- 'document.workspaceId',
243
- 'upload.baseRevisionId',
244
- 'upload.objectKey',
245
- 'upload.checksum',
246
- 'upload.byteSize',
247
- 'upload.contentType',
248
- 'upload.multipartUploadId',
249
- 'upload.status',
250
- 'upload.expiresAt'
251
- ])
252
- }
253
- type UploadRow = Awaited<ReturnType<ReturnType<typeof selectUploads>['executeTakeFirstOrThrow']>>
254
-
255
- /**
256
- * Claims a pending upload, completes it when it came in parts, checks the store holds the file
257
- * it declared, and lands it with `land`. An upload that fails goes back to pending, so its
258
- * commit can be tried again.
259
- */
260
- async function finalizeUpload<T>(
261
- upload: UploadRow,
262
- multipart: CommitUploadInput['multipart'],
263
- land: (stored: StoredObject) => Promise<T>
264
- ): Promise<T> {
265
- if (upload.status !== 'pending' || new Date(upload.expiresAt).getTime() <= Date.now()) {
266
- throw new UploadInvalidError()
267
- }
268
- const claimed = await database
269
- .updateTable('upload')
270
- .set({ status: 'finalizing', finalizationStartedAt: new Date() })
271
- .where('id', '=', upload.id)
272
- .where('status', '=', 'pending')
273
- .returning('id')
274
- .executeTakeFirst()
275
- if (!claimed) throw new UploadInvalidError()
276
- try {
277
- if (multipart) {
278
- if (
279
- !objects.capabilities.multipartUpload ||
280
- multipart.uploadId !== upload.multipartUploadId
281
- ) {
282
- throw new UploadInvalidError()
283
- }
284
- await objects.completeUpload({
285
- key: upload.objectKey,
286
- uploadId: multipart.uploadId,
287
- parts: multipart.parts
288
- })
289
- }
290
- const stored = await objects.head(upload.objectKey)
291
- if (!stored || !matchesUpload(stored, upload)) throw new UploadInvalidError()
292
- return await land(stored)
293
- } catch (error) {
294
- await database
295
- .updateTable('upload')
296
- .set({ status: 'pending', finalizationStartedAt: null })
297
- .where('id', '=', upload.id)
298
- .where('status', '=', 'finalizing')
299
- .execute()
300
- throw error
301
- }
302
- }
303
-
304
- function landImage(upload: UploadRow, stored: StoredObject): Promise<void> {
305
- return database.transaction().execute((transaction) =>
306
- images.landInTransaction(transaction, {
307
- uploadId: upload.id,
308
- workspaceId: upload.workspaceId,
309
- objectKey: upload.objectKey,
310
- stored
311
- })
312
- )
313
- }
314
-
315
- async function commitUpload(
316
- userId: string,
317
- uploadId: string,
318
- input: CommitUploadInput
319
- ): Promise<DocumentSummary> {
320
- const upload = await selectUploads()
321
- .where('upload.id', '=', uploadId)
322
- .where('upload.createdBy', '=', userId)
323
- .executeTakeFirst()
324
- if (!upload) throw new DocumentNotFoundError()
325
- const access = await resolveDocumentAccess(database, userId, upload.documentId)
326
- if (!access) throw new DocumentNotFoundError()
327
- if (!canEditDocument(access)) throw new DocumentForbiddenError()
328
- if (input.checksum !== upload.checksum) throw new UploadInvalidError()
329
- if (upload.status !== 'committed') {
330
- const checkpointed =
331
- upload.kind === 'image'
332
- ? await finalizeUpload(upload, input.multipart, (stored) =>
333
- landImage(upload, stored).then(() => false)
334
- )
335
- : await finalizeUpload(upload, input.multipart, (stored) =>
336
- database.transaction().execute((transaction) =>
337
- landRevision(transaction, {
338
- userId,
339
- uploadId,
340
- upload,
341
- stored,
342
- revisionId: crypto.randomUUID(),
343
- storageObjectId: crypto.randomUUID(),
344
- images: input.images,
345
- name: input.name,
346
- kind: input.kind
347
- })
348
- )
349
- )
350
- if (checkpointed) announceVersions(options.onVersionsChanged, upload.documentId)
351
- }
352
- const document = await findDocument(database, userId, upload.documentId)
353
- if (!document) throw new DocumentNotFoundError()
354
- return document
355
- }
356
-
357
104
  return {
358
105
  async cleanupExpiredUploads(now = new Date()): Promise<number> {
359
106
  const result = await cleanup.cleanupExpiredUploads({
@@ -451,6 +198,9 @@ export function createDocumentService(
451
198
  const role = await workspaceRole(database, userId, workspaceId)
452
199
  if (!role) throw new DocumentNotFoundError()
453
200
  if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
201
+ if (await storageLimitReached(database, options.policy, { workspaceId })) {
202
+ throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
203
+ }
454
204
  const documentId = input.id ?? crypto.randomUUID()
455
205
  await insertDocument(database, {
456
206
  id: documentId,
@@ -463,148 +213,8 @@ export function createDocumentService(
463
213
  if (!document) throw new DocumentNotFoundError()
464
214
  return document
465
215
  },
466
-
467
- async createUpload(
468
- userId: string,
469
- documentId: string,
470
- input: CreateUploadInput
471
- ): Promise<CreateDocumentUploadResult> {
472
- const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
473
- database,
474
- options,
475
- userId,
476
- documentId
477
- )
478
- if (document.currentRevisionId !== input.baseRevisionId) throw new DocumentConflictError()
479
- if (input.byteSize > maximumFileBytes) throw new UploadInvalidError()
480
- const uploadId = crypto.randomUUID()
481
- const objectKey = `documents/${document.workspaceId}/${documentId}/uploads/${uploadId}.fig`
482
- const expiresAt = new Date(Date.now() + UPLOAD_LIFETIME_MS)
483
- await database.transaction().execute(async (transaction) => {
484
- await transaction
485
- .insertInto('upload')
486
- .values({
487
- id: uploadId,
488
- documentId,
489
- baseRevisionId: input.baseRevisionId,
490
- objectKey,
491
- checksum: input.checksum,
492
- byteSize: input.byteSize,
493
- contentType: input.contentType,
494
- multipartUploadId: null,
495
- status: 'pending',
496
- finalizationStartedAt: null,
497
- createdBy: userId,
498
- expiresAt
499
- })
500
- .execute()
501
- await quota.reserveInTransaction(transaction, {
502
- workspaceId: document.workspaceId,
503
- uploadId,
504
- bytes: input.byteSize,
505
- expiresAt,
506
- maximumBytes: maximumStorageBytes
507
- })
508
- })
509
- let objectUpload
510
- try {
511
- objectUpload = await objects.createUpload({
512
- key: objectKey,
513
- byteSize: input.byteSize,
514
- checksum: input.checksum,
515
- contentType: input.contentType,
516
- expiresAt
517
- })
518
- } catch (error) {
519
- await quota.release(uploadId)
520
- await database
521
- .updateTable('upload')
522
- .set({ status: 'abandoned' })
523
- .where('id', '=', uploadId)
524
- .execute()
525
- throw error
526
- }
527
- if (objectUpload.kind === 'multipart') {
528
- await database
529
- .updateTable('upload')
530
- .set({ multipartUploadId: objectUpload.uploadId })
531
- .where('id', '=', uploadId)
532
- .execute()
533
- }
534
- return { id: uploadId, upload: objectUpload }
535
- },
536
-
537
- /** Uploads for the images a save of the document will name that its workspace lacks. */
538
- async requestImageUploads(
539
- userId: string,
540
- documentId: string,
541
- input: RequestImageUploadsInput
542
- ): Promise<{ uploads: ImageUploadOffer[] }> {
543
- const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
544
- database,
545
- options,
546
- userId,
547
- documentId
548
- )
549
- if (input.images.some((image) => image.byteSize > maximumFileBytes)) {
550
- throw new UploadInvalidError()
551
- }
552
- return {
553
- uploads: await images.requestUploads({
554
- userId,
555
- documentId,
556
- workspaceId: document.workspaceId,
557
- images: input.images,
558
- expiresAt: new Date(Date.now() + UPLOAD_LIFETIME_MS),
559
- maximumBytes: maximumStorageBytes
560
- })
561
- }
562
- },
563
- commitUpload,
564
-
565
- /**
566
- * Commits the image uploads a save asked for in one request, however many there are: each
567
- * must be an image upload this person made for the document.
568
- */
569
- async commitImageUploads(
570
- userId: string,
571
- documentId: string,
572
- uploads: CommitImageUploadsInput['uploads']
573
- ): Promise<void> {
574
- const access = await resolveDocumentAccess(database, userId, documentId)
575
- if (!access) throw new DocumentNotFoundError()
576
- if (!canEditDocument(access)) throw new DocumentForbiddenError()
577
- const ids = uniq(uploads.map((upload) => upload.uploadId))
578
- const rows = new Map(
579
- (
580
- await selectUploads()
581
- .where('upload.id', 'in', ids)
582
- .where('upload.documentId', '=', documentId)
583
- .where('upload.createdBy', '=', userId)
584
- .execute()
585
- ).map((row) => [row.id, row])
586
- )
587
- if (rows.size !== ids.length || [...rows.values()].some((row) => row.kind !== 'image')) {
588
- throw new UploadInvalidError()
589
- }
590
- for (const commit of uploads) {
591
- if (rows.get(commit.uploadId)?.checksum !== commit.checksum) throw new UploadInvalidError()
592
- }
593
- // Each image is its own object, so a few are checked and landed at once, each once however
594
- // often the request names it.
595
- const land = limitAsync(
596
- (upload: UploadRow, multipart: CommitUploadInput['multipart']) =>
597
- finalizeUpload(upload, multipart, (stored) => landImage(upload, stored)),
598
- IMAGE_COMMIT_CONCURRENCY
599
- )
600
- await Promise.all(
601
- uniqBy(uploads, (commit) => commit.uploadId).map(async (commit) => {
602
- const upload = rows.get(commit.uploadId)
603
- if (!upload) throw new UploadInvalidError()
604
- if (upload.status !== 'committed') await land(upload, commit.multipart)
605
- })
606
- )
607
- }
216
+ /** Saving: uploads of revisions and their images, and committing them. */
217
+ ...uploads
608
218
  }
609
219
  }
610
220