Files
classroomio/prd/media-manager [DONE]/decoupling.md
T
Rotimi BestRotimi Bestgreptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>Cursor AgentAbdulmuizz HamzatAdeyemi Adetunji
1e9a82e912 feat: complete rewrite of course (#622)
* theming improvements

* border style improvements

* refactor: migrate org routes to server-side data fetching

Move client-side API calls to server-side load functions for better
performance, security, and SEO. Implement permission-based course filtering

* refactor: clean up course and community components

This commit removes unused components and files related to courses and community features, streamlining the codebase. It also updates various routes to enhance performance and maintainability. Additionally, it introduces new types and constants for better type safety and organization.

* refactor: fix video formatting on website and other styles

* refactor: update course and dashboard components for improved styling and functionality

* more icon improvements

* refactor: streamline course and dashboard components

This commit updates various course and dashboard components for improved styling and functionality. It includes the removal of unused files, updates to imports and exports, and enhancements to type safety. Additionally, it introduces new routes and optimizes existing ones for better performance and maintainability.

* refactor: enhance lesson completion logic

* chore: delete all emails on dashboard and move other api routes to backend.

move all emails to the backend and

* feat: Refactor API and database queries for courses, quizzes, and lessons, migrating Supabase functions and schema management.

* get app loading

* fix ui and build error of cio/api

* refactor: move direct database queries to query layer

- Create query functions: getCourseWithOrgData, getOrganizationByCourseId, updateCourseVersion, updateLessonsSectionId, getProfileByGroupMemberId, getQuestionTypesByIds
- Refactor services to use query functions instead of direct db queries
- Add transaction support to updateCourseVersion and updateLessonsSectionId
- Fix getProfileByGroupMemberId to return profile directly (not nested)
- Fix updateLessonsSectionId to use .returning() instead of rowCount

This aligns services with architecture pattern where services orchestrate queries rather than executing them directly.

* exercise saving improvements

* fix and optimise exercise creation

* refactor: update dialog components for improved styling and functionality

- Adjusted dialog content width and maximum constraints for better responsiveness.
- Changed heading font size for consistency in the new exercise modal.
- Replaced CloseIcon with CrossIcon for a more modern appearance in dialog close button.

* refactor: enhance course and exercise management components

- Introduced new components for exercise creation, editing, and viewing, improving user experience.
- Refactored existing lesson and exercise components for better organization and maintainability.
- Updated routing for exercises and lessons to streamline navigation within the course structure.
- Added new utility functions and constants to support content management across the application.

* feat: implement course content management API and UI components

- Added new API routes for updating and deleting course content, enhancing content management capabilities.
- Introduced new Svelte components for displaying and managing course content, including content lists and action handlers.
- Updated existing course routing to integrate new content management features.
- Refactored content-related utility functions for improved organization and maintainability.

* refactor: remove Supabase references and implement course section management

- Removed Supabase-related environment variables and Dockerfile arguments to streamline configuration.
- Introduced a new section management API with routes for creating, updating, deleting, and reordering course sections.
- Updated course services to handle section operations, ensuring consistency in course content management.
- Refactored existing lesson section logic to align with the new course section structure, enhancing maintainability.

* chore: update .prettierignore to exclude additional file types

- Added exclusions for SQL and environment files to the Prettier ignore list.
- Included Dockerfile and .prettierignore itself to prevent formatting issues.

* feat: enhance course content management with translation support and UI improvements

- Added translation keys for user-facing text in course-related components, ensuring all UI copy is sourced from translation files.
- Updated lesson and exercise creation components to utilize translation keys for success and error messages, improving localization.
- Refactored course content management components to streamline the creation and editing process, enhancing user experience.
- Introduced new constants for content options and stepper states to standardize component behavior and improve maintainability.
- Improved styling and functionality of course content modals and steppers for better user interaction.

* feat: enhance course content management and UI components

- Updated .prettierignore to exclude storybook-static directory.
- Improved exercise creation logic by refining option handling and adding UUID support for option values.
- Enhanced navigation component with debugging output for custom links.
- Introduced a new method in CourseApi to update content items locally, improving state management during navigation.
- Added caching for exercise templates by tag in ExerciseTemplateApi to optimize performance.
- Refactored lesson and exercise components for better organization and maintainability, including updates to modal styling and functionality.
- Implemented local patching for lesson and exercise list items to ensure UI consistency after updates.

* student + teacher invite (not fully tested)

* refactor: streamline course team member middleware

- Removed unnecessary console logging and timing for checking user permissions.
- Simplified the logic for determining if a user is a course team member or organization admin.

* reorder course sidebar to move content to its own page.

* feat: implement unified course enrollment route and API

- Introduced a new enrollment route `/course/:slug/enroll` to replace the previous invite system.
- Unified enrollment logic for free and paid courses, allowing direct enrollment for free courses and invite token validation for paid courses.
- Updated course landing page to redirect to the new enrollment route for free courses.
- Removed obsolete invite routes and related logic, streamlining the enrollment process.
- Enhanced API with a new `POST /course/:courseId/enroll` endpoint for handling enrollments.
- Added rate limiting for enrollment requests to prevent abuse.
- Updated UI components to reflect changes in enrollment flow and messaging.

* chore: update docker stack, docs, and auth token automation

* chore: make prettier check ignore unknown file types

* fix: avoid using authClient on server file and keep authClient client focused

* chore: always get the server on demand depending on the environment

* chore: update dashboard configuration and Docker setup for environment variables

- Modified the Svelte configuration to directly use the PUBLIC_SERVER_URL environment variable.
- Enhanced the Docker Compose file to include health check formatting and added environment variable arguments for the dashboard build.
- Updated the Dockerfile to echo environment variable values during the build process, ensuring clarity on the configuration being used.

* chore: enhance Docker setup and documentation for production deployment

- Added a new production Docker Compose file and example environment file for published images.
- Updated README and usage documentation to include instructions for deploying with production images.
- Modified Svelte configuration to standardize the use of the posthog domain.
- Removed unnecessary console logs from the API client and layout server files for cleaner output.
- Improved overall clarity in Docker documentation regarding self-hosting and image publishing.

* chore(db): log function name in query catch blocks (console.error 'funcName error:', error)

Co-authored-by: Cursor <[email protected]>

* fix: multiple refresh issue

* fix: server key not passed to api

* fix: clarify usage of PRIVATE_SERVER_KEY and AUTH_BEARER_TOKEN in documentation

- Updated documentation to specify that PRIVATE_SERVER_KEY is used for server-to-server API key authentication and is validated by API key middleware.
- Clarified that AUTH_BEARER_TOKEN is a separate token not utilized by apiKeyMiddleware.
- Adjusted examples in Docker setup and self-hosting documentation to reflect these changes.

* fix migration script

* fix: improve language handling and add database reset script

- Updated language extraction to handle potential undefined values, defaulting to 'en' if necessary.
- Added a new script for resetting the database, including functionality to truncate public tables and run setup with optional seeding.

* feat: add promote ungrouped section functionality and enhance document handling in dashboard

- Implemented a new API endpoint to promote synthetic "Ungrouped" content into a real persisted section, allowing for better organization of course materials.
- Updated the dashboard to include a new component for displaying content count badges for lessons and exercises.
- Enhanced document management features, including a new document card component with dropdown actions for viewing, downloading, and removing documents.
- Improved file upload handling with a dedicated file drop zone and error handling for file type and size restrictions.
- Refactored existing components for better readability and maintainability, including updates to lesson and document lists.

* chore: use root env file for docker compose

* docs: document docker no-build startup flow

* chore: add no-build option to docker full stack script

* fix(email): handle SMTP TLS mode by port

* feat(auth): add configurable cookie domain env and docs

* apps/dashboard/src/lib/utils/services/auth/session.ts

* fix(email): make sender configurable via SMTP_SENDER

* chore(i18n): regenerate locale translations and support language targeting

* chore(config): switch api to PUBLIC_SERVER_URL

* fix(i18n): persist selected locale across navigation

* feat(assets): implement organization asset management routes and services

- Added new routes for managing organization assets, including listing, creating, updating, deleting, and exporting assets.
- Introduced asset-related services for handling asset operations such as creation, retrieval, and deletion.
- Integrated YouTube metadata resolution and asset attachment/detachment functionalities.
- Updated organization routes to include the new asset management routes.
- Enhanced error handling for asset-related operations.

* fix(document-list): update conditional rendering for edit mode in document list

* debug self hosted layout setup

* refactor(sidebar): simplify lms sidebar footer and improve navigation item rendering

* feat: tags

* chore: tags roadmap + translations

* fix(api): add authMiddleware to exercise, invites, and content routers

- exerciseRouter: apply authMiddleware before courseMemberMiddleware on all 10 routes
- invitesRouter: apply authMiddleware before courseTeamMemberMiddleware on all 4 routes
- contentRouter: add authMiddleware to PUT route (DELETE already had it)

Both courseMemberMiddleware and courseTeamMemberMiddleware document requiring
authMiddleware to be applied first. This aligns with lessonRouter pattern and
ensures explicit middleware contract regardless of global session middleware.

Co-authored-by: Rotimi Best <[email protected]>

* fix(course): use courseTeamMemberMiddleware for PUT, DELETE, and analytics routes

These routes require admin/tutor role per JSDoc but were using
courseMemberMiddleware which allows any course member including students.
Switch to courseTeamMemberMiddleware to restrict access to ADMIN/TUTOR
group members and org admins.

Co-authored-by: Rotimi Best <[email protected]>

* chore: unify pricing component between dashboard and website (#630)

* feat: modularize pricing card

* feat: initia implementation of pricing togge

* fix: minor amendment

* refactor: update pricing components for improved layout and styling

- Adjusted spacing in the pricing card layout for better visual consistency.
- Modified button styles and sizes for enhanced usability.
- Cleaned up the pricing toggle component by removing unnecessary console logs and restructuring the button rendering logic.

* add handle click

* chore(css): update Tailwind CSS output with new utility classes and theme variables

* fix(api): enable orgMemberMiddleware for stats route

This change ensures that the stats route is protected by orgMemberMiddleware, aligning access control with other routes. Additionally, removed unnecessary console log from the layout server file to clean up the code.

---------

Co-authored-by: Rotimi Best <[email protected]>

* pass query parameter to signup from pricing page

* ui updates to pricing cards

* commit all PRDs

* add open api spec to api prd

* make blue default theme

* more prds

* sync option id sequence on question update

* docs: more classroomio features

* chore: more ui improvements

* docs: new component docs

* chore: CSP Configuration (#638)

* feat(validation): add GitHub repository name validation

- Add ZRepositoryName schema for validating GitHub repo names
- Add ZRepositoryOwner for owner/username validation
- Add ZRepositoryIdentifier for owner/repo format
- Follow GitHub rules: A-Za-z0-9_.-, 1-100 chars, reserved names
- Add translation keys for validation messages

Fixes #637

Co-authored-by: Rotimi Best <[email protected]>

* Revert "feat(validation): add GitHub repository name validation"

This reverts commit 3436536bb17a0f05b3a9fe8dd7682cde64dd1dce.

* feat(dashboard): make CSP external domains configurable via env

Move hardcoded external domains from svelte.config.js to .env configuration
for on-premise deployments with strict security policies, data residency
constraints, or CISO-driven allowlists.

- ALLOWED_EXTERNAL_DOMAINS: single list for all CSP directives
- CSP_SCRIPT_SRC_DOMAINS, CSP_STYLE_SRC_DOMAINS, CSP_CONNECT_SRC_DOMAINS,
  CSP_FRAME_SRC_DOMAINS, CSP_FONT_SRC_DOMAINS: per-directive override
- When unset, defaults retain current SaaS behavior (backward compatible)

Fixes #637

Co-authored-by: Rotimi Best <[email protected]>

* fix(dashboard): self-hosted CSP uses only env vars, no defaults

Co-authored-by: Rotimi Best <[email protected]>

* fix(dashboard): add all classroomio.com domains to CSP defaults

Co-authored-by: Rotimi Best <[email protected]>

* docs: add CSP ALLOWED_EXTERNAL_DOMAINS to self-hosting guides

Co-authored-by: Rotimi Best <[email protected]>

* feat(docker): pass CSP domain env vars as build args to dashboard

Co-authored-by: Rotimi Best <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Rotimi Best <[email protected]>

* feat: add SSO and token authentication routes and services

- Introduced new routes for SSO and token authentication under the organization namespace.
- Implemented SSO connection management including creation, update, activation, and deletion.
- Added token authentication configuration with support for secret rotation and status retrieval.
- Enhanced error handling for SSO and token authentication processes.
- Updated existing routes and services to integrate new functionalities.
- Improved gradebook service to fetch student marks and exercises.
- Refactored API structure for better organization and maintainability.

* update translations

* fix: add PUBLIC_SERVER_URL to .env.example to prevent localhost fallback in production (#641)

Co-authored-by: Cursor Agent <[email protected]>

* feat: new exercise question types

* MinIO S3 storage backend (#639)

* feat(storage): add MinIO/S3-compatible object storage backend

- Add OBJECT_STORAGE_* env vars for generic S3-compatible backends (MinIO, AWS S3)
- Create storage config module with Cloudflare R2 fallback for backward compatibility
- Refactor s3.ts to use configurable client and bucket names from config
- Update media.ts to use provider-neutral public URL (OBJECT_STORAGE_MEDIA_PUBLIC_BASE_URL or CLOUDFLARE_IMAGE_BUCKET_DOMAIN)
- Add MinIO service and init to docker-compose (profile: minio) with videos/documents/media buckets
- Add localhost:9000 to dashboard CSP for local MinIO presigned URL uploads
- Update self-hosting and Cloudflare docs with MinIO option
- Update .env examples and docker USAGE.md

Fixes #635

Co-authored-by: Rotimi Best <[email protected]>

* docs: add MINIO_ROOT_USER and MINIO_ROOT_PASSWORD to self-hosting guides

Co-authored-by: Rotimi Best <[email protected]>

* feat(media): refactor media management and integrate MinIO support

- Updated media API to use a new `mediaApi` module, replacing `mediaManagerApi` references across components.
- Added MinIO configuration options to the Docker setup, allowing for optional object storage exclusion.
- Enhanced environment variable management for MinIO in the `.env.example` file.
- Updated Content Security Policy (CSP) to allow local MinIO access in the dashboard.
- Introduced new media components for asset management, including asset cards and usage dialogs.

This refactor improves the modularity of the media handling system and provides better support for S3-compatible storage solutions.

* feat(dashboard): enhance logging and refactor auth session handling

- Added console logging for event and environment variables in hooks.server.ts to aid debugging.
- Refactored auth session handling to use authServerClient instead of authClient in session.ts for improved consistency.
- Updated Dockerfile to ensure the dashboard build output is present before starting the application.
- Expanded self-hosting documentation to include troubleshooting steps for disk space issues in PostgreSQL and Docker.

These changes improve the observability of the application and streamline the authentication process.

* refactor(dashboard): remove debug logging and enhance Postgres healthcheck

- Removed console logging for event and environment variables in hooks.server.ts to clean up the code.
- Updated Postgres healthcheck in docker-compose.yaml to require a successful `SELECT 1` query, ensuring dependent services only start once the database is ready.
- Expanded self-hosting documentation to include troubleshooting steps for database recovery issues.

These changes improve code clarity and reliability in service startup sequences.

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Rotimi Best <[email protected]>

* feat: add Plyr media player icons and PDF.js integration

- Introduced new Plyr SVG icons for media player controls in both course and dashboard applications.
- Updated Plyr player configuration to use local icon assets.
- Integrated PDF.js for document viewing, replacing external CDN links with local paths for improved performance and reliability.
- Enhanced PDF viewer modal z-index for better visibility.

This update improves the media player experience and ensures consistent access to necessary resources.

* feat: enhance account and organization services with type definitions and promise return types

- Updated `getAccountData` to return a structured result type including profile and organizations.
- Modified `getOrganizationsWithFilters` to specify a promise return type for better type safety.
- Added new type imports for organization-related data structures to improve clarity and maintainability.

These changes improve type safety and enhance the overall structure of the account and organization services.

* feat: implement Redis caching for presigned URLs and refactor API interactions

- Added Redis caching for presigned download URLs in the API to reduce redundant S3 calls, improving performance.
- Introduced a new `PresignApi` class in the frontend to centralize presigned URL requests, enhancing code maintainability.
- Updated various components to utilize the new `presignApi` methods for document and video upload/download operations.
- Enhanced error handling for Redis operations to ensure graceful degradation in case of cache failures.

These changes optimize the presigned URL generation process and streamline API interactions across the application.

* fix: add better-auth dependency to dashboard (#645)

* chore: unify exercise answer data format

* feat: make selfhosting run under one org for simplicity (#647)

* feat: implement self-hosting PRD + license key system

Self-hosting single-org:
- Add PUBLIC_IS_SELFHOSTED to API env, block org creation when org exists
- Auto-add non-members as students in GET /account bootstrap
- getOrgSiteInfo: root domain returns single org for self-hosted
- App init: route to /lms when self-hosted + no orgs
- Hide org switcher and Add Org modal when self-hosted

Invite-only signup:
- Add settings.signup.inviteOnly via ZUpdateOrganization
- Allow public signups toggle in org auth settings
- Signup page: hide form when invite-only and no invite context
- Landing page nav: respect inviteOnly for signup link

First signup auto-verify:
- Self-hosted first signup (no orgs) auto-verifies email in createProfileHook

License key system:
- LICENSE_KEY, LICENSE_VERIFICATION_URL for external verification API
- Feature gates: SSO, token-auth, no-tracking (PostHog/Senja)
- requireLicense middleware for API routes
- Dashboard: license store, gate PostHog/Senja/SSO/token-auth UI
- docs/LICENSE_API_CONTRACT.md for external API spec

Co-authored-by: Rotimi Best <[email protected]>

* feat: get rid of carbon completely

* chore: license verification happens only for selfhosted instance

* fix: in self hosted instance, orgSite is the default

* docs: how to write math in cio editor

* Update packages/db/src/queries/organization/organization.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

* Update apps/api/src/services/license.ts

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

* fix: translations, cursor pr reviews, simplify license fetching and check

* refactor: update analytics setup for cloud and self-hosted deployments; enhance settings merge logic

* refactor: update license fetching URL for development environment; optimize organization query to prevent plan row loss

* more selfhosting improvements

* feat: enhance signup guard for self-hosted instances by requiring organization context when no org ID is provided

* pr reviews

* fix: correct query parameter handling in course navigation

* pr reviews

---------

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Rotimi Best <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: Cursor <[email protected]>
Co-authored-by: Rotimi Best <[email protected]>
Co-authored-by: Abdulmuizz Hamzat <[email protected]>
Co-authored-by: Adeyemi Adetunji <[email protected]>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
2026-03-09 11:18:22 +01:00

11 KiB

Media Storage Decoupling + MinIO Support PRD

Purpose

Decouple ClassroomIO media storage from Cloudflare-specific infrastructure so open-source and self-hosted users can control their own media storage backend, with first-class MinIO support.

Problem Statement

The current media pipeline is tightly coupled to Cloudflare R2 and Cloudflare Browser Rendering APIs. This creates friction for open-source adopters who want full infrastructure ownership, local/private deployments, and non-Cloudflare object storage.

Current tight-coupling points:

  • apps/api/src/utils/s3.ts hardcodes Cloudflare R2 endpoint and credentials.
  • apps/api/src/services/media.ts requires CLOUDFLARE_IMAGE_BUCKET_DOMAIN for image URLs.
  • apps/api/src/routes/course/presign.ts directly uses Cloudflare-bound storage utilities.
  • apps/api/src/utils/lesson-media.ts directly generates download URLs using Cloudflare-bound utilities.
  • apps/api/src/config/env.ts and apps/api/src/constants/index.ts are Cloudflare-first.
  • Docker and docs surface only CLOUDFLARE_* storage setup paths.

Goals

  1. Introduce provider-based object storage architecture in API code.
  2. Preserve current dashboard and API contracts for uploads/downloads.
  3. Support Cloudflare R2 through a generic S3-compatible provider.
  4. Add MinIO as a required, documented, self-hosted storage option.
  5. Keep backward compatibility for existing Cloudflare-based deployments.
  6. Maintain canonical asset metadata and usage tracking without regressions.

Non-Goals (Initial Release)

  • Migrating away from presigned URL upload/download flows.
  • Replacing current video transcoding strategy.
  • Fully decoupling PDF generation from Cloudflare in the same release.
  • Per-organization storage-provider selection (initial scope is instance-level).

Requirements

Functional Requirements

  1. API storage access must go through a provider abstraction layer, not Cloudflare-specific helpers.
  2. Existing presign API endpoints and response shapes must remain unchanged.
  3. Existing media image upload endpoint response shape must remain unchanged.
  4. Download URL generation for lessons/documents must remain transparent to frontend consumers.
  5. Existing Cloudflare deployments must continue working with no required immediate migration.

MinIO Requirements (Must-Have)

  1. MinIO support is required in the first release of this decoupling effort.
  2. MinIO must work with direct browser uploads via presigned URLs.
  3. MinIO must support three buckets equivalent to current model:
  • videos
  • documents
  • media
  1. MinIO path-style compatibility must be supported.
  2. Docker self-host flow must include an option to run MinIO locally.
  3. Self-host docs must include complete MinIO setup steps:
  • bucket creation
  • credentials
  • CORS rules
  • required env vars
  • verification steps

Backward Compatibility Requirements

  1. If new storage env vars are absent and CLOUDFLARE_* vars are present, runtime must auto-map to Cloudflare-compatible mode.
  2. Existing frontend flows must not require code changes for basic upload/download behavior.
  3. Existing storageProvider values in assets should continue to validate and persist.

Non-Functional Requirements

  1. No material latency regression for presign endpoints.
  2. Error messages should be provider-agnostic and actionable.
  3. Security posture must remain consistent:
  • short-lived presigned URLs
  • content-type restrictions
  • file-size validation
  • principle of least privilege for storage credentials

Current State Audit

Area Current State Gap Priority
Object storage client Cloudflare R2 hardcoded No provider abstraction P0
Presigned URLs Cloudflare-bound utility Cannot swap provider P0
Image upload public URL Cloudflare domain env only No generic public URL strategy P0
Env config CLOUDFLARE_* only No generic object storage config P0
Docker self-host No MinIO service path Hard for OSS users to self-own media P0
Docs Cloudflare-first guidance Missing MinIO-first self-host path P0
Asset uniqueness constraint Unique key excludes storageProvider Potential collision if multiple providers introduced later P1
PDF rendering Cloudflare-specific browser rendering API Separate Cloudflare dependency remains P2

Proposed Architecture

1) Storage Provider Abstraction

Create a provider interface under API utils/services layer.

Suggested interface responsibilities:

  • uploadObject
  • getObject
  • deleteObject
  • createUploadPresignedUrl
  • createDownloadPresignedUrl
  • buildPublicUrl

2) S3-Compatible Provider

Implement a single S3-compatible provider using AWS SDK v3, configured by env.

This provider should support:

  • Cloudflare R2
  • MinIO
  • AWS S3
  • Other S3-compatible object stores

3) Config Model

Add provider-neutral env vars in API config:

  • OBJECT_STORAGE_DRIVER (initial values: s3-compatible)
  • OBJECT_STORAGE_REGION
  • OBJECT_STORAGE_ENDPOINT
  • OBJECT_STORAGE_PUBLIC_ENDPOINT (optional; used for browser-facing presigned URLs where needed)
  • OBJECT_STORAGE_ACCESS_KEY_ID
  • OBJECT_STORAGE_SECRET_ACCESS_KEY
  • OBJECT_STORAGE_FORCE_PATH_STYLE (important for MinIO compatibility)
  • OBJECT_STORAGE_BUCKET_VIDEOS
  • OBJECT_STORAGE_BUCKET_DOCUMENTS
  • OBJECT_STORAGE_BUCKET_MEDIA
  • OBJECT_STORAGE_MEDIA_PUBLIC_BASE_URL
  • OBJECT_STORAGE_PRESIGN_UPLOAD_EXPIRES_SECONDS
  • OBJECT_STORAGE_PRESIGN_DOWNLOAD_EXPIRES_SECONDS

Backward-compatible fallback mapping from existing vars:

  • CLOUDFLARE_ACCOUNT_ID
  • CLOUDFLARE_ACCESS_KEY
  • CLOUDFLARE_SECRET_ACCESS_KEY
  • CLOUDFLARE_BUCKET_DOMAIN
  • CLOUDFLARE_IMAGE_BUCKET_DOMAIN

4) API Contract Stability

Keep these contracts unchanged:

  • POST /course/presign/video/upload
  • POST /course/presign/document/upload
  • POST /course/presign/video/download
  • POST /course/presign/document/download
  • POST /media/image

5) Data Model Considerations

Current assets schema default storage_provider='s3' is acceptable for initial rollout.
If multi-provider-per-instance or migration coexistence is introduced, consider updating unique constraint:

  • from (organization_id, provider, storage_key)
  • to (organization_id, provider, storage_provider, storage_key)

Complexity Assessment

Workstream Complexity Why
Storage abstraction refactor (apps/api) High Core upload/download paths and presign generation are central and cross-cutting
MinIO support in presign/browser flows High Requires endpoint/path-style/CORS correctness across API and browser contexts
Env/config migration with backward compatibility Medium Needs careful fallback behavior and clear deprecation strategy
Docker self-host MinIO integration Medium Additional service wiring, bucket bootstrap, and docs updates
Dashboard compatibility Low Existing frontend contracts can remain unchanged if API contracts are preserved
DB uniqueness hardening for future multi-provider Medium Requires migration planning and index/constraint rollout
Testing and regression coverage High Upload/download flows span API, browser, storage service, and content rendering paths
PDF rendering decoupling Out of scope in this release Separate dependency track and broader decision surface

Implementation Plan

Phase 0: Foundation

  1. Introduce provider interface and s3-compatible provider implementation.
  2. Replace direct imports from Cloudflare-bound storage utility across:
  • apps/api/src/services/media.ts
  • apps/api/src/routes/course/presign.ts
  • apps/api/src/utils/lesson-media.ts
  1. Implement provider-neutral config in env schema and constants.
  2. Add Cloudflare fallback mapping and warning logs.

Phase 1: MinIO Required Path

  1. Add MinIO configuration support in API.
  2. Add/extend Docker compose for optional MinIO service.
  3. Add bucket bootstrap instructions (videos/documents/media).
  4. Add CORS guidance specific to browser presigned PUT/GET/HEAD behavior.

Phase 2: Documentation + Hardening

  1. Update self-host docs with MinIO-first workflow.
  2. Update Cloudflare docs to show Cloudflare as one provider option.
  3. Update apps/api/.env.example and docker env templates.
  4. Add integration smoke tests for upload/download and media image URL generation.

Phase 3: Optional Follow-Up

  1. Evaluate schema unique constraint expansion to include storage_provider.
  2. Evaluate PDF rendering provider abstraction (Cloudflare-independent renderer).

Required File Touchpoints

Core API:

  • apps/api/src/config/env.ts
  • apps/api/src/constants/index.ts
  • apps/api/src/utils/s3.ts (or replacement provider module)
  • apps/api/src/services/media.ts
  • apps/api/src/routes/course/presign.ts
  • apps/api/src/utils/lesson-media.ts

Docs and self-host:

  • apps/docs/content/docs/quickstart/self-hosting.mdx
  • apps/docs/content/docs/contributor-guides/cloudflare-setup.mdx
  • apps/api/.env.example
  • docker/docker-compose.yaml
  • docker/docker-compose.prod.yaml
  • docker/.env.prod.example
  • docker/docs/USAGE.md
  • docker/docs/SELF_HOST.md

Optional DB follow-up:

  • packages/db/src/schema.ts
  • corresponding migration files

Testing Strategy

Unit Tests

  1. Provider config parsing and fallback behavior.
  2. Presigned URL generation for upload/download.
  3. Public URL generation for media bucket objects.

Integration Tests

  1. Presign video upload endpoint returns valid URL + key.
  2. Presign document upload endpoint returns valid URL + key.
  3. Presign download endpoints return URL map for provided keys.
  4. Media image upload endpoint uploads and returns public URL.

Manual Verification Matrix

  1. Cloudflare R2 existing env-only setup remains functional.
  2. MinIO self-host setup works end-to-end for:
  • video upload
  • document upload
  • thumbnail/image upload
  • lesson playback/download link resolution

Risks and Mitigations

Risk Impact Mitigation
Presigned URL host mismatch in containerized MinIO setups Upload/download failures in browser Support public endpoint config and document deployment topology clearly
CORS misconfiguration in MinIO buckets Browser request failures Provide explicit CORS examples and startup checklist
Hidden reliance on Cloudflare-specific behavior Runtime regressions Add compatibility tests and phased rollout with fallback
Config ambiguity between old and new env vars Misconfiguration risk Define clear precedence, validation, and startup logs
Future multi-provider collisions in assets uniqueness Data integrity risk Plan optional schema hardening phase with migration

Acceptance Criteria

  1. A self-hosted deployment can use MinIO without any Cloudflare credentials.
  2. Existing Cloudflare deployments continue to work without immediate config rewrite.
  3. Dashboard upload/playback/download behavior remains unchanged for users.
  4. Docs provide a complete MinIO setup path from zero to working upload/playback.
  5. Docker self-host users can run a documented MinIO-backed stack.

Open Questions

  1. Should we support two endpoints in config by default (internal for API operations, public for presigned browser URLs), or require one endpoint reachable by both?
  2. Do we want to enforce a strict enum for storageProvider in validation now, or keep it flexible string-based for compatibility?
  3. Should PDF rendering decoupling be planned as a separate PRD immediately after this release?