Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(docx): wait for upload extraction before opening documents (#8372)

gbirman67baf768f45d
diff --git a/Cargo.lock b/Cargo.lock
index 1b085b2955..d41d7ed289 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6585,6 +6585,7 @@ dependencies = [
"secretsmanager_client",
"serde",
"serde_json",
+ "sha2 0.10.9",
"slack_integration",
"soup",
"soup_realtime",
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index 695b6fb611..fdc9f583cf 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -110,6 +110,9 @@ ownership. Cancel, logout, and the ten-minute timeout retire pending callbacks.
- Keep native booking exit navigation in the route shell, outside page loading boundaries, and use the router so the native hash route stays intact.
- Diagnose native full reloads with `native_webview_started` and native launch/termination/update counters; termination coverage is iOS/macOS only, and a pageview alone also includes SPA navigation. See `tauri/src-tauri/README.md`.
- All API/network calls live in service-clients.
+- Keep early DOCX opens loading while exports return processing (409); retry only
+ that response, with a bounded wait. See the Word editor walkthrough in
+ `docs/agent-guide/documents.md` at the repository root.
- Add Google Meet account hints only to join actions using the displayed calendar's
connected email; keep stored and copied conference links unchanged. See the
[calendar walkthrough](../../docs/agent-guide/calendar.md).
diff --git a/apps/web/src/features/block-write/browser-test/README.md b/apps/web/src/features/block-write/browser-test/README.md
index 769bbff102..3fb3746aaf 100644
--- a/apps/web/src/features/block-write/browser-test/README.md
+++ b/apps/web/src/features/block-write/browser-test/README.md
@@ -35,6 +35,9 @@ to wasm, `crates/docx_engine`) in a worker; build it first.
- `agent.browser.e2e.ts` runs the AI tools' `runDocxAgentRequest` as its own
sync peer, exactly as the editing worker does, and checks that reads see
the live document and that edits reach an open editor and survive a reopen.
+- `upload.browser.e2e.ts` uses local mode and the production export retry policy
+ to hold an early open at a processing response, then renders the extracted
+ DOCX without reopening. It does not need the sync Worker build.
```sh
(\cd services/sync-service && just worker-build) # once, builds build/worker/shim.mjs
diff --git a/apps/web/src/features/block-write/browser-test/fixture.tsx b/apps/web/src/features/block-write/browser-test/fixture.tsx
index 12ff54e9f1..4e48eb9983 100644
--- a/apps/web/src/features/block-write/browser-test/fixture.tsx
+++ b/apps/web/src/features/block-write/browser-test/fixture.tsx
@@ -1,5 +1,6 @@
import '@fontsource-variable/inter';
import '../../../index.css';
+import { ThrownResultError } from '@core/util/result';
import { noopChatter } from '@macro-inc/collaboration/collab/chatter';
import type { SnapshotStore } from '@macro-inc/collaboration/collab/snapshot-store';
import { InMemoryWALStore } from '@macro-inc/collaboration/collab/wal';
@@ -16,6 +17,7 @@ import {
} from '@macro-inc/collaboration/sync-service/source';
import { createWebsocketStateSignal } from '@macro-inc/collaboration/websocket/solid/state-signal';
import type { MessageListItem } from '@service-storage/messages';
+import { QueryClient } from '@tanstack/solid-query';
import type { LoroDoc } from 'loro-crdt';
import { createSignal, For, onCleanup, Show } from 'solid-js';
import { render } from 'solid-js/web';
@@ -26,6 +28,7 @@ import ComplexMsa from '../core/fixtures/complex-msa.docx?url';
import MutualNda from '../core/fixtures/mutual-nda.docx?url';
import type { LocatedThread } from '../primitives/create-docx-comments';
import type { DocxEditor } from '../primitives/create-docx-editor';
+import { docxOriginalQueryOptions } from '../queries/docx-original';
import { createDocxSession } from '../queries/docx-session';
import { DocxEditorView } from '../views/DocxEditorView';
import { DocxMarginLayout } from '../views/DocxMarginLayout';
@@ -490,8 +493,20 @@ function LocalFixture() {
const pair = params.get('local') === 'pair';
const [docs, setDocs] = createSignal<LoroDoc[]>();
const [editor, setEditor] = createSignal<DocxEditor>();
+ const client = new QueryClient();
+ onCleanup(() => client.clear());
const seed = async () => {
- const original = new Uint8Array(await (await fetch(fixture)).arrayBuffer());
+ const original = await client.fetchQuery(
+ docxOriginalQueryOptions(documentId, async () => {
+ const response = await fetch(fixture);
+ if (response.status === 409)
+ throw new ThrownResultError([
+ { code: 'CONFLICT', message: 'Document is still processing.' },
+ ]);
+ if (!response.ok) throw new Error('Unable to download fixture.');
+ return new Uint8Array(await response.arrayBuffer());
+ })
+ );
const snapshot = await buildSeedSnapshot(original);
const { LoroDoc } = await import('loro-crdt');
const a = new LoroDoc();
diff --git a/apps/web/src/features/block-write/browser-test/upload.browser.e2e.ts b/apps/web/src/features/block-write/browser-test/upload.browser.e2e.ts
new file mode 100644
index 0000000000..a3a57d149c
--- /dev/null
+++ b/apps/web/src/features/block-write/browser-test/upload.browser.e2e.ts
@@ -0,0 +1,56 @@
+import { readFile } from 'node:fs/promises';
+import { expect, test } from '@playwright/test';
+
+test('an early open waits for extraction and renders without reopening', async ({
+ page,
+}) => {
+ const docx = await readFile(
+ new URL('../core/fixtures/mutual-nda.docx', import.meta.url)
+ );
+ let requests = 0;
+ let finishExtraction = () => {};
+ const extracted = new Promise<void>((resolve) => {
+ finishExtraction = resolve;
+ });
+ await page.route(
+ (url) => url.pathname === '/pending-export.docx',
+ async (route) => {
+ requests++;
+ if (requests === 1) {
+ await route.fulfill({
+ status: 409,
+ body: 'Document is still processing.',
+ });
+ return;
+ }
+ await extracted;
+ await route.fulfill({
+ status: 200,
+ contentType:
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ body: docx,
+ });
+ }
+ );
+
+ await page.goto('/?local&src=/pending-export.docx');
+ await expect.poll(() => requests).toBe(2);
+ await expect(page.getByText('seeding…')).toBeVisible();
+ await expect(page.locator('[data-docx-page]')).toHaveCount(0);
+ finishExtraction();
+
+ await expect
+ .poll(() => page.evaluate(() => window.docxFixture?.ready()))
+ .toBe(true);
+ await expect(page.locator('[data-docx-page]').first()).toBeVisible();
+ const paragraphs = await page.evaluate(() =>
+ window.docxFixture?.paragraphs()
+ );
+ expect(paragraphs?.join('\n')).toContain('MUTUAL NON-DISCLOSURE AGREEMENT');
+ expect(requests).toBe(2);
+ if (process.env.DOCX_E2E_SHOTS) {
+ await page.screenshot({
+ path: `${process.env.DOCX_E2E_SHOTS}/upload-ready.png`,
+ });
+ }
+});
diff --git a/apps/web/src/features/block-write/queries/docx-document.test.ts b/apps/web/src/features/block-write/queries/docx-document.test.ts
new file mode 100644
index 0000000000..a5446b3717
--- /dev/null
+++ b/apps/web/src/features/block-write/queries/docx-document.test.ts
@@ -0,0 +1,68 @@
+import { ThrownResultError } from '@core/util/result';
+import { queryClient } from '@queries/client';
+import { downloadExportedDocument } from '@service-storage/util/downloadExportedDocument';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { fetchOriginalDocx } from './docx-document';
+
+vi.mock('@queries/client', async () => {
+ const { QueryClient } = await import('@tanstack/solid-query');
+ return { queryClient: new QueryClient() };
+});
+vi.mock('@service-storage/util/downloadExportedDocument', () => ({
+ downloadExportedDocument: vi.fn(),
+}));
+vi.mock('@service-sync/client', () => ({ syncServiceClient: {} }));
+vi.mock('@service-sync/source', () => ({ createSyncServiceSource: vi.fn() }));
+vi.mock('../core/docx-seed', () => ({ buildSeedSnapshot: vi.fn() }));
+
+const download = vi.mocked(downloadExportedDocument);
+beforeEach(() => {
+ vi.useFakeTimers();
+ download.mockReset();
+});
+afterEach(() => {
+ queryClient.clear();
+ vi.useRealTimers();
+});
+
+it('keeps an early open pending until extraction finishes and shares the download', async () => {
+ const bytes = new Uint8Array([80, 75, 3, 4]);
+ download
+ .mockRejectedValueOnce(
+ new ThrownResultError([{ code: 'CONFLICT', message: 'processing' }])
+ )
+ .mockResolvedValueOnce(bytes);
+ const completed = vi.fn();
+ const first = fetchOriginalDocx('upload').then(completed);
+ const second = fetchOriginalDocx('upload');
+ await vi.advanceTimersByTimeAsync(999);
+ expect(completed).not.toHaveBeenCalled();
+ expect(download).toHaveBeenCalledOnce();
+ await vi.advanceTimersByTimeAsync(1);
+ await first;
+ expect(await second).toEqual(bytes);
+ expect(completed).toHaveBeenCalledWith(bytes);
+ expect(download).toHaveBeenCalledTimes(2);
+});
+
+it('stops waiting after the processing deadline and allows a later reopen', async () => {
+ download.mockRejectedValue(
+ new ThrownResultError([{ code: 'CONFLICT', message: 'processing' }])
+ );
+ const first = fetchOriginalDocx('upload');
+ const rejected = expect(first).rejects.toBeInstanceOf(ThrownResultError);
+ await vi.advanceTimersByTimeAsync(30_000);
+ await rejected;
+
+ const bytes = new Uint8Array([80, 75, 3, 4]);
+ download.mockResolvedValue(bytes);
+ expect(await fetchOriginalDocx('upload')).toEqual(bytes);
+});
+
+it('does not retry a failed export that is not a processing response', async () => {
+ download.mockRejectedValue(new Error('Unable to export the document.'));
+ await expect(fetchOriginalDocx('upload')).rejects.toThrow(
+ 'Unable to export the document.'
+ );
+ expect(download).toHaveBeenCalledOnce();
+});
diff --git a/apps/web/src/features/block-write/queries/docx-document.ts b/apps/web/src/features/block-write/queries/docx-document.ts
index 5b32ca99cd..33644fb97a 100644
--- a/apps/web/src/features/block-write/queries/docx-document.ts
+++ b/apps/web/src/features/block-write/queries/docx-document.ts
@@ -1,23 +1,43 @@
+import { thrownResultErrorHasCode } from '@core/util/result';
+import { Telemetry } from '@macro-inc/observability';
import { queryClient } from '@queries/client';
import { downloadExportedDocument } from '@service-storage/util/downloadExportedDocument';
import { syncServiceClient } from '@service-sync/client';
import { createSyncServiceSource } from '@service-sync/source';
import type { DocumentSyncAuthorization } from '@service-sync/source/authorization';
import { buildSeedSnapshot } from '../core/docx-seed';
+import { docxOriginalQueryOptions } from './docx-original';
import type { DocxConnection } from './docx-session';
-const docxKeys = {
- original: (documentId: string) => ['docx', 'original', documentId] as const,
-};
-
/**
* The DOCX as uploaded, rebuilt by document storage from its stored parts.
* Cached so views opening the same file share one download.
*/
export function fetchOriginalDocx(documentId: string): Promise<Uint8Array> {
- return queryClient.fetchQuery({
- queryKey: docxKeys.original(documentId),
- queryFn: () => downloadExportedDocument({ documentId }),
+ return Telemetry.span('docx.original.wait', async (span) => {
+ span.setAttr('document.id', documentId);
+ let attempts = 0;
+ try {
+ const original = await queryClient.fetchQuery(
+ docxOriginalQueryOptions(documentId, () => {
+ attempts++;
+ return span.run(() => downloadExportedDocument({ documentId }));
+ })
+ );
+ span.setAttr('docx.export.outcome', 'ready');
+ return original;
+ } catch (error) {
+ span.setAttr(
+ 'docx.export.outcome',
+ thrownResultErrorHasCode(error, 'CONFLICT')
+ ? 'processing_timeout'
+ : 'failed'
+ );
+ span.error(error);
+ throw error;
+ } finally {
+ span.setAttr('docx.export.retry_count', Math.max(0, attempts - 1));
+ }
});
}
diff --git a/apps/web/src/features/block-write/queries/docx-original.ts b/apps/web/src/features/block-write/queries/docx-original.ts
new file mode 100644
index 0000000000..8aa9974fa8
--- /dev/null
+++ b/apps/web/src/features/block-write/queries/docx-original.ts
@@ -0,0 +1,19 @@
+import { thrownResultErrorHasCode } from '@core/util/result';
+import { queryOptions } from '@tanstack/solid-query';
+import { docxKeys } from './keys';
+
+/** Share the original download and wait briefly for a newly uploaded DOCX. */
+export function docxOriginalQueryOptions(
+ documentId: string,
+ download: () => Promise<Uint8Array>
+) {
+ const deadline = Date.now() + 30_000;
+ return queryOptions({
+ queryKey: docxKeys.original(documentId).queryKey,
+ queryFn: download,
+ // Only a confirmed processing response is retryable.
+ retry: (_failureCount: number, error: Error) =>
+ thrownResultErrorHasCode(error, 'CONFLICT') && Date.now() < deadline,
+ retryDelay: 1_000,
+ });
+}
diff --git a/apps/web/src/features/block-write/queries/docx-session.ts b/apps/web/src/features/block-write/queries/docx-session.ts
index 6c5d1f9363..16e1aece96 100644
--- a/apps/web/src/features/block-write/queries/docx-session.ts
+++ b/apps/web/src/features/block-write/queries/docx-session.ts
@@ -1,4 +1,5 @@
import { fireAndForget } from '@core/util/fireAndForget';
+import { thrownResultErrorHasCode } from '@core/util/result';
import { createAwareness } from '@macro-inc/collaboration/collab/awareness';
import type { Chatter } from '@macro-inc/collaboration/collab/chatter';
import { createSyncEngine } from '@macro-inc/collaboration/collab/engine';
@@ -313,7 +314,12 @@ export function createDocxSession(options: DocxSessionOptions): DocxSession {
hydrate().catch((cause: unknown) => {
console.error('DOCX session failed to open', cause);
if (!disposed)
- setState({ t: 'error', message: 'Unable to open this document.' });
+ setState({
+ t: 'error',
+ message: thrownResultErrorHasCode(cause, 'CONFLICT')
+ ? 'This document is still processing. Try opening it again shortly.'
+ : 'Unable to open this document.',
+ });
});
onCleanup(() => {
diff --git a/apps/web/src/features/block-write/queries/keys.ts b/apps/web/src/features/block-write/queries/keys.ts
new file mode 100644
index 0000000000..ac4f2cd383
--- /dev/null
+++ b/apps/web/src/features/block-write/queries/keys.ts
@@ -0,0 +1,5 @@
+import { createQueryKeys } from '@lukemorales/query-key-factory';
+
+export const docxKeys = createQueryKeys('docx', {
+ original: (documentId: string) => ({ queryKey: [documentId] }),
+});
diff --git a/apps/web/src/lib/service-clients/service-storage/openapi.json b/apps/web/src/lib/service-clients/service-storage/openapi.json
index 0f95aa2822..44904715ea 100644
--- a/apps/web/src/lib/service-clients/service-storage/openapi.json
+++ b/apps/web/src/lib/service-clients/service-storage/openapi.json
@@ -10322,6 +10322,16 @@
}
}
},
+ "409": {
+ "description": "DOCX upload is still processing; retry the export.",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
diff --git a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts
index ab6f6930c5..c34cc01d51 100644
--- a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts
+++ b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts
@@ -1,3 +1,4 @@
+import { ThrownResultError } from '@core/util/result';
import { err, ok } from 'neverthrow';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { downloadExportedDocument } from './downloadExportedDocument';
@@ -42,7 +43,9 @@ describe('downloadExportedDocument', () => {
);
await expect(
downloadExportedDocument({ documentId: 'doc' })
- ).rejects.toThrow('Unable to export the document.');
+ ).rejects.toMatchObject({
+ errors: [{ code: 'SERVER_ERROR', message: 'boom' }],
+ });
mocks.exportDocument.mockResolvedValueOnce(
ok({ presigned_url: 'https://bucket/export.docx' })
);
@@ -53,4 +56,18 @@ describe('downloadExportedDocument', () => {
downloadExportedDocument({ documentId: 'doc' })
).rejects.toThrow('Unable to download the exported document');
});
+
+ it('identifies a processing response without downloading or caching an empty file', async () => {
+ mocks.exportDocument.mockResolvedValue(
+ err([{ code: 'CONFLICT', message: 'Resource conflict' }])
+ );
+ await expect(
+ downloadExportedDocument({ documentId: 'doc' })
+ ).rejects.toEqual(
+ new ThrownResultError([
+ { code: 'CONFLICT', message: 'Resource conflict' },
+ ])
+ );
+ expect(mocks.platformFetch).not.toHaveBeenCalled();
+ });
});
diff --git a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
index 5808e00a15..656314627c 100644
--- a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
+++ b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
@@ -1,3 +1,4 @@
+import { throwOnErr } from '@core/util/result';
import { storageServiceClient } from '../client';
import { fetchPresigned } from './fetchPresigned';
@@ -10,12 +11,10 @@ export async function downloadExportedDocument({
}: {
documentId: string;
}): Promise<Uint8Array> {
- const exported = await storageServiceClient.exportDocument({ documentId });
- if (exported.isErr()) throw new Error('Unable to export the document.');
- const bytes = await fetchPresigned(
- exported.value.presigned_url,
- 'arrayBuffer'
+ const exported = await throwOnErr(() =>
+ storageServiceClient.exportDocument({ documentId })
);
+ const bytes = await fetchPresigned(exported.presigned_url, 'arrayBuffer');
if (bytes.isErr())
throw new Error(
`Unable to download the exported document: ${bytes.error[0]?.message}`
diff --git a/crates/documents/AGENTS.md b/crates/documents/AGENTS.md
index b9284a6598..964745bc78 100644
--- a/crates/documents/AGENTS.md
+++ b/crates/documents/AGENTS.md
@@ -14,6 +14,8 @@ word docs, Illustrator/Photoshop files).
adapters implement.
- `src/domain/content.rs`: document content storage model (markdown vs legacy
Office formats, S3 locations).
+- `src/domain/docx_export.rs`: DOCX export readiness policy; empty parts mean
+ extraction is pending. Its entry point requires a checked document receipt.
- `src/inbound/toolset.rs`: one submodule per AI document tool (`create_document`,
`edit_document`, `word_document`, `spreadsheet`, `presentation`, ...).
- `src/inbound/axum_router.rs`: the HTTP surface; `axum_router/get_text.rs`
diff --git a/crates/documents/src/domain.rs b/crates/documents/src/domain.rs
index e4e4b36a9f..317d04eedc 100644
--- a/crates/documents/src/domain.rs
+++ b/crates/documents/src/domain.rs
@@ -9,6 +9,7 @@ pub mod branch_name;
#[cfg(feature = "ai_tools")]
pub mod comments;
pub mod content;
+pub mod docx_export;
/// Unified entity-mutation capability impls.
#[cfg(feature = "service")]
pub mod entity_mutation;
diff --git a/crates/documents/src/domain/docx_export.rs b/crates/documents/src/domain/docx_export.rs
new file mode 100644
index 0000000000..8242117ed2
--- /dev/null
+++ b/crates/documents/src/domain/docx_export.rs
@@ -0,0 +1,70 @@
+//! Export an uploaded Word document only after its extracted parts are ready.
+
+use entity_access::domain::models::{EntityAccessReceipt, EntityType, ViewAccessLevel};
+use model::document::BomPart;
+use std::future::Future;
+
+use super::models::DocumentError;
+
+/// Persistence capabilities for exporting an uploaded DOCX.
+#[cfg_attr(test, mockall::automock(type Err = std::io::Error;))]
+pub trait DocxExportStore: Send + Sync {
+ /// The adapter's persistence error.
+ type Err: std::error::Error + Send + Sync + 'static;
+
+ /// Read the latest version's parts from the primary database. The unzip
+ /// worker inserts these atomically after their objects have been stored.
+ fn parts(
+ &self,
+ document_id: &str,
+ ) -> impl Future<Output = Result<Vec<BomPart>, Self::Err>> + Send;
+
+ /// Export this exact set of parts and return its signed download URL.
+ /// Cache identity must include the parts, so incomplete legacy exports
+ /// and exports of earlier versions cannot be reused.
+ fn export_parts(
+ &self,
+ document_id: &str,
+ parts: Vec<BomPart>,
+ ) -> impl Future<Output = Result<String, Self::Err>> + Send;
+}
+
+/// An export may be requested while the upload is still being extracted.
+#[derive(Debug, PartialEq, Eq)]
+pub enum DocxExport {
+ /// The version exists, but the worker has not saved its parts yet.
+ Processing,
+ /// A signed URL for a complete export.
+ Ready(String),
+}
+
+/// Readiness is determined by the parts, independently of PDF conversion or
+/// the legacy `uploaded` flag. Never cache the absence of parts as an empty ZIP.
+#[tracing::instrument(skip_all, fields(document_id = %access.entity().entity_id, part_count), err)]
+pub async fn export_docx(
+ store: &impl DocxExportStore,
+ access: EntityAccessReceipt<ViewAccessLevel>,
+) -> Result<DocxExport, DocumentError> {
+ if access.entity().entity_type != EntityType::Document {
+ return Err(DocumentError::Unauthorized);
+ }
+ let document_id = &access.entity().entity_id;
+ let parts = store
+ .parts(document_id)
+ .await
+ .map_err(|error| DocumentError::Internal(error.into()))?;
+ tracing::Span::current().record("part_count", parts.len());
+ if parts.is_empty() {
+ tracing::info!("DOCX export waiting for upload extraction");
+ return Ok(DocxExport::Processing);
+ }
+ Ok(DocxExport::Ready(
+ store
+ .export_parts(document_id, parts)
+ .await
+ .map_err(|error| DocumentError::Internal(error.into()))?,
+ ))
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/documents/src/domain/docx_export/test.rs b/crates/documents/src/domain/docx_export/test.rs
new file mode 100644
index 0000000000..45aa74fccb
--- /dev/null
+++ b/crates/documents/src/domain/docx_export/test.rs
@@ -0,0 +1,87 @@
+use super::*;
+use entity_access::domain::models::{Entity, EntityPermission};
+use macro_user_id::user_id::MacroUserIdStr;
+use models_permissions::share_permission::access_level::AccessLevel;
+
+fn receipt(entity_type: EntityType) -> EntityAccessReceipt<ViewAccessLevel> {
+ EntityAccessReceipt::try_new_authenticated_user(
+ MacroUserIdStr::try_from("macro|[email protected]".to_owned()).unwrap(),
+ Entity {
+ entity_type,
+ entity_id: "document-id".to_owned(),
+ },
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::View,
+ },
+ )
+ .unwrap()
+}
+
+#[tokio::test]
+async fn early_open_waits_without_exporting_then_succeeds_after_extraction() {
+ let mut store = MockDocxExportStore::new();
+ let mut sequence = mockall::Sequence::new();
+ store
+ .expect_parts()
+ .withf(|id| id == "document-id")
+ .times(1)
+ .in_sequence(&mut sequence)
+ .returning(|_| Box::pin(async { Ok(vec![]) }));
+ store
+ .expect_parts()
+ .times(1)
+ .in_sequence(&mut sequence)
+ .returning(|_| {
+ Box::pin(async {
+ Ok(vec![BomPart {
+ id: "part-id".into(),
+ path: "word/document.xml".into(),
+ sha: "content-sha".into(),
+ }])
+ })
+ });
+ store
+ .expect_export_parts()
+ .withf(|id, parts| {
+ id == "document-id" && parts.len() == 1 && parts[0].path == "word/document.xml"
+ })
+ .times(1)
+ .in_sequence(&mut sequence)
+ .returning(|_, _| Box::pin(async { Ok("https://download/complete.docx".into()) }));
+
+ assert_eq!(
+ export_docx(&store, receipt(EntityType::Document))
+ .await
+ .unwrap(),
+ DocxExport::Processing
+ );
+ assert_eq!(
+ export_docx(&store, receipt(EntityType::Document))
+ .await
+ .unwrap(),
+ DocxExport::Ready("https://download/complete.docx".into())
+ );
+}
+
+#[tokio::test]
+async fn database_failure_is_not_reported_as_processing() {
+ let mut store = MockDocxExportStore::new();
+ store
+ .expect_parts()
+ .times(1)
+ .returning(|_| Box::pin(async { Err(std::io::Error::other("database unavailable")) }));
+ store.expect_export_parts().never();
+ assert!(matches!(
+ export_docx(&store, receipt(EntityType::Document)).await,
+ Err(DocumentError::Internal(_))
+ ));
+}
+
+#[tokio::test]
+async fn unrelated_entity_receipt_cannot_export_a_document() {
+ let store = MockDocxExportStore::new();
+ assert!(matches!(
+ export_docx(&store, receipt(EntityType::Project)).await,
+ Err(DocumentError::Unauthorized)
+ ));
+}
diff --git a/docs/agent-guide/documents.md b/docs/agent-guide/documents.md
index 260d251e96..eead6b2be0 100644
--- a/docs/agent-guide/documents.md
+++ b/docs/agent-guide/documents.md
@@ -2713,6 +2713,13 @@ in the DOM. Edits sync through the sync service as you type: every open copy
updates live and shows each collaborator's caret with their name
(`[data-docx-peer]`).
+- Open a newly uploaded DOCX while it is still processing. The view stays
+ loading and retries the original export once per second for up to 30 seconds,
+ then renders as soon as extraction finishes without needing a reopen. If
+ processing takes longer, it asks you to try opening it again shortly.
+ A first-open sync `/exists` 404 is expected: the editor seeds collaboration
+ from the stored upload. The export endpoint returns 409 while parts are
+ missing and never publishes an empty archive.
- Pages are `[data-docx-page="<index>"]` elements. Click a page to place the
caret, drag to select, double-click for a word and triple-click for a
paragraph. On a touch screen a swipe scrolls, a tap places the caret, and
diff --git a/packages/sdk-docs/reference/generated/storage/types.gen/type-aliases/ExportDocumentErrors.md b/packages/sdk-docs/reference/generated/storage/types.gen/type-aliases/ExportDocumentErrors.md
index 51b5530cad..299d5420d8 100644
--- a/packages/sdk-docs/reference/generated/storage/types.gen/type-aliases/ExportDocumentErrors.md
+++ b/packages/sdk-docs/reference/generated/storage/types.gen/type-aliases/ExportDocumentErrors.md
@@ -1,11 +1,12 @@
# Type Alias: ExportDocumentErrors
-> **ExportDocumentErrors** = \{ `401`: [`GenericErrorResponse`](GenericErrorResponse.md); `404`: [`GenericErrorResponse`](GenericErrorResponse.md); `500`: [`GenericErrorResponse`](GenericErrorResponse.md); \}
+> **ExportDocumentErrors** = \{ `401`: [`GenericErrorResponse`](GenericErrorResponse.md); `404`: [`GenericErrorResponse`](GenericErrorResponse.md); `409`: [`ErrorResponse`](ErrorResponse.md); `500`: [`GenericErrorResponse`](GenericErrorResponse.md); \}
## Properties
-| Property | Type |
-| ------ | ------ |
-| <a id="property-401"></a> `401` | [`GenericErrorResponse`](GenericErrorResponse.md) |
-| <a id="property-404"></a> `404` | [`GenericErrorResponse`](GenericErrorResponse.md) |
-| <a id="property-500"></a> `500` | [`GenericErrorResponse`](GenericErrorResponse.md) |
+| Property | Type | Description |
+| ------ | ------ | ------ |
+| <a id="property-401"></a> `401` | [`GenericErrorResponse`](GenericErrorResponse.md) | - |
+| <a id="property-404"></a> `404` | [`GenericErrorResponse`](GenericErrorResponse.md) | - |
+| <a id="property-409"></a> `409` | [`ErrorResponse`](ErrorResponse.md) | DOCX upload is still processing; retry the export. |
+| <a id="property-500"></a> `500` | [`GenericErrorResponse`](GenericErrorResponse.md) | - |
diff --git a/packages/sdk/AGENTS.md b/packages/sdk/AGENTS.md
index f12807e50f..04962c0d85 100644
--- a/packages/sdk/AGENTS.md
+++ b/packages/sdk/AGENTS.md
@@ -36,6 +36,8 @@ excluded from source linting.
- Regenerate Rust OpenAPI and web service clients before `bun run sync-specs &&
bun run generate`; do not copy deployed schemas over a local API change.
+- Regenerate `../sdk-docs/reference` after SDK source or generated type changes;
+ run that package's checks because web CI verifies the reference is current.
- `generated/agent-fold/` is not from OpenAPI: `cargo run -p agent_fold --bin
export_types` writes it, and `foldedLog()` casts the opaque wire schema to it.
- Workspace advanced filters use the server's serialized AST names, not Rust
diff --git a/packages/sdk/generated/storage/types.gen.ts b/packages/sdk/generated/storage/types.gen.ts
index 5454a9b52c..a6af3d0556 100644
--- a/packages/sdk/generated/storage/types.gen.ts
+++ b/packages/sdk/generated/storage/types.gen.ts
@@ -19519,6 +19519,10 @@ export type ExportDocumentData = {
export type ExportDocumentErrors = {
401: GenericErrorResponse;
404: GenericErrorResponse;
+ /**
+ * DOCX upload is still processing; retry the export.
+ */
+ 409: ErrorResponse;
500: GenericErrorResponse;
};
diff --git a/packages/sdk/specs/storage.json b/packages/sdk/specs/storage.json
index 0f95aa2822..44904715ea 100644
--- a/packages/sdk/specs/storage.json
+++ b/packages/sdk/specs/storage.json
@@ -10322,6 +10322,16 @@
}
}
},
+ "409": {
+ "description": "DOCX upload is still processing; retry the export.",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/ErrorResponse"
+ }
+ }
+ }
+ },
"500": {
"description": "",
"content": {
diff --git a/services/document_storage_service/AGENTS.md b/services/document_storage_service/AGENTS.md
index fa7c698c7b..7972bbfe92 100644
--- a/services/document_storage_service/AGENTS.md
+++ b/services/document_storage_service/AGENTS.md
@@ -22,6 +22,10 @@ threads, pins, recents, notifications, folders, activity.
`call` verifies the RTC session and `messages` owns the shared discussion.
- `src/service/entity_mutation.rs`: generic entity-mutation plumbing used
across item types.
+- `src/outbound/docx_export.rs`: primary-database parts and immutable DOCX export
+ objects keyed by part paths/hashes. The export route maps the documents domain's
+ pending result to 409 with `Retry-After`; never reuse document-id-only archives.
+ Location and export URLs share signing helpers in `src/service/cloudfront.rs`.
- `src/service/delete_document_worker.rs`: background worker handling document
deletion side effects (feature-gated: `delete_document_worker`).
- `src/service/conn_gateway.rs`: pushes realtime updates through
diff --git a/services/document_storage_service/Cargo.toml b/services/document_storage_service/Cargo.toml
index 5cac998fe7..c0eaef0454 100644
--- a/services/document_storage_service/Cargo.toml
+++ b/services/document_storage_service/Cargo.toml
@@ -232,6 +232,7 @@ reminders = { path = "../../crates/reminders", features = [
] }
reqwest = { workspace = true }
rootcause = { workspace = true }
+sha2 = { workspace = true }
s3_key = { path = "../../crates/s3_key" }
saved_views = { path = "../../crates/saved_views" }
scheduled_action = { path = "../scheduled_action" }
diff --git a/services/document_storage_service/src/api/documents/export_document.rs b/services/document_storage_service/src/api/documents/export_document.rs
index c08c453583..41668c4603 100644
--- a/services/document_storage_service/src/api/documents/export_document.rs
+++ b/services/document_storage_service/src/api/documents/export_document.rs
@@ -1,7 +1,4 @@
-use std::{
- io::{Cursor, Write},
- str::FromStr,
-};
+use std::str::FromStr;
use crate::api::context::ApiContext;
use crate::api::context::{AuthorizationService, EntityAccessService};
@@ -11,23 +8,20 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
+use documents_hex::domain::docx_export::{DocxExport, export_docx};
use entity_access::inbound::axum_extractors::DocumentAccessExtractor;
-use futures::StreamExt;
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use model::{
document::{DocumentBasic, FileType, response::LocationResponseData},
response::{ErrorResponse, GenericErrorResponse},
};
use model_owner::Owner;
-use s3_key::build_temp_docx_key;
use models_permissions::share_permission::access_level::ViewAccessLevel;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
-use super::location::{
- get_cloudfront_signed_options, get_presigned_url, get_presigned_url_by_type,
-};
+use super::location::get_presigned_url_by_type;
#[allow(unused, reason = "this could probably be removed wait for tests")]
// The document_id field here is not used within the function handler body. Instead we use
@@ -59,12 +53,13 @@ pub struct ExportDocumentResponse {
(status = 304, body=GenericErrorResponse),
(status = 401, body=GenericErrorResponse),
(status = 404, body=GenericErrorResponse),
+ (status = 409, description = "DOCX upload is still processing; retry the export.", body=ErrorResponse),
(status = 500, body=GenericErrorResponse),
)
)]
-#[tracing::instrument(skip(state, user, _access), fields(user_id=?user.authorization.user.macro_user_id))]
+#[tracing::instrument(skip(state, user, access), fields(user_id=?user.authorization.user.macro_user_id))]
pub async fn handler(
- _access: DocumentAccessExtractor<ViewAccessLevel, EntityAccessService, AuthorizationService>,
+ access: DocumentAccessExtractor<ViewAccessLevel, EntityAccessService, AuthorizationService>,
Path(Params { .. }): Path<Params>,
State(state): State<ApiContext>,
user: MacroAuthorizationExtractor<AuthorizationService, UserOrInternal>,
@@ -84,7 +79,20 @@ pub async fn handler(
})?;
let presigned_url = match file_type {
- FileType::Docx => export_docx_document(&state, &document_context.document_id).await,
+ FileType::Docx => match export_docx(&state, access.entity_access_receipt).await {
+ Ok(DocxExport::Ready(url)) => Ok(url),
+ Ok(DocxExport::Processing) => {
+ return Ok((
+ StatusCode::CONFLICT,
+ [(axum::http::header::RETRY_AFTER, "1")],
+ Json(ErrorResponse {
+ message: "Document is still processing. Please retry shortly.".into(),
+ }),
+ )
+ .into_response());
+ }
+ Err(error) => Err(anyhow::Error::new(error)),
+ },
_ => {
export_basic_document(
&state,
@@ -147,81 +155,3 @@ async fn export_basic_document(
}
}
}
-
-async fn export_docx_document(state: &ApiContext, document_id: &str) -> anyhow::Result<String> {
- let docx_key = build_temp_docx_key(document_id);
- let exists = state.s3_client.exists(&docx_key).await?;
-
- if !exists {
- tracing::trace!("document does not exist in s3, downloading bom parts");
- let latest_document_bom_parts =
- macro_db_client::document::get_document_bom(state.db.clone(), document_id).await?;
-
- let shared_s3_client = &state.s3_client;
-
- #[expect(clippy::type_complexity, reason = "too annoying to fix now")]
- let downloaded_bom_parts: Vec<Result<(String, Vec<u8>), (String, anyhow::Error)>> =
- futures::stream::iter(latest_document_bom_parts.iter())
- .then(|bp| async move {
- let content = shared_s3_client
- .get_document(bp.sha.as_str())
- .await
- .map_err(|e| (bp.sha.clone(), e))?;
-
- Ok((bp.path.clone(), content))
- })
- .collect()
- .await;
-
- if downloaded_bom_parts.iter().any(|r| r.is_err()) {
- anyhow::bail!("unable to download bom parts");
- }
-
- let downloaded_bom_parts: Vec<(String, Vec<u8>)> = downloaded_bom_parts
- .into_iter()
- .filter_map(|r| r.ok())
- .collect();
-
- let writer = Cursor::new(Vec::new());
- let mut zip = zip::ZipWriter::new(writer);
-
- for part in downloaded_bom_parts {
- zip.start_file::<_, ()>(part.0, zip::write::FileOptions::default())?;
- zip.write_all(&part.1)?;
- }
-
- let writer = zip.finish()?;
- let data = writer.into_inner();
-
- state.s3_client.upload_document(&docx_key, data).await?;
- }
-
- sign_docx_url(state, &docx_key).await
-}
-
-async fn sign_docx_url(state: &ApiContext, key: &str) -> anyhow::Result<String> {
- let encoded_key = urlencoding::encode(key);
-
- let signed_options = get_cloudfront_signed_options(
- &state
- .config
- .document_storage_service_cloudfront_signer_public_key_id,
- state
- .config
- .document_storage_service_cloudfront_signer_private_key
- .as_ref(),
- state
- .config
- .document_storage_service_presigned_url_expiry_seconds,
- );
-
- let signed_url = get_presigned_url(
- &state
- .config
- .document_storage_service_cloudfront_distribution_url,
- &encoded_key,
- &signed_options,
- )?;
-
- Ok(signed_url)
-}
diff --git a/services/document_storage_service/src/api/documents/location.rs b/services/document_storage_service/src/api/documents/location.rs
index 47a6a3619a..a597ef3abf 100644
--- a/services/document_storage_service/src/api/documents/location.rs
+++ b/services/document_storage_service/src/api/documents/location.rs
@@ -4,12 +4,10 @@ use entity_access::inbound::axum_extractors::DocumentAccessExtractor;
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use models_permissions::share_permission::access_level::ViewAccessLevel;
use rayon::prelude::*;
-use std::{
- str::FromStr,
- time::{SystemTime, UNIX_EPOCH},
-};
+use std::str::FromStr;
use crate::model::request::documents::location::LocationQueryParams;
+use crate::service::cloudfront::{get_cloudfront_signed_options, get_presigned_url};
use axum::{
Extension,
body::Body,
@@ -17,7 +15,6 @@ use axum::{
http::{Response, StatusCode},
response::IntoResponse,
};
-use cloudfront_sign::{SignedOptions, get_signed_url};
use model::{
document::{DocumentBasic, FileType, FileTypeExt, response::LocationResponseData},
response::{GenericErrorResponse, GenericResponse, PresignedUrl},
@@ -142,7 +139,7 @@ async fn sign_document_key(
state
.config
.document_storage_service_presigned_url_expiry_seconds,
- );
+ )?;
let signed_url = get_presigned_url(
&state
@@ -233,7 +230,7 @@ async fn get_docx_urls(
state
.config
.document_storage_service_presigned_url_expiry_seconds,
- );
+ )?;
let cloudfront_distribution_url = state
.config
@@ -265,27 +262,6 @@ async fn get_docx_urls(
Ok(LocationResponseData::PresignedUrls(presigned_urls))
}
-/// Creates signed options for the cloudfront presigned url
-#[tracing::instrument(skip(private_key))]
-pub(in crate::api::documents) fn get_cloudfront_signed_options(
- public_key_id: &str,
- private_key: &str,
- presigned_url_expiry_seconds: u64,
-) -> SignedOptions {
- let current_unix_timestamp = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .unwrap()
- .as_secs();
- let date_less_than = current_unix_timestamp + presigned_url_expiry_seconds;
-
- SignedOptions {
- key_pair_id: public_key_id.to_string(),
- date_less_than,
- private_key: private_key.to_string(),
- ..Default::default()
- }
-}
-
/// Helper function to get the appropriate presigned URL based on file type (static vs editable)
#[tracing::instrument(skip(state))]
pub(in crate::api::documents) async fn get_presigned_url_by_type(
@@ -312,21 +288,3 @@ pub(in crate::api::documents) async fn get_presigned_url_by_type(
}
}
}
-
-/// Makes a cloudfront presigned url for the provided key
-#[tracing::instrument(skip(options), err)]
-pub(in crate::api::documents) fn get_presigned_url(
- cloudfront_distribution_url: &str,
- key: &str,
- options: &SignedOptions,
-) -> anyhow::Result<String> {
- let constructed_url = format!("{}/{}", cloudfront_distribution_url, key);
-
- let signed_url = if !macro_aws_config::is_local_aws() {
- get_signed_url(&constructed_url, options)?
- } else {
- constructed_url
- };
-
- Ok(signed_url)
-}
diff --git a/services/document_storage_service/src/outbound/docx_export.rs b/services/document_storage_service/src/outbound/docx_export.rs
new file mode 100644
index 0000000000..98f86baa4c
--- /dev/null
+++ b/services/document_storage_service/src/outbound/docx_export.rs
@@ -0,0 +1,93 @@
+//! DOCX export persistence using the primary database and immutable S3 objects.
+
+use std::io::{Cursor, Write};
+
+use documents_hex::domain::docx_export::DocxExportStore;
+use model::document::BomPart;
+use s3_key::document_key_url_path;
+use sha2::{Digest, Sha256};
+
+use crate::api::context::ApiContext;
+use crate::service::cloudfront::{get_cloudfront_signed_options, get_presigned_url};
+use crate::service::s3::S3;
+
+#[derive(Debug, thiserror::Error)]
+pub enum DocxExportError {
+ #[error("unable to read or export document parts")]
+ Storage(#[from] anyhow::Error),
+}
+
+/// The version's paths and content hashes identify both the stored archive and
+/// the CDN URL. Old document-id-only exports may contain an empty ZIP.
+fn export_key(document_id: &str, parts: &[BomPart]) -> String {
+ let mut hash = Sha256::new();
+ for part in parts {
+ hash.update((part.path.len() as u64).to_be_bytes());
+ hash.update(part.path.as_bytes());
+ hash.update((part.sha.len() as u64).to_be_bytes());
+ hash.update(part.sha.as_bytes());
+ }
+ // Keep the two-segment temporary-file shape so storage event consumers
+ // classify this as an export rather than a new document upload.
+ format!("temp_files/{document_id}-{:x}.docx", hash.finalize())
+}
+
+#[tracing::instrument(skip_all, fields(document_id = %document_id, part_count = parts.len(), cache_hit), err)]
+async fn store_export(
+ s3: &S3,
+ document_id: &str,
+ mut parts: Vec<BomPart>,
+) -> anyhow::Result<String> {
+ parts.sort_by(|a, b| a.path.cmp(&b.path));
+ let key = export_key(document_id, &parts);
+ let exists = s3.exists(&key).await?;
+ tracing::Span::current().record("cache_hit", exists);
+ if !exists {
+ let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ for part in parts {
+ let content = s3.get_document(&part.sha).await?;
+ zip.start_file::<_, ()>(part.path, zip::write::FileOptions::default())?;
+ zip.write_all(&content)?;
+ }
+ s3.upload_document(&key, zip.finish()?.into_inner()).await?;
+ }
+ Ok(key)
+}
+
+impl DocxExportStore for ApiContext {
+ type Err = DocxExportError;
+
+ #[tracing::instrument(skip_all, fields(document_id = %document_id), err)]
+ async fn parts(&self, document_id: &str) -> Result<Vec<BomPart>, Self::Err> {
+ Ok(macro_db_client::document::get_document_bom(self.db.clone(), document_id).await?)
+ }
+
+ #[tracing::instrument(skip_all, fields(document_id = %document_id), err)]
+ async fn export_parts(
+ &self,
+ document_id: &str,
+ parts: Vec<BomPart>,
+ ) -> Result<String, Self::Err> {
+ let key = store_export(&self.s3_client, document_id, parts).await?;
+ let signed_options = get_cloudfront_signed_options(
+ &self
+ .config
+ .document_storage_service_cloudfront_signer_public_key_id,
+ self.config
+ .document_storage_service_cloudfront_signer_private_key
+ .as_ref(),
+ self.config
+ .document_storage_service_presigned_url_expiry_seconds,
+ )?;
+ Ok(get_presigned_url(
+ &self
+ .config
+ .document_storage_service_cloudfront_distribution_url,
+ &document_key_url_path(&key),
+ &signed_options,
+ )?)
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/services/document_storage_service/src/outbound/docx_export/test.rs b/services/document_storage_service/src/outbound/docx_export/test.rs
new file mode 100644
index 0000000000..437e0117a4
--- /dev/null
+++ b/services/document_storage_service/src/outbound/docx_export/test.rs
@@ -0,0 +1,115 @@
+use super::*;
+use std::io::Read;
+
+#[test]
+fn cache_identity_bypasses_legacy_exports_and_tracks_part_content_and_paths() {
+ let mut parts = vec![BomPart {
+ id: "part-id".into(),
+ sha: "first-sha".into(),
+ path: "word/document.xml".into(),
+ }];
+ let original = export_key("document-id", &parts);
+ assert_ne!(original, s3_key::build_temp_docx_key("document-id"));
+ assert!(original.starts_with("temp_files/document-id-"));
+ assert!(original.ends_with(".docx"));
+ assert!(
+ s3_key::DocumentKey::from_s3_key(&original)
+ .unwrap()
+ .is_temp()
+ );
+
+ parts[0].sha = "second-sha".into();
+ assert_ne!(original, export_key("document-id", &parts));
+ parts[0].sha = "first-sha".into();
+ parts[0].path = "word/styles.xml".into();
+ assert_ne!(original, export_key("document-id", &parts));
+}
+
+#[tokio::test]
+async fn exports_complete_parts_without_reusing_a_legacy_empty_archive() {
+ let mut s3 = S3::default();
+ s3.expect_exists().times(1).returning(|key| {
+ // The old export exists, but must never be used or signed again.
+ Ok(key == s3_key::build_temp_docx_key("document-id"))
+ });
+ s3.expect_get_document()
+ .withf(|sha| sha == "content-sha")
+ .times(1)
+ .returning(|_| Ok(b"<document>original content</document>".to_vec()));
+ s3.expect_upload_document()
+ .times(1)
+ .returning(|key, bytes| {
+ assert_ne!(key, s3_key::build_temp_docx_key("document-id"));
+ let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();
+ assert_eq!(archive.len(), 1);
+ let mut xml = String::new();
+ archive
+ .by_name("word/document.xml")
+ .unwrap()
+ .read_to_string(&mut xml)
+ .unwrap();
+ assert_eq!(xml, "<document>original content</document>");
+ Ok(())
+ });
+ store_export(
+ &s3,
+ "document-id",
+ vec![BomPart {
+ id: "part-id".into(),
+ path: "word/document.xml".into(),
+ sha: "content-sha".into(),
+ }],
+ )
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn complete_cached_export_is_reused_regardless_of_database_part_order() {
+ let mut s3 = S3::default();
+ s3.expect_exists().times(2).returning(|_| Ok(true));
+ s3.expect_get_document().never();
+ s3.expect_upload_document().never();
+ let mut parts = vec![
+ BomPart {
+ id: "document-part".into(),
+ path: "word/document.xml".into(),
+ sha: "document-sha".into(),
+ },
+ BomPart {
+ id: "styles-part".into(),
+ path: "word/styles.xml".into(),
+ sha: "styles-sha".into(),
+ },
+ ];
+ let first = store_export(&s3, "document-id", parts.clone())
+ .await
+ .unwrap();
+ parts.reverse();
+ assert_eq!(
+ first,
+ store_export(&s3, "document-id", parts).await.unwrap()
+ );
+}
+
+#[tokio::test]
+async fn failed_part_download_does_not_publish_a_partial_archive() {
+ let mut s3 = S3::default();
+ s3.expect_exists().returning(|_| Ok(false));
+ s3.expect_get_document()
+ .returning(|_| Err(anyhow::anyhow!("object storage unavailable")));
+ s3.expect_upload_document().never();
+ assert!(
+ store_export(
+ &s3,
+ "document-id",
+ vec![BomPart {
+ id: "part-id".into(),
+ path: "word/document.xml".into(),
+ sha: "content-sha".into(),
+ }],
+ )
+ .await
+ .is_err()
+ );
+}
diff --git a/services/document_storage_service/src/outbound/mod.rs b/services/document_storage_service/src/outbound/mod.rs
index 62b5cc6947..29e28a65f4 100644
--- a/services/document_storage_service/src/outbound/mod.rs
+++ b/services/document_storage_service/src/outbound/mod.rs
@@ -2,4 +2,5 @@
pub mod access_requests;
pub mod agent_dm_authorizer;
+pub mod docx_export;
pub mod entity_mutation;
diff --git a/services/document_storage_service/src/service/cloudfront.rs b/services/document_storage_service/src/service/cloudfront.rs
new file mode 100644
index 0000000000..32092bd0d9
--- /dev/null
+++ b/services/document_storage_service/src/service/cloudfront.rs
@@ -0,0 +1,40 @@
+//! CloudFront signing shared by document locations and exports.
+
+use cloudfront_sign::{SignedOptions, get_signed_url};
+use std::time::{SystemTime, UNIX_EPOCH};
+
+/// Creates signed options for the cloudfront presigned url
+#[tracing::instrument(skip(private_key), err)]
+pub(crate) fn get_cloudfront_signed_options(
+ public_key_id: &str,
+ private_key: &str,
+ presigned_url_expiry_seconds: u64,
+) -> anyhow::Result<SignedOptions> {
+ let current_unix_timestamp = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
+ let date_less_than = current_unix_timestamp + presigned_url_expiry_seconds;
+
+ Ok(SignedOptions {
+ key_pair_id: public_key_id.to_string(),
+ date_less_than,
+ private_key: private_key.to_string(),
+ ..Default::default()
+ })
+}
+
+/// Makes a cloudfront presigned url for the provided key
+#[tracing::instrument(skip(options), err)]
+pub(crate) fn get_presigned_url(
+ cloudfront_distribution_url: &str,
+ key: &str,
+ options: &SignedOptions,
+) -> anyhow::Result<String> {
+ let constructed_url = format!("{}/{}", cloudfront_distribution_url, key);
+
+ let signed_url = if !macro_aws_config::is_local_aws() {
+ get_signed_url(&constructed_url, options)?
+ } else {
+ constructed_url
+ };
+
+ Ok(signed_url)
+}
diff --git a/services/document_storage_service/src/service/mod.rs b/services/document_storage_service/src/service/mod.rs
index fa41cf3b94..e579cd9cb5 100644
--- a/services/document_storage_service/src/service/mod.rs
+++ b/services/document_storage_service/src/service/mod.rs
@@ -1,4 +1,5 @@
pub mod activity;
+pub(crate) mod cloudfront;
pub mod conn_gateway;
#[cfg(feature = "delete_document_worker")]
pub mod delete_document_worker;