@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.6
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 +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +783 -47
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1651 -382
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +701 -45
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +2138 -34
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +4343 -306
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1338 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +45 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +958 -340
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +93 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +18032 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +12615 -5249
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +17367 -8567
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +13587 -7333
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +11601 -5401
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +84 -62
- package/dist/server2.mjs.map +1 -1
- package/package.json +31 -5
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +11 -3
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +195 -10
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +15 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +52 -473
- package/src/client/auth/client.ts +22 -3
- package/src/client/comments.ts +205 -0
- package/src/client/community.ts +40 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +39 -11
- package/src/client/documents.ts +160 -0
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +4 -0
- package/src/client/portal/account.ts +73 -3
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +6 -2
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +110 -0
- package/src/contract/account.ts +111 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +235 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/comments.ts +223 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +94 -5
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +64 -5
- package/src/contract/links.ts +62 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +3 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +62 -20
- package/src/contract/workspaces.ts +5 -0
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +169 -0
- package/src/email/locales/de.json +169 -0
- package/src/email/locales/es.json +169 -0
- package/src/email/locales/fr.json +169 -0
- package/src/email/locales/it.json +169 -0
- package/src/email/locales/ja.json +169 -0
- package/src/email/locales/pl.json +169 -0
- package/src/email/locales/pt-br.json +169 -0
- package/src/email/locales/ru.json +169 -0
- package/src/email/locales/zh-cn.json +169 -0
- package/src/email/messages.ts +235 -0
- package/src/email/render.ts +160 -276
- package/src/email/samples.ts +142 -0
- package/src/email/schema.ts +98 -8
- package/src/email/templates/BrandEmail.vue +56 -0
- package/src/email/templates/CommentNotification.vue +39 -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/EnrollmentInvitation.vue +53 -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 +47 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +15 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/{server/errors → errors}/posthog.ts +28 -4
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +28 -7
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +14 -1
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +14 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +20 -4
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +5 -2
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +40 -4
- package/src/server/account/routes.ts +305 -253
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +44 -6
- package/src/server/app/checks.ts +89 -0
- package/src/server/app/cors.ts +44 -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 +123 -0
- package/src/server/app/services.ts +219 -0
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +36 -3
- package/src/server/auth/email.ts +33 -18
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +165 -189
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +190 -2
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/session.ts +2 -0
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +8 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +80 -166
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- 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 +35 -10
- package/src/server/config/index.ts +2 -2
- package/src/server/config/public.ts +25 -6
- package/src/server/config/schema.ts +53 -293
- package/src/server/config/toml/schema.ts +330 -0
- package/src/server/db/index.ts +3 -1
- package/src/server/db/migrate.ts +33 -1
- 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/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +122 -3
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +31 -407
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +86 -26
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +39 -6
- package/src/server/email/types.ts +13 -0
- package/src/server/errors/index.ts +49 -12
- package/src/server/index.ts +27 -6
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +3 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +5 -3
- package/src/server/reports/service.ts +27 -3
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +3 -4
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +17 -8
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +35 -24
- package/src/server/workspaces/members.ts +57 -31
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +31 -22
- package/src/services/config.ts +48 -8
- package/src/services/metering.ts +15 -27
- package/src/services/usage.ts +6 -14
- 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/app.ts +0 -453
|
@@ -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,14 @@ 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,
|
|
19
|
+
DOWNLOAD_LIFETIME_MS,
|
|
29
20
|
documentImageLinks,
|
|
30
21
|
revisionImageDownloads
|
|
31
22
|
} from '#cloud/server/documents/images'
|
|
32
|
-
import { writableDocument } from '#cloud/server/documents/limits'
|
|
33
23
|
import {
|
|
34
24
|
findDocument,
|
|
35
25
|
insertDocument,
|
|
@@ -37,25 +27,23 @@ import {
|
|
|
37
27
|
listSharedDocuments,
|
|
38
28
|
workspaceRole
|
|
39
29
|
} from '#cloud/server/documents/repository'
|
|
40
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
documentSummary,
|
|
32
|
+
getDocumentSummaryRow,
|
|
33
|
+
getRetainedDocumentSummaryRow
|
|
34
|
+
} from '#cloud/server/documents/summary'
|
|
41
35
|
import {
|
|
42
36
|
createDocumentTrash,
|
|
43
37
|
DEFAULT_TRASH_OPTIONS,
|
|
44
38
|
type DocumentTrashOptions
|
|
45
39
|
} from '#cloud/server/documents/trash'
|
|
46
|
-
import
|
|
47
|
-
import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
|
|
40
|
+
import { createDocumentUploads } from '#cloud/server/documents/uploads'
|
|
48
41
|
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
49
|
-
import type { ObjectStore
|
|
42
|
+
import type { ObjectStore } from '#cloud/server/objects'
|
|
50
43
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
51
|
-
import {
|
|
52
|
-
import {
|
|
53
|
-
import type { Kysely
|
|
54
|
-
|
|
55
|
-
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
|
|
44
|
+
import { storageLimitReached } from '#cloud/server/policy/storage'
|
|
45
|
+
import { createStorageQuotaService, StorageQuotaExceededError } from '#cloud/server/quota'
|
|
46
|
+
import type { Kysely } from 'kysely'
|
|
59
47
|
|
|
60
48
|
export type DocumentServiceOptions = {
|
|
61
49
|
policy?: CloudPolicy
|
|
@@ -68,18 +56,6 @@ export type DocumentServiceOptions = {
|
|
|
68
56
|
reportError?: CloudErrorReporter
|
|
69
57
|
}
|
|
70
58
|
|
|
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
59
|
export function createDocumentService(
|
|
84
60
|
database: Kysely<CloudDatabase>,
|
|
85
61
|
objects: ObjectStore,
|
|
@@ -89,109 +65,7 @@ export function createDocumentService(
|
|
|
89
65
|
const quota = createStorageQuotaService(database)
|
|
90
66
|
const images = createDocumentImages(database, objects, quota)
|
|
91
67
|
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
|
-
|
|
68
|
+
const uploads = createDocumentUploads(database, objects, { quota, images, options })
|
|
195
69
|
/** Links for images a live room names, by checksum. */
|
|
196
70
|
async function imageLinksOf(documentId: string, checksums: string[]) {
|
|
197
71
|
const expiresAt = new Date(Date.now() + DOWNLOAD_LIFETIME_MS)
|
|
@@ -200,8 +74,11 @@ export function createDocumentService(
|
|
|
200
74
|
}
|
|
201
75
|
}
|
|
202
76
|
|
|
203
|
-
async function createDownload(
|
|
204
|
-
|
|
77
|
+
async function createDownload(
|
|
78
|
+
documentId: string,
|
|
79
|
+
lookup: typeof getDocumentSummaryRow = getDocumentSummaryRow
|
|
80
|
+
): Promise<DocumentDownload> {
|
|
81
|
+
const row = await lookup(database, documentId)
|
|
205
82
|
if (!row?.currentRevisionId) throw new DocumentNotFoundError()
|
|
206
83
|
const document: DocumentSummary = documentSummary(row)
|
|
207
84
|
const revision = await database
|
|
@@ -230,130 +107,6 @@ export function createDocumentService(
|
|
|
230
107
|
}
|
|
231
108
|
}
|
|
232
109
|
|
|
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
110
|
return {
|
|
358
111
|
async cleanupExpiredUploads(now = new Date()): Promise<number> {
|
|
359
112
|
const result = await cleanup.cleanupExpiredUploads({
|
|
@@ -411,6 +164,14 @@ export function createDocumentService(
|
|
|
411
164
|
return createDownload(documentId)
|
|
412
165
|
},
|
|
413
166
|
|
|
167
|
+
/**
|
|
168
|
+
* A document as it is now for a deployment administrator, taken down or in Trash included,
|
|
169
|
+
* to look at what was reported or blocked; the caller checks the administrator.
|
|
170
|
+
*/
|
|
171
|
+
async downloadRetained(documentId: string): Promise<DocumentDownload> {
|
|
172
|
+
return createDownload(documentId, getRetainedDocumentSummaryRow)
|
|
173
|
+
},
|
|
174
|
+
|
|
414
175
|
/** Links for images a live room names, for someone who may view the document. */
|
|
415
176
|
async imageLinks(userId: string, documentId: string, checksums: string[]) {
|
|
416
177
|
const access = await resolveDocumentAccess(database, userId, documentId)
|
|
@@ -451,6 +212,9 @@ export function createDocumentService(
|
|
|
451
212
|
const role = await workspaceRole(database, userId, workspaceId)
|
|
452
213
|
if (!role) throw new DocumentNotFoundError()
|
|
453
214
|
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
215
|
+
if (await storageLimitReached(database, options.policy, { workspaceId })) {
|
|
216
|
+
throw new StorageQuotaExceededError('The owner holds their whole storage allowance')
|
|
217
|
+
}
|
|
454
218
|
const documentId = input.id ?? crypto.randomUUID()
|
|
455
219
|
await insertDocument(database, {
|
|
456
220
|
id: documentId,
|
|
@@ -463,148 +227,8 @@ export function createDocumentService(
|
|
|
463
227
|
if (!document) throw new DocumentNotFoundError()
|
|
464
228
|
return document
|
|
465
229
|
},
|
|
466
|
-
|
|
467
|
-
|
|
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
|
-
}
|
|
230
|
+
/** Saving: uploads of revisions and their images, and committing them. */
|
|
231
|
+
...uploads
|
|
608
232
|
}
|
|
609
233
|
}
|
|
610
234
|
|
|
@@ -36,3 +36,18 @@ export async function getDocumentSummaryRow(
|
|
|
36
36
|
.where('disabledAt', 'is', null)
|
|
37
37
|
.executeTakeFirst()
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A document the server still keeps, even one in Trash or taken down, which only a deployment
|
|
42
|
+
* administrator may reach; ordinary access goes through `getDocumentSummaryRow`.
|
|
43
|
+
*/
|
|
44
|
+
export async function getRetainedDocumentSummaryRow(
|
|
45
|
+
database: DocumentDatabase,
|
|
46
|
+
documentId: string
|
|
47
|
+
): Promise<DocumentSummaryRow | undefined> {
|
|
48
|
+
return database
|
|
49
|
+
.selectFrom('document')
|
|
50
|
+
.select(SUMMARY_COLUMNS)
|
|
51
|
+
.where('id', '=', documentId)
|
|
52
|
+
.executeTakeFirst()
|
|
53
|
+
}
|