Files

File upload, storage, and management system. Files are stored on S3-compatible storage (Tigris) and linked to any entity via a polymorphic association table. Supports image renditions, bulk operations, and label-based organization.

Source Files

LayerPath
Schemabackend/src/db/schema.ts
Routesbackend/src/routes/files/ (includes bulk operations)
File Servicebackend/src/services/file-service.ts
Bulk Servicebackend/src/services/bulk-file-service.ts
Label Servicebackend/src/services/file-label-service.ts
Link Servicebackend/src/services/entity-file-link-service.ts
Storage Libbackend/src/lib/storage.ts
Componentsfrontend/src/components/files/ (25 files)
Queriesfrontend/src/lib/queries/fileQueries.ts

Database Tables

TablePurpose
filesMain entity — filename, MIME type, size, storage key, rendition metadata
entityFileLinksPolymorphic join — links a file to any entity type (building, project, document request, etc.)
fileUploadsMetadataUpload tracking (progress, chunked upload state)
fileLabelAssociationsTags/labels applied to files for organization and filtering

Storage Architecture

ConcernImplementation
Object storageS3-compatible via Tigris (TIGRIS_* env vars)
UploadPresigned URLs for direct browser-to-S3 upload
Renditionssharp library generates thumbnails and preview sizes
Background processingRendition generation via Background Jobs
ValidationMIME type checking and file size limits enforced server-side

The entityFileLinks table provides a polymorphic association pattern:

FieldPurpose
fileIdFK to files
entityTypeDiscriminator string (e.g., building, project, documentRequest)
entityIdUUID of the linked entity

This means any entity in the system can have files attached without schema changes. Queries filter by entityType + entityId.

Frontend Components

ComponentPurpose
FileUploadDrag-and-drop upload with progress indicator
FileAssociationManagerLink/unlink files to entities
FileGrid / FileListGallery and list views for browsing files
FilePreviewInline preview for images and PDFs
SyncChangelogDisplays file sync history
FileLabelFilterFilter files by assigned labels

Relationships

File 1──* EntityFileLinks ──* (any entity)
File 1──* FileLabelAssociations
File *──1 Users (uploader)

Files are linked to Buildings, Projects, Documents, Document Obtaining fulfillments, Quotes, Invoices, and other entities through entityFileLinks.

Documents | Document Obtaining | Buildings | Projects | Background Jobs | Database Architecture | Service Layer Pattern | Deployment Pipeline