@open-pencil/cloud 0.17.0-beta.2 → 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.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { TrashedDocument, WorkspaceRole } from '#cloud/contract'
|
|
2
|
+
import { documentRoleCan, workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
3
|
+
import { createDocumentCleanupService } from '#cloud/server/cleanup'
|
|
4
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
5
|
+
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
6
|
+
import type { ObjectStore } from '#cloud/server/objects'
|
|
7
|
+
import { compact, uniq } from 'es-toolkit'
|
|
8
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
9
|
+
import * as v from 'valibot'
|
|
10
|
+
|
|
11
|
+
import { trashedDocumentAllows } from './access'
|
|
12
|
+
import { DocumentForbiddenError, DocumentNotFoundError, DocumentPurgeFailedError } from './errors'
|
|
13
|
+
import { markPurged } from './purge'
|
|
14
|
+
|
|
15
|
+
const uuidSchema = v.pipe(v.string(), v.uuid())
|
|
16
|
+
|
|
17
|
+
const WORKSPACE_ROLES: WorkspaceRole[] = ['admin', 'editor', 'viewer']
|
|
18
|
+
|
|
19
|
+
/** The workspace roles whose members may delete any document there, and so restore it. */
|
|
20
|
+
const RESTORING_ROLES = WORKSPACE_ROLES.filter((role) =>
|
|
21
|
+
workspaceRoleCan(role, { document: ['delete'] })
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
export type DocumentTrashOptions = {
|
|
25
|
+
/** How long a deleted document stays in Trash before the cleanup worker deletes it for good. */
|
|
26
|
+
retentionMs: number
|
|
27
|
+
/**
|
|
28
|
+
* How long a document deleted forever keeps its files, out of sight, before cleanup destroys
|
|
29
|
+
* it; 0 destroys it at once.
|
|
30
|
+
*/
|
|
31
|
+
purgeGraceMs: number
|
|
32
|
+
/** How long a cleanup claim holds before another pass may take the document over. */
|
|
33
|
+
leaseDurationMs: number
|
|
34
|
+
/** Where destroying a document deleted forever with no grace reports a failure. */
|
|
35
|
+
reportError?: CloudErrorReporter
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A deployment's defaults: 30 days in Trash, 7 days' grace, and a five-minute cleanup lease. */
|
|
39
|
+
export const DEFAULT_TRASH_OPTIONS: DocumentTrashOptions = {
|
|
40
|
+
retentionMs: 30 * 24 * 60 * 60_000,
|
|
41
|
+
purgeGraceMs: 7 * 24 * 60 * 60_000,
|
|
42
|
+
leaseDurationMs: 5 * 60_000
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The names of the accounts these IDs belong to; IDs that are not accounts are left out. */
|
|
46
|
+
async function accountNames(
|
|
47
|
+
database: Kysely<CloudDatabase>,
|
|
48
|
+
ids: (string | null)[]
|
|
49
|
+
): Promise<Map<string, string>> {
|
|
50
|
+
const accounts = uniq(compact(ids)).filter((id) => v.is(uuidSchema, id))
|
|
51
|
+
if (accounts.length === 0) return new Map()
|
|
52
|
+
const users = await database
|
|
53
|
+
.selectFrom('user')
|
|
54
|
+
.select(['id', 'name'])
|
|
55
|
+
.where('id', 'in', accounts)
|
|
56
|
+
.execute()
|
|
57
|
+
return new Map(users.map((user) => [user.id, user.name]))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toISOString(value: Date | string): string {
|
|
61
|
+
return value instanceof Date ? value.toISOString() : value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Trash: documents deleted in the person's workspaces that cleanup has not removed yet. Whoever
|
|
66
|
+
* may delete a document may restore it; its owner and workspace administrators may also delete
|
|
67
|
+
* it forever, which takes it out of Trash and off their storage at once (see `markPurged`).
|
|
68
|
+
*/
|
|
69
|
+
export function createDocumentTrash(
|
|
70
|
+
database: Kysely<CloudDatabase>,
|
|
71
|
+
objects: ObjectStore,
|
|
72
|
+
options: DocumentTrashOptions
|
|
73
|
+
) {
|
|
74
|
+
const cleanup = createDocumentCleanupService(database, objects, {
|
|
75
|
+
reportError: options.reportError
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
/** Deleted after this moment, a document is still in Trash. */
|
|
79
|
+
function retainedSince(now: Date): Date {
|
|
80
|
+
return new Date(now.getTime() - options.retentionMs)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
async list(userId: string, now = new Date()): Promise<TrashedDocument[]> {
|
|
85
|
+
const rows = await database
|
|
86
|
+
.selectFrom('document')
|
|
87
|
+
.innerJoin('workspaceMember', (join) =>
|
|
88
|
+
join
|
|
89
|
+
.onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
|
|
90
|
+
.on('workspaceMember.userId', '=', userId)
|
|
91
|
+
)
|
|
92
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
93
|
+
.select([
|
|
94
|
+
'document.id',
|
|
95
|
+
'document.workspaceId',
|
|
96
|
+
'workspace.name as workspaceName',
|
|
97
|
+
'document.name',
|
|
98
|
+
'document.kind',
|
|
99
|
+
'document.createdBy',
|
|
100
|
+
'document.deletedAt',
|
|
101
|
+
'document.deletedBy',
|
|
102
|
+
'workspaceMember.role'
|
|
103
|
+
])
|
|
104
|
+
.where('document.deletedAt', '>', retainedSince(now))
|
|
105
|
+
.where('document.purgedAt', 'is', null)
|
|
106
|
+
.where('document.cleanupClaimId', 'is', null)
|
|
107
|
+
.where('document.disabledAt', 'is', null)
|
|
108
|
+
.where((expression) =>
|
|
109
|
+
expression.or([
|
|
110
|
+
expression('workspaceMember.role', 'in', RESTORING_ROLES),
|
|
111
|
+
expression('document.createdBy', '=', userId)
|
|
112
|
+
])
|
|
113
|
+
)
|
|
114
|
+
.orderBy('document.deletedAt', 'desc')
|
|
115
|
+
.execute()
|
|
116
|
+
const names = await accountNames(
|
|
117
|
+
database,
|
|
118
|
+
rows.map((row) => row.deletedBy)
|
|
119
|
+
)
|
|
120
|
+
return rows.flatMap(({ createdBy, deletedAt, deletedBy, role, ...row }) => {
|
|
121
|
+
if (deletedAt === null) return []
|
|
122
|
+
const deleted = new Date(deletedAt)
|
|
123
|
+
const deleterName = deletedBy === null ? undefined : names.get(deletedBy)
|
|
124
|
+
return {
|
|
125
|
+
...row,
|
|
126
|
+
deletedAt: toISOString(deletedAt),
|
|
127
|
+
deletedBy: deleterName === undefined ? null : { name: deleterName },
|
|
128
|
+
purgeAt: new Date(deleted.getTime() + options.retentionMs).toISOString(),
|
|
129
|
+
canPurge:
|
|
130
|
+
workspaceRoleCan(role, { document: ['purge'] }) ||
|
|
131
|
+
(createdBy === userId && documentRoleCan('owner', { document: ['purge'] }))
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/** Puts a document back where it was; it counted toward storage all along, so no quota check. */
|
|
137
|
+
async restore(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
138
|
+
await database.transaction().execute(async (transaction) => {
|
|
139
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
140
|
+
await transaction
|
|
141
|
+
.updateTable('document')
|
|
142
|
+
// Changed now, so devices that deleted it stop hiding it (see `reconcileStorageDocuments`).
|
|
143
|
+
.set({ deletedAt: null, deletedBy: null, updatedAt: now })
|
|
144
|
+
.where('id', '=', documentId)
|
|
145
|
+
.execute()
|
|
146
|
+
})
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Deletes a document in Trash forever, for the person: it leaves Trash and their storage now,
|
|
151
|
+
* and cleanup destroys it once the deployment's grace passes, or at once without one.
|
|
152
|
+
*/
|
|
153
|
+
async purge(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
154
|
+
await database.transaction().execute(async (transaction) => {
|
|
155
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
156
|
+
if (!(await trashedDocumentAllows(transaction, userId, documentId, 'purge'))) {
|
|
157
|
+
throw new DocumentForbiddenError()
|
|
158
|
+
}
|
|
159
|
+
await markPurged(transaction, [documentId], userId, now)
|
|
160
|
+
})
|
|
161
|
+
if (options.purgeGraceMs > 0) return
|
|
162
|
+
try {
|
|
163
|
+
// A save still in flight holds it for the next cleanup pass; it is out of Trash already.
|
|
164
|
+
await cleanup.destroyPurgedDocument(documentId, {
|
|
165
|
+
leaseDurationMs: options.leaseDurationMs,
|
|
166
|
+
now
|
|
167
|
+
})
|
|
168
|
+
} catch (error) {
|
|
169
|
+
throw new DocumentPurgeFailedError(`Document ${documentId} could not be destroyed`, {
|
|
170
|
+
cause: error
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Holds a document in Trash until the transaction ends: deleted, not forever, inside retention,
|
|
178
|
+
* and not being cleaned, by someone who may restore it; anything else is not found.
|
|
179
|
+
*/
|
|
180
|
+
async function lockInTrash(
|
|
181
|
+
transaction: Transaction<CloudDatabase>,
|
|
182
|
+
userId: string,
|
|
183
|
+
documentId: string,
|
|
184
|
+
now: Date
|
|
185
|
+
): Promise<void> {
|
|
186
|
+
const document = await transaction
|
|
187
|
+
.selectFrom('document')
|
|
188
|
+
.select(['deletedAt', 'purgedAt', 'cleanupClaimId'])
|
|
189
|
+
.where('id', '=', documentId)
|
|
190
|
+
.forUpdate()
|
|
191
|
+
.executeTakeFirst()
|
|
192
|
+
const inTrash =
|
|
193
|
+
document?.deletedAt != null &&
|
|
194
|
+
document.purgedAt === null &&
|
|
195
|
+
document.cleanupClaimId === null &&
|
|
196
|
+
new Date(document.deletedAt) > retainedSince(now)
|
|
197
|
+
if (!inTrash || !(await trashedDocumentAllows(transaction, userId, documentId, 'delete'))) {
|
|
198
|
+
throw new DocumentNotFoundError()
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type DocumentTrash = ReturnType<typeof createDocumentTrash>
|
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CommitImageUploadsInput,
|
|
3
|
+
CommitUploadInput,
|
|
4
|
+
CreateUploadInput,
|
|
5
|
+
DocumentSummary,
|
|
6
|
+
ImageUploadOffer,
|
|
7
|
+
RequestImageUploadsInput
|
|
8
|
+
} from '#cloud/contract'
|
|
9
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
10
|
+
import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
11
|
+
import {
|
|
12
|
+
DocumentConflictError,
|
|
13
|
+
DocumentForbiddenError,
|
|
14
|
+
DocumentNotFoundError,
|
|
15
|
+
UploadInvalidError
|
|
16
|
+
} from '#cloud/server/documents/errors'
|
|
17
|
+
import type { DocumentImages } from '#cloud/server/documents/images'
|
|
18
|
+
import { writableDocument } from '#cloud/server/documents/limits'
|
|
19
|
+
import { findDocument } from '#cloud/server/documents/repository'
|
|
20
|
+
import type { DocumentServiceOptions } from '#cloud/server/documents/service'
|
|
21
|
+
import type { CreateDocumentUploadResult } from '#cloud/server/documents/types'
|
|
22
|
+
import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
|
|
23
|
+
import type { ObjectStore, StoredObject } from '#cloud/server/objects'
|
|
24
|
+
import { storageLimitReached } from '#cloud/server/policy/storage'
|
|
25
|
+
import { StorageQuotaExceededError, type StorageQuotaService } from '#cloud/server/quota'
|
|
26
|
+
import { isUndefined, limitAsync, omitBy, uniq, uniqBy } from 'es-toolkit'
|
|
27
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
28
|
+
|
|
29
|
+
const UPLOAD_LIFETIME_MS = 15 * 60 * 1000
|
|
30
|
+
/** How many of a save's images are checked and landed at once. */
|
|
31
|
+
const IMAGE_COMMIT_CONCURRENCY = 8
|
|
32
|
+
|
|
33
|
+
/** Whether the stored object is the file the upload declared, which the commit names again. */
|
|
34
|
+
function matchesUpload(
|
|
35
|
+
stored: { checksum: string; byteSize: number; contentType: string },
|
|
36
|
+
upload: { checksum: string; byteSize: number | string | bigint; contentType: string }
|
|
37
|
+
): boolean {
|
|
38
|
+
return (
|
|
39
|
+
stored.checksum === upload.checksum &&
|
|
40
|
+
stored.byteSize === Number(upload.byteSize) &&
|
|
41
|
+
stored.contentType === upload.contentType
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Saving a document: uploads of a new revision and of the images it names, reserved against the
|
|
47
|
+
* workspace's storage, then committed once the store holds what each declared, landing the
|
|
48
|
+
* revision as the document's current one.
|
|
49
|
+
*/
|
|
50
|
+
export function createDocumentUploads(
|
|
51
|
+
database: Kysely<CloudDatabase>,
|
|
52
|
+
objects: ObjectStore,
|
|
53
|
+
{
|
|
54
|
+
quota,
|
|
55
|
+
images,
|
|
56
|
+
options
|
|
57
|
+
}: {
|
|
58
|
+
quota: StorageQuotaService
|
|
59
|
+
images: DocumentImages
|
|
60
|
+
options: Pick<
|
|
61
|
+
DocumentServiceOptions,
|
|
62
|
+
'policy' | 'technicalMaximumUploadBytes' | 'onVersionsChanged'
|
|
63
|
+
>
|
|
64
|
+
}
|
|
65
|
+
) {
|
|
66
|
+
/**
|
|
67
|
+
* Refuses to land an upload once its document's owner holds their whole storage allowance, as
|
|
68
|
+
* when their plan shrank after the upload was offered; an upload already landed is not landed
|
|
69
|
+
* again, so retrying one stays as it was.
|
|
70
|
+
*/
|
|
71
|
+
async function refuseAtStorageLimit(documentId: string): Promise<void> {
|
|
72
|
+
if (await storageLimitReached(database, options.policy, { documentId })) {
|
|
73
|
+
throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Makes a committed upload the document's current revision, unless it already was, and records
|
|
79
|
+
* the checkpoint the save adds, if any. Whether the history gained one.
|
|
80
|
+
*/
|
|
81
|
+
async function landRevision(
|
|
82
|
+
transaction: Transaction<CloudDatabase>,
|
|
83
|
+
{
|
|
84
|
+
userId,
|
|
85
|
+
uploadId,
|
|
86
|
+
upload,
|
|
87
|
+
stored,
|
|
88
|
+
revisionId,
|
|
89
|
+
storageObjectId,
|
|
90
|
+
images: revisionImages,
|
|
91
|
+
name,
|
|
92
|
+
kind
|
|
93
|
+
}: {
|
|
94
|
+
userId: string
|
|
95
|
+
uploadId: string
|
|
96
|
+
upload: {
|
|
97
|
+
documentId: string
|
|
98
|
+
workspaceId: string
|
|
99
|
+
baseRevisionId: string | null
|
|
100
|
+
objectKey: string
|
|
101
|
+
}
|
|
102
|
+
stored: { checksum: string; byteSize: number; contentType: string }
|
|
103
|
+
revisionId: string
|
|
104
|
+
storageObjectId: string
|
|
105
|
+
images: CommitUploadInput['images']
|
|
106
|
+
name: CommitUploadInput['name']
|
|
107
|
+
kind: CommitUploadInput['kind']
|
|
108
|
+
}
|
|
109
|
+
): Promise<boolean> {
|
|
110
|
+
const lockedUpload = await transaction
|
|
111
|
+
.selectFrom('upload')
|
|
112
|
+
.select('status')
|
|
113
|
+
.where('id', '=', uploadId)
|
|
114
|
+
.forUpdate()
|
|
115
|
+
.executeTakeFirstOrThrow()
|
|
116
|
+
if (lockedUpload.status === 'committed') return false
|
|
117
|
+
if (lockedUpload.status !== 'finalizing') throw new UploadInvalidError()
|
|
118
|
+
const document = await transaction
|
|
119
|
+
.selectFrom('document')
|
|
120
|
+
.select(['currentRevisionId'])
|
|
121
|
+
.where('id', '=', upload.documentId)
|
|
122
|
+
.forUpdate()
|
|
123
|
+
.executeTakeFirstOrThrow()
|
|
124
|
+
if (document.currentRevisionId !== upload.baseRevisionId) {
|
|
125
|
+
throw new DocumentConflictError()
|
|
126
|
+
}
|
|
127
|
+
await transaction
|
|
128
|
+
.insertInto('storageObject')
|
|
129
|
+
.values({
|
|
130
|
+
id: storageObjectId,
|
|
131
|
+
objectKey: upload.objectKey,
|
|
132
|
+
checksum: stored.checksum,
|
|
133
|
+
byteSize: stored.byteSize,
|
|
134
|
+
contentType: stored.contentType
|
|
135
|
+
})
|
|
136
|
+
.execute()
|
|
137
|
+
await transaction
|
|
138
|
+
.insertInto('documentRevision')
|
|
139
|
+
.values({
|
|
140
|
+
id: revisionId,
|
|
141
|
+
documentId: upload.documentId,
|
|
142
|
+
parentRevisionId: upload.baseRevisionId,
|
|
143
|
+
storageObjectId,
|
|
144
|
+
createdBy: userId
|
|
145
|
+
})
|
|
146
|
+
.execute()
|
|
147
|
+
const attached = await images.attachInTransaction(transaction, {
|
|
148
|
+
workspaceId: upload.workspaceId,
|
|
149
|
+
revisionId,
|
|
150
|
+
images: revisionImages
|
|
151
|
+
})
|
|
152
|
+
if (!attached) throw new UploadInvalidError()
|
|
153
|
+
await transaction
|
|
154
|
+
.updateTable('document')
|
|
155
|
+
.set({
|
|
156
|
+
// A revision may also retitle its document, or say the file it holds is a board.
|
|
157
|
+
...omitBy({ name, kind }, isUndefined),
|
|
158
|
+
currentRevisionId: revisionId,
|
|
159
|
+
version: (expression) => expression('version', '+', 1)
|
|
160
|
+
})
|
|
161
|
+
.where('id', '=', upload.documentId)
|
|
162
|
+
.execute()
|
|
163
|
+
const checkpointed = await recordCheckpoint(transaction, {
|
|
164
|
+
documentId: upload.documentId,
|
|
165
|
+
previousRevisionId: document.currentRevisionId,
|
|
166
|
+
revisionId,
|
|
167
|
+
userId,
|
|
168
|
+
now: new Date()
|
|
169
|
+
})
|
|
170
|
+
await transaction
|
|
171
|
+
.updateTable('upload')
|
|
172
|
+
.set({ status: 'committed', finalizationStartedAt: null })
|
|
173
|
+
.where('id', '=', uploadId)
|
|
174
|
+
.execute()
|
|
175
|
+
await quota.commitInTransaction(transaction, uploadId)
|
|
176
|
+
return checkpointed
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** What committing an upload reads of it and its document. */
|
|
180
|
+
function selectUploads() {
|
|
181
|
+
return database
|
|
182
|
+
.selectFrom('upload')
|
|
183
|
+
.innerJoin('document', 'document.id', 'upload.documentId')
|
|
184
|
+
.select([
|
|
185
|
+
'upload.id',
|
|
186
|
+
'upload.documentId',
|
|
187
|
+
'upload.kind',
|
|
188
|
+
'document.workspaceId',
|
|
189
|
+
'upload.baseRevisionId',
|
|
190
|
+
'upload.objectKey',
|
|
191
|
+
'upload.checksum',
|
|
192
|
+
'upload.byteSize',
|
|
193
|
+
'upload.contentType',
|
|
194
|
+
'upload.multipartUploadId',
|
|
195
|
+
'upload.status',
|
|
196
|
+
'upload.expiresAt'
|
|
197
|
+
])
|
|
198
|
+
}
|
|
199
|
+
type UploadRow = Awaited<ReturnType<ReturnType<typeof selectUploads>['executeTakeFirstOrThrow']>>
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Claims a pending upload, completes it when it came in parts, checks the store holds the file
|
|
203
|
+
* it declared, and lands it with `land`. An upload that fails goes back to pending, so its
|
|
204
|
+
* commit can be tried again.
|
|
205
|
+
*/
|
|
206
|
+
async function finalizeUpload<T>(
|
|
207
|
+
upload: UploadRow,
|
|
208
|
+
multipart: CommitUploadInput['multipart'],
|
|
209
|
+
land: (stored: StoredObject) => Promise<T>
|
|
210
|
+
): Promise<T> {
|
|
211
|
+
if (upload.status !== 'pending' || new Date(upload.expiresAt).getTime() <= Date.now()) {
|
|
212
|
+
throw new UploadInvalidError()
|
|
213
|
+
}
|
|
214
|
+
const claimed = await database
|
|
215
|
+
.updateTable('upload')
|
|
216
|
+
.set({ status: 'finalizing', finalizationStartedAt: new Date() })
|
|
217
|
+
.where('id', '=', upload.id)
|
|
218
|
+
.where('status', '=', 'pending')
|
|
219
|
+
.returning('id')
|
|
220
|
+
.executeTakeFirst()
|
|
221
|
+
if (!claimed) throw new UploadInvalidError()
|
|
222
|
+
try {
|
|
223
|
+
if (multipart) {
|
|
224
|
+
if (
|
|
225
|
+
!objects.capabilities.multipartUpload ||
|
|
226
|
+
multipart.uploadId !== upload.multipartUploadId
|
|
227
|
+
) {
|
|
228
|
+
throw new UploadInvalidError()
|
|
229
|
+
}
|
|
230
|
+
await objects.completeUpload({
|
|
231
|
+
key: upload.objectKey,
|
|
232
|
+
uploadId: multipart.uploadId,
|
|
233
|
+
parts: multipart.parts
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
const stored = await objects.head(upload.objectKey)
|
|
237
|
+
if (!stored || !matchesUpload(stored, upload)) throw new UploadInvalidError()
|
|
238
|
+
return await land(stored)
|
|
239
|
+
} catch (error) {
|
|
240
|
+
await database
|
|
241
|
+
.updateTable('upload')
|
|
242
|
+
.set({ status: 'pending', finalizationStartedAt: null })
|
|
243
|
+
.where('id', '=', upload.id)
|
|
244
|
+
.where('status', '=', 'finalizing')
|
|
245
|
+
.execute()
|
|
246
|
+
throw error
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function landImage(upload: UploadRow, stored: StoredObject): Promise<void> {
|
|
251
|
+
return database.transaction().execute((transaction) =>
|
|
252
|
+
images.landInTransaction(transaction, {
|
|
253
|
+
uploadId: upload.id,
|
|
254
|
+
workspaceId: upload.workspaceId,
|
|
255
|
+
objectKey: upload.objectKey,
|
|
256
|
+
stored
|
|
257
|
+
})
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function commitUpload(
|
|
262
|
+
userId: string,
|
|
263
|
+
uploadId: string,
|
|
264
|
+
input: CommitUploadInput
|
|
265
|
+
): Promise<DocumentSummary> {
|
|
266
|
+
const upload = await selectUploads()
|
|
267
|
+
.where('upload.id', '=', uploadId)
|
|
268
|
+
.where('upload.createdBy', '=', userId)
|
|
269
|
+
.executeTakeFirst()
|
|
270
|
+
if (!upload) throw new DocumentNotFoundError()
|
|
271
|
+
const access = await resolveDocumentAccess(database, userId, upload.documentId)
|
|
272
|
+
if (!access) throw new DocumentNotFoundError()
|
|
273
|
+
if (!canEditDocument(access)) throw new DocumentForbiddenError()
|
|
274
|
+
if (input.checksum !== upload.checksum) throw new UploadInvalidError()
|
|
275
|
+
if (upload.status !== 'committed') {
|
|
276
|
+
await refuseAtStorageLimit(upload.documentId)
|
|
277
|
+
const checkpointed =
|
|
278
|
+
upload.kind === 'image'
|
|
279
|
+
? await finalizeUpload(upload, input.multipart, (stored) =>
|
|
280
|
+
landImage(upload, stored).then(() => false)
|
|
281
|
+
)
|
|
282
|
+
: await finalizeUpload(upload, input.multipart, (stored) =>
|
|
283
|
+
database.transaction().execute((transaction) =>
|
|
284
|
+
landRevision(transaction, {
|
|
285
|
+
userId,
|
|
286
|
+
uploadId,
|
|
287
|
+
upload,
|
|
288
|
+
stored,
|
|
289
|
+
revisionId: crypto.randomUUID(),
|
|
290
|
+
storageObjectId: crypto.randomUUID(),
|
|
291
|
+
images: input.images,
|
|
292
|
+
name: input.name,
|
|
293
|
+
kind: input.kind
|
|
294
|
+
})
|
|
295
|
+
)
|
|
296
|
+
)
|
|
297
|
+
if (checkpointed) announceVersions(options.onVersionsChanged, upload.documentId)
|
|
298
|
+
}
|
|
299
|
+
const document = await findDocument(database, userId, upload.documentId)
|
|
300
|
+
if (!document) throw new DocumentNotFoundError()
|
|
301
|
+
return document
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return {
|
|
305
|
+
async createUpload(
|
|
306
|
+
userId: string,
|
|
307
|
+
documentId: string,
|
|
308
|
+
input: CreateUploadInput
|
|
309
|
+
): Promise<CreateDocumentUploadResult> {
|
|
310
|
+
const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
|
|
311
|
+
database,
|
|
312
|
+
options,
|
|
313
|
+
userId,
|
|
314
|
+
documentId
|
|
315
|
+
)
|
|
316
|
+
if (document.currentRevisionId !== input.baseRevisionId) throw new DocumentConflictError()
|
|
317
|
+
if (input.byteSize > maximumFileBytes) throw new UploadInvalidError()
|
|
318
|
+
const uploadId = crypto.randomUUID()
|
|
319
|
+
const objectKey = `documents/${document.workspaceId}/${documentId}/uploads/${uploadId}.fig`
|
|
320
|
+
const expiresAt = new Date(Date.now() + UPLOAD_LIFETIME_MS)
|
|
321
|
+
await database.transaction().execute(async (transaction) => {
|
|
322
|
+
await transaction
|
|
323
|
+
.insertInto('upload')
|
|
324
|
+
.values({
|
|
325
|
+
id: uploadId,
|
|
326
|
+
documentId,
|
|
327
|
+
baseRevisionId: input.baseRevisionId,
|
|
328
|
+
objectKey,
|
|
329
|
+
checksum: input.checksum,
|
|
330
|
+
byteSize: input.byteSize,
|
|
331
|
+
contentType: input.contentType,
|
|
332
|
+
multipartUploadId: null,
|
|
333
|
+
status: 'pending',
|
|
334
|
+
finalizationStartedAt: null,
|
|
335
|
+
createdBy: userId,
|
|
336
|
+
expiresAt
|
|
337
|
+
})
|
|
338
|
+
.execute()
|
|
339
|
+
await quota.reserveInTransaction(transaction, {
|
|
340
|
+
workspaceId: document.workspaceId,
|
|
341
|
+
uploadId,
|
|
342
|
+
bytes: input.byteSize,
|
|
343
|
+
expiresAt,
|
|
344
|
+
maximumBytes: maximumStorageBytes
|
|
345
|
+
})
|
|
346
|
+
})
|
|
347
|
+
let objectUpload
|
|
348
|
+
try {
|
|
349
|
+
objectUpload = await objects.createUpload({
|
|
350
|
+
key: objectKey,
|
|
351
|
+
byteSize: input.byteSize,
|
|
352
|
+
checksum: input.checksum,
|
|
353
|
+
contentType: input.contentType,
|
|
354
|
+
expiresAt
|
|
355
|
+
})
|
|
356
|
+
} catch (error) {
|
|
357
|
+
await quota.release(uploadId)
|
|
358
|
+
await database
|
|
359
|
+
.updateTable('upload')
|
|
360
|
+
.set({ status: 'abandoned' })
|
|
361
|
+
.where('id', '=', uploadId)
|
|
362
|
+
.execute()
|
|
363
|
+
throw error
|
|
364
|
+
}
|
|
365
|
+
if (objectUpload.kind === 'multipart') {
|
|
366
|
+
await database
|
|
367
|
+
.updateTable('upload')
|
|
368
|
+
.set({ multipartUploadId: objectUpload.uploadId })
|
|
369
|
+
.where('id', '=', uploadId)
|
|
370
|
+
.execute()
|
|
371
|
+
}
|
|
372
|
+
return { id: uploadId, upload: objectUpload }
|
|
373
|
+
},
|
|
374
|
+
|
|
375
|
+
/** Uploads for the images a save of the document will name that its workspace lacks. */
|
|
376
|
+
async requestImageUploads(
|
|
377
|
+
userId: string,
|
|
378
|
+
documentId: string,
|
|
379
|
+
input: RequestImageUploadsInput
|
|
380
|
+
): Promise<{ uploads: ImageUploadOffer[] }> {
|
|
381
|
+
const { document, maximumFileBytes, maximumStorageBytes } = await writableDocument(
|
|
382
|
+
database,
|
|
383
|
+
options,
|
|
384
|
+
userId,
|
|
385
|
+
documentId
|
|
386
|
+
)
|
|
387
|
+
if (input.images.some((image) => image.byteSize > maximumFileBytes)) {
|
|
388
|
+
throw new UploadInvalidError()
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
uploads: await images.requestUploads({
|
|
392
|
+
userId,
|
|
393
|
+
documentId,
|
|
394
|
+
workspaceId: document.workspaceId,
|
|
395
|
+
images: input.images,
|
|
396
|
+
expiresAt: new Date(Date.now() + UPLOAD_LIFETIME_MS),
|
|
397
|
+
maximumBytes: maximumStorageBytes
|
|
398
|
+
})
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
commitUpload,
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Commits the image uploads a save asked for in one request, however many there are: each
|
|
405
|
+
* must be an image upload this person made for the document.
|
|
406
|
+
*/
|
|
407
|
+
async commitImageUploads(
|
|
408
|
+
userId: string,
|
|
409
|
+
documentId: string,
|
|
410
|
+
uploads: CommitImageUploadsInput['uploads']
|
|
411
|
+
): Promise<void> {
|
|
412
|
+
const access = await resolveDocumentAccess(database, userId, documentId)
|
|
413
|
+
if (!access) throw new DocumentNotFoundError()
|
|
414
|
+
if (!canEditDocument(access)) throw new DocumentForbiddenError()
|
|
415
|
+
const ids = uniq(uploads.map((upload) => upload.uploadId))
|
|
416
|
+
const rows = new Map(
|
|
417
|
+
(
|
|
418
|
+
await selectUploads()
|
|
419
|
+
.where('upload.id', 'in', ids)
|
|
420
|
+
.where('upload.documentId', '=', documentId)
|
|
421
|
+
.where('upload.createdBy', '=', userId)
|
|
422
|
+
.execute()
|
|
423
|
+
).map((row) => [row.id, row])
|
|
424
|
+
)
|
|
425
|
+
if (rows.size !== ids.length || [...rows.values()].some((row) => row.kind !== 'image')) {
|
|
426
|
+
throw new UploadInvalidError()
|
|
427
|
+
}
|
|
428
|
+
for (const commit of uploads) {
|
|
429
|
+
if (rows.get(commit.uploadId)?.checksum !== commit.checksum) throw new UploadInvalidError()
|
|
430
|
+
}
|
|
431
|
+
if ([...rows.values()].some((row) => row.status !== 'committed')) {
|
|
432
|
+
await refuseAtStorageLimit(documentId)
|
|
433
|
+
}
|
|
434
|
+
// Each image is its own object, so a few are checked and landed at once, each once however
|
|
435
|
+
// often the request names it.
|
|
436
|
+
const land = limitAsync(
|
|
437
|
+
(upload: UploadRow, multipart: CommitUploadInput['multipart']) =>
|
|
438
|
+
finalizeUpload(upload, multipart, (stored) => landImage(upload, stored)),
|
|
439
|
+
IMAGE_COMMIT_CONCURRENCY
|
|
440
|
+
)
|
|
441
|
+
await Promise.all(
|
|
442
|
+
uniqBy(uploads, (commit) => commit.uploadId).map(async (commit) => {
|
|
443
|
+
const upload = rows.get(commit.uploadId)
|
|
444
|
+
if (!upload) throw new UploadInvalidError()
|
|
445
|
+
if (upload.status !== 'committed') await land(upload, commit.multipart)
|
|
446
|
+
})
|
|
447
|
+
)
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
2
3
|
import type { Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
/** How long the newest version may be old before a save keeps the state it replaces. */
|
|
@@ -93,6 +94,6 @@ export function announceVersions(
|
|
|
93
94
|
try {
|
|
94
95
|
onVersionsChanged?.(documentId)
|
|
95
96
|
} catch (error) {
|
|
96
|
-
|
|
97
|
+
logCloud('error', 'Announcing a version history change failed', { error: loggedError(error) })
|
|
97
98
|
}
|
|
98
99
|
}
|