Asset storage backends
Asset storage keeps the binary files that support saved content separate from the database records that describe them. The system gives server routes and workers one asset-store contract, then selects either local filesystem storage or S3-compatible storage behind that contract. This page is a child of Search, inference, and plugin architecture and relates to Data model and persistence and Content ingestion and enrichment jobs.
Asset persistence model
An asset has two parts: a database record for identity, ownership, type, size, and bookmark links, plus a binary object in the selected store. The assets table in packages/…/schema.ts is the system record; its userId and assetId values also define the ownership boundary and storage address.
The database record can point to a bookmark or remain unattached. This lets an uploaded file enter the system before a user uses it as a bookmark asset. A bookmark asset record in bookmarkAssets supplies the bookmark-facing type and extracted content while the assets row and store retain the original file.
| Record or object | System role |
|---|---|
| Asset database row | Tracks ownership, use, content type, file name, size, and optional bookmark link. |
| Stored asset object | Holds the binary file and its content metadata. |
| Bookmark asset relation | Connects an image or PDF asset to a bookmark and holds derived text or PDF metadata. |
Asset types
The shared asset rules define which content types the store accepts and which types users may upload or attach as bookmark assets. Images and PDFs are the main bookmark file types. HTML, video, ZIP, and other accepted types support crawl output, uploads, backups, or later processing.
References: packages/…/assetdb.ts• L3-50 packages/…/schema.ts• L338-376 packages/…/schema.ts• L447-459
Asset store contract
The AssetStore contract in packages/…/assetdb.ts describes the common actions that all providers must support. It accepts a user ID and asset ID for every access, carries content type and optional file name metadata, and supports both whole-file and stream reads.
The contract also supports byte ranges. The HTTP layer uses this behavior for partial responses such as video playback or large-file reads. Bulk deletion and enumeration serve account deletion and maintenance work without exposing provider-specific storage rules to callers.
| Contract group | Capability |
|---|---|
| Write | Save a buffer or move a temporary file into storage. |
| Read | Read a complete asset or a byte range with metadata. |
| Stream | Return a complete or ranged readable stream. |
| Inspect | Read metadata or report the stored byte size. |
| Remove | Delete one asset or all assets for a user. |
| Audit | Enumerate stored assets with owner, ID, metadata, and size. |
Provider selection
The shared server facade loads asset-store plugins and asks the plugin manager for the active client. The last registered provider for the asset-store plugin type wins. Callers therefore use saveAsset, readAsset, createAssetReadStream, and related facade functions without knowing whether data is local or remote.
The configuration selects S3 when an S3 endpoint is present. Otherwise the filesystem provider is used. S3 mode requires a bucket and access credentials before its provider can register.
References: packages/…/assetdb.ts• L67-108 packages/…/assetdb.ts• L22-160 packages/…/plugins.ts• L10-23 packages/…/plugins.ts• L65-93 packages/…/config.ts• L457-501
Storage providers
Both providers preserve the same asset identity and metadata rules. Their main difference is where the object lives and which storage service performs reads, writes, range reads, and deletes.
Filesystem provider
The filesystem provider stores each asset below the configured assets directory. It groups data by user and asset ID, keeps the binary content with its metadata, and uses local file reads or read streams for retrieval. The configured directory must remain available to every process that reads or writes assets.
The provider can copy a temporary file into the asset store. This path lets uploads and crawlers avoid keeping large files in memory. It can also remove all objects for one user and scan stored objects for maintenance checks.
References: packages/…/index.ts• L1-24 packages/…/index.ts• L11-16 packages/…/index.ts• L25-79 packages/…/index.ts• L81-205
S3 provider
The S3 provider stores each asset as an object under a key made from the user ID and asset ID. It uses object metadata for the content type and file name, which keeps the provider independent of the application database for binary delivery.
The provider maps whole reads and byte ranges to object reads. It uses object headers for metadata and size. User deletion lists and removes objects under that user’s prefix, while maintenance enumeration walks the bucket and reads object headers for each asset.
References: packages/…/index.ts• L15-67 packages/…/index.ts• L17-49 packages/…/index.ts• L51-193 packages/…/index.ts• L196-275
Server asset flows
The server accepts user uploads, records their metadata, and serves stored bytes only after an access check. The same delivery path supports authenticated requests and short-lived signed links for public or external clients.
Upload flow
An upload first passes content detection, type checks, size limits, and the user storage quota check. The server writes the incoming stream to a temporary file, creates an unattached database row, and transfers the file to the active asset store. The returned asset ID is then available for attachment to a bookmark.
This flow is shown below. The database row and stored object are two parts of one logical asset, so a failure during either step can leave work for cleanup or repair.
flowchart LR
Client[Client upload] --> Validate[Detect type and check limits]
Validate --> Quota[Check user quota]
Quota --> Temp[Write temporary file]
Temp --> Row[Create asset database row]
Row --> Store[Save through AssetStore]
Store --> Result[Return asset ID and metadata]
References: packages/…/assets.ts• L15-44 packages/…/upload.ts• L43-144 packages/…/assetdb.ts• L59-78
Download flow
An authenticated download resolves the asset record and checks whether the requester owns the asset or can view its attached bookmark. A signed download uses a token that binds the asset ID to its owner and has an expiry time. After the check, both paths read metadata and size from the store and stream the bytes to the client.
The response supports byte ranges and sets the content type and length from stored metadata and size. This keeps large files out of the route’s memory and lets clients resume or seek within supported assets.
sequenceDiagram
participant Client
participant API
participant DB as Asset database
participant Store as AssetStore
Client->>API: Request asset or signed URL
API->>DB: Check asset identity and access
DB-->>API: Owner and bookmark relation
API->>Store: Read metadata and size
Store-->>API: Metadata and byte count
API->>Store: Create full or ranged stream
Store-->>Client: Asset bytes
References: packages/…/assets.ts• L46-72 packages/…/assets.ts• L14-48 packages/…/assets.ts• L28-49 packages/…/assets.ts• L225-280 packages/…/assets.ts• L12-75
Attachment lifecycle
Asset attachment changes the database relationship rather than copying the binary object. Attach, replace, and detach operations check ownership and allowed asset types. When an asset is replaced or detached, the old database row and stored object are removed through separate database and store actions.
References: packages/…/assets.ts• L12-83 packages/…/assets.ts• L84-202
Worker asset flows
Workers capture files while they crawl pages and later read those files for enrichment. They use the same shared asset-store facade as the server, so a worker can persist and retrieve assets without knowing the selected provider.
Crawl capture flow
The crawler stores screenshots, PDFs, downloaded images or videos, page archives, and large HTML content. It creates an asset ID, checks the user quota, and saves either a buffer or a temporary file. Small HTML content stays with the bookmark, while large HTML content becomes a stored asset.
The capture path applies size and content checks before storage. Data URIs are decoded locally and limited to supported raster image types before they become banner assets. A failed quota check skips the optional capture instead of writing an over-limit object.
flowchart TD
Capture[Crawler output] --> Kind{Output form}
Kind -->|Buffer| Check[Check type, size, and quota]
Kind -->|Downloaded or archived file| Temp[Create bounded temporary file]
Temp --> Check
Kind -->|Small HTML| Inline[Keep content with bookmark]
Check --> Save[Save through AssetStore]
Save --> Link[Return asset identity to crawl result]
References: apps/…/assetStorage.ts• L37-145 apps/…/assetStorage.ts• L149-223 apps/…/assetStorage.ts• L225-344 apps/…/assetStorage.ts• L369-480 apps/…/assetStorage.ts• L484-563
Asset preprocessing flow
The asset preprocessing worker reads the stored file through the shared store and turns image or PDF bytes into bookmark content. Image assets use OCR, while PDF assets produce extracted text and may produce a first-page screenshot that is saved as a new asset and database row.
After preprocessing, the worker starts the next enrichment and search steps when the result changed or when the job is not in repair mode. A missing object, unsupported bookmark asset, or permanent processing error stops that path and clears pending enrichment states as needed.
flowchart LR
Job[Preprocessing job] --> DB[Load bookmark relations]
DB --> Read[Read asset from AssetStore]
Read --> Type{Image or PDF}
Type -->|Image| OCR[Extract image text]
Type -->|PDF| PDF[Extract text and page image]
PDF --> New[Save derived screenshot asset]
OCR --> Enrich[Queue enrichment and reindex]
New --> Enrich
References: apps/…/assetPreprocessingWorker.ts• L189-277 apps/…/assetPreprocessingWorker.ts• L280-373 apps/…/assetPreprocessingWorker.ts• L376-506
Consistency and access boundaries
The asset ID is shared by the database row, storage object, download URL, and worker messages. The user ID travels with each store operation, which prevents a provider call from addressing another user’s object through an asset ID alone.
Storage quota checks happen before server uploads and worker captures. The shared save facade also verifies that the quota approval belongs to the same user. Database ownership checks remain separate from provider access checks because the provider stores bytes but does not decide whether a caller may view a bookmark.
Deletion is also split across the two layers. Database cascades remove asset records when their owning user or bookmark is removed, while application flows call the store delete operation to remove the binary object. Best-effort cleanup is used for some replacement and detach paths, so maintenance can find stored objects that no longer have database rows.
References: packages/…/assetdb.ts• L35-78 packages/…/schema.ts• L363-369 packages/…/assets.ts• L204-250 packages/…/assets.ts• L150-165 packages/…/assets.ts• L188-202
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
AssetStorecontract aligned across the shared facade, filesystem provider, S3 provider, and interface-compliance checks. Range reads and temporary-file ownership are easy to change in one provider only. - Keep the database asset fields and stored metadata aligned. Uploads and worker-created assets must expose the content type and size expected by download and preprocessing flows.
- Keep user and asset IDs consistent across database rows, provider keys or paths, signed-token payloads, and worker requests.
- When changing deletion or attachment behavior, check both the database row and the stored object. A database cascade does not by itself delete provider data.
- When changing accepted content types or size limits, review upload validation, crawler capture, provider validation, and downstream image or PDF processing together.
- When changing S3 settings, verify bucket, endpoint, credentials, and path-style behavior as one provider configuration.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The asset store plugin is not registered | Server or worker storage calls fail before reading or writing bytes. | Load the configured provider and fail startup for missing required S3 settings. |
| A database row exists but its provider object is missing | Asset listing can succeed but download or preprocessing cannot read the file. | Keep row and object creation linked, and use asset maintenance enumeration to find drift. |
| A provider receives unsupported metadata | The save is rejected and the caller cannot persist the asset. | Apply shared content-type rules before upload or capture and in both providers. |
| A user exceeds storage quota | An upload or optional crawler capture is rejected or skipped. | Check quota before writing and validate the approval against the user and size. |
| A request has no valid owner or signed token | The asset is not served. | Check database ownership or bookmark access, then validate signed-token asset and user IDs. |
| A range request reaches inconsistent size or stream behavior | Clients receive bad partial content or cannot seek within the asset. | Keep inclusive range handling consistent with getAssetSize and stream support in both providers. |
| Filesystem storage is not persistent or shared across processes | Assets disappear after restart or are unavailable to another service process. | Mount the configured assets directory as durable storage, or use the S3 provider. |
Key files
| File | Why to read it |
|---|---|
| packages/…/assetdb.ts | Defines the asset types, metadata shape, and provider contract. |
| packages/…/assetdb.ts | Shows the provider-neutral server and worker facade plus quota approval checks. |
| packages/…/schema.ts | Defines asset ownership, bookmark links, and the bookmark-facing asset relation. |
| packages/…/plugins.ts | Explains how the active asset-store provider is selected. |
| packages/…/index.ts | Shows the local persistence model and range behavior. |
| packages/…/index.ts | Shows the S3 object model, metadata mapping, and bulk operations. |
| packages/…/upload.ts | Shows the server upload boundary from validation through database and store persistence. |
| packages/…/assets.ts | Shows metadata lookup and full or ranged HTTP streaming. |
| packages/…/assets.ts | Shows ownership checks, signed links, and attachment lifecycle rules. |
| apps/…/assetStorage.ts | Shows how crawl outputs become quota-checked stored assets. |
| apps/…/assetPreprocessingWorker.ts | Shows how workers read assets and save derived PDF results before enrichment. |
| packages/…/interface-compliance.test.ts | Defines the behavior both providers must preserve, including range reads, bulk actions, and error handling. |