# artin0123/teimg architecture

> Architecture diagram of the GitHub repository artin0123/teimg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/artin0123/teimg
- Repository: https://github.com/artin0123/teimg

## Overview

#### Purpose and Delivered Surface
CloudFlare-ImgBed is a multi-backend file-hosting service with upload, browsing, management, deletion, public/random access, REST APIs, and WebDAV. The repository ships the compiled SPA rather than frontend source in `frontend-dist/index.html` and `frontend-dist/js/app.4b2a8bcd.js`; backend behavior is implemented as JavaScript edge/server routes.

#### Deployment Entry Points
Two runtime adapters expose the same functional route tree:
- Cloudflare Worker entry: `deploy/worker/index.js`, with generated routing/config support in `deploy/worker/generate-routes.js` and `deploy/worker/wrangler.toml`.
- Docker/Node server entry: `deploy/server/index.js`, bootstrapped through `deploy/server/loader.mjs` and `deploy/server/register.mjs`.
- Container deployment is defined by `Dockerfile` and `docker-compose.yml`. Deployment paths bridge Cloudflare-oriented APIs such as D1/R2 for local/server execution.

#### HTTP Route Domains
Cloudflare Pages Functions provide the main API surface under `functions/`:
- File read/delivery is handled by `functions/file/[[path]].js` behind `functions/file/_middleware.js`.
- Upload entry and multipart/chunk handling live in `functions/upload/index.js`, `functions/upload/chunkUpload.js`, and `functions/upload/chunkMerge.js`.
- WebDAV is a distinct protocol boundary at `functions/dav/[[path]].js`, with access middleware in `functions/dav/_middleware.js`.
- Management operations—listing, mutations, metadata, quota, batch backup/restore—are rooted in `functions/api/manage/_middleware.js` and `functions/api/manage/list.js`.

#### Authentication and Request Policy
Auth endpoints cover login, logout, admin login, reset, and session checks at `functions/api/auth/login.js`, `functions/api/auth/adminLogin.js`, and `functions/api/auth/sessionCheck.js`. Shared auth logic separates user/session/token concerns in `functions/utils/auth/authCore.js`, `functions/utils/auth/sessionManager.js`, and `functions/utils/auth/tokenValidator.js`. Route middleware creates the control boundary protecting uploads, file operations, management APIs, and WebDAV.

#### Storage and Metadata Plane
Content storage is provider-adapted: `functions/utils/storage/telegramAPI.js`, `functions/utils/storage/discordAPI.js`, `functions/utils/storage/huggingfaceAPI.js`, and `functions/utils/storage/webdavAPI.js` represent external channel integrations. Cloudflare R2 is supported by the server-side adapter `deploy/server/r2Storage.js`. Metadata, channel configuration, credentials, and visibility/security policy are handled in `functions/utils/metadata/channelConfig.js`, `functions/utils/metadata/channelCredentials.js`, and `functions/utils/metadata/metadataSecurity.js`.

#### Database and State Boundaries
Persistent metadata/configuration uses a database abstraction in `functions/utils/databaseAdapter.js`, with Cloudflare D1 access in `functions/utils/d1Database.js`; Docker/server emulation is provided by `deploy/server/sqliteD1.js`. The base schema and upgrades are versioned in `database/init.sql` and `database/migrations/v2.2.1_add_tags_column.sql`. Management and public listing routes query this plane rather than storage providers directly.

#### Primary Flows and Edge Services
Upload requests pass middleware, validate/process upload data, persist binary content through the selected storage channel, then record metadata/index state. Retrieval resolves a path through file middleware and storage/index logic; management mutates both metadata and remote objects. Public and random-image experiences are exposed through `functions/api/public/list.js`, `functions/random/index.js`, and `functions/random/adaptive.js`. Cache invalidation is centralized in `functions/utils/purgeCache.js`; Bing wallpaper retrieval is an external content integration at `functions/api/bing/wallpaper/index.js`.

Main entry points: [`functions/utils/databaseAdapter.js`](https://github.com/artin0123/teimg/blob/main/functions/utils/databaseAdapter.js) (Database abstraction), [`functions/file/[[path]].js`](https://github.com/artin0123/teimg/blob/main/functions/file/%5B%5Bpath%5D%5D.js) (File delivery), [`functions/upload/index.js`](https://github.com/artin0123/teimg/blob/main/functions/upload/index.js) (Upload pipeline), [`functions/api/manage/list.js`](https://github.com/artin0123/teimg/blob/main/functions/api/manage/list.js) (Management API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime adapters"]
  node_spa["Compiled SPA<br/>frontend asset<br/>[index.html]"]
  node_worker{{"Cloudflare Worker<br/>edge entry<br/>[index.js]"}}
  node_node{{"Docker Node server<br/>server entry<br/>[index.js]"}}
end

subgraph group_routes["HTTP surfaces"]
  node_file["File delivery<br/>file routes<br/>[&#91;&#91;path&#93;&#93;.js]"]
  node_upload["Upload pipeline<br/>upload routes<br/>[index.js]"]
  node_dav["WebDAV gateway<br/>protocol route<br/>[&#91;&#91;path&#93;&#93;.js]"]
  node_manage["Management API<br/>management routes<br/>[list.js]"]
  node_auth["Auth API<br/>auth routes<br/>[login.js]"]
  node_public["Public listing<br/>public route<br/>[list.js]"]
  node_random["Random images<br/>public route<br/>[index.js]"]
  node_bing["Bing wallpaper<br/>external content route<br/>[index.js]"]
end

subgraph group_core["Core services"]
  node_file_policy["File access policy<br/>route middleware<br/>[_middleware.js]"]
  node_dav_policy["WebDAV access policy<br/>route middleware<br/>[_middleware.js]"]
  node_auth_core["Sessions and tokens<br/>auth service<br/>[authCore.js]"]
  node_metadata["Metadata security<br/>metadata service"]
  node_cache["Cache purge<br/>cache invalidation<br/>[purgeCache.js]"]
end

subgraph group_storage["Storage and state"]
  node_database[("Database abstraction<br/>metadata persistence<br/>[databaseAdapter.js]")]
  node_d1[("Cloudflare D1<br/>database adapter<br/>[d1Database.js]")]
  node_sqlite[("SQLite D1 emulation<br/>server database adapter<br/>[sqliteD1.js]")]
  node_channels["External storage channels<br/>storage provider adapter<br/>[telegramAPI.js]"]
  node_r2["R2 storage adapter<br/>storage provider adapter<br/>[r2Storage.js]"]
  node_schema["Database schema<br/>SQL schema<br/>[init.sql]"]
end

node_spa -->|"served by"| node_worker
node_spa -->|"served by"| node_node
node_worker -->|"routes requests"| node_file
node_worker -->|"routes requests"| node_upload
node_worker -->|"routes requests"| node_dav
node_node -->|"runs route tree"| node_file
node_node -->|"runs route tree"| node_upload
node_file -->|"protected by"| node_file_policy
node_dav -->|"protected by"| node_dav_policy
node_upload -->|"authenticates through"| node_auth_core
node_manage -->|"authenticates through"| node_auth_core
node_dav_policy -->|"validates access"| node_auth_core
node_auth -->|"creates and checks sessions"| node_auth_core
node_upload -->|"stores content"| node_channels
node_upload -->|"records upload state"| node_metadata
node_file -->|"retrieves content"| node_channels
node_file -->|"resolves metadata"| node_database
node_dav -->|"reads and writes objects"| node_channels
node_manage -->|"queries and mutates"| node_database
node_manage -->|"mutates remote objects"| node_channels
node_public -->|"lists indexed files"| node_database
node_random -->|"selects indexed files"| node_database
node_metadata -->|"persists configuration"| node_database
node_database -->|"uses on Cloudflare"| node_d1
node_database -->|"uses on server"| node_sqlite
node_schema -.->|"defines schema"| node_d1
node_node -->|"adapts R2 access"| node_r2
node_manage -->|"purges after mutations"| node_cache

click node_spa "https://github.com/artin0123/teimg/blob/main/frontend-dist/index.html"
click node_worker "https://github.com/artin0123/teimg/blob/main/deploy/worker/index.js"
click node_node "https://github.com/artin0123/teimg/blob/main/deploy/server/index.js"
click node_file "https://github.com/artin0123/teimg/blob/main/functions/file/%5B%5Bpath%5D%5D.js"
click node_upload "https://github.com/artin0123/teimg/blob/main/functions/upload/index.js"
click node_dav "https://github.com/artin0123/teimg/blob/main/functions/dav/%5B%5Bpath%5D%5D.js"
click node_manage "https://github.com/artin0123/teimg/blob/main/functions/api/manage/list.js"
click node_auth "https://github.com/artin0123/teimg/blob/main/functions/api/auth/login.js"
click node_public "https://github.com/artin0123/teimg/blob/main/functions/api/public/list.js"
click node_random "https://github.com/artin0123/teimg/blob/main/functions/random/index.js"
click node_file_policy "https://github.com/artin0123/teimg/blob/main/functions/file/_middleware.js"
click node_dav_policy "https://github.com/artin0123/teimg/blob/main/functions/dav/_middleware.js"
click node_auth_core "https://github.com/artin0123/teimg/blob/main/functions/utils/auth/authCore.js"
click node_metadata "https://github.com/artin0123/teimg/blob/main/functions/utils/metadata/metadataSecurity.js"
click node_cache "https://github.com/artin0123/teimg/blob/main/functions/utils/purgeCache.js"
click node_database "https://github.com/artin0123/teimg/blob/main/functions/utils/databaseAdapter.js"
click node_d1 "https://github.com/artin0123/teimg/blob/main/functions/utils/d1Database.js"
click node_sqlite "https://github.com/artin0123/teimg/blob/main/deploy/server/sqliteD1.js"
click node_channels "https://github.com/artin0123/teimg/blob/main/functions/utils/storage/telegramAPI.js"
click node_r2 "https://github.com/artin0123/teimg/blob/main/deploy/server/r2Storage.js"
click node_schema "https://github.com/artin0123/teimg/blob/main/database/init.sql"
click node_bing "https://github.com/artin0123/teimg/blob/main/functions/api/bing/wallpaper/index.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa,node_worker,node_node toneBlue
class node_file,node_upload,node_dav,node_manage,node_auth,node_public,node_random,node_bing toneAmber
class node_file_policy,node_dav_policy,node_auth_core,node_metadata,node_cache toneMint
class node_database,node_d1,node_sqlite,node_channels,node_r2,node_schema toneRose
```

## Components

### Runtime adapters

- [Compiled SPA](https://github.com/artin0123/teimg/blob/main/frontend-dist/index.html): frontend asset · Delivered client application. · `frontend-dist/index.html`
- [Cloudflare Worker](https://github.com/artin0123/teimg/blob/main/deploy/worker/index.js): edge entry · `deploy/worker/index.js`
- [Docker Node server](https://github.com/artin0123/teimg/blob/main/deploy/server/index.js): server entry · `deploy/server/index.js`

### HTTP surfaces

- [File delivery](https://github.com/artin0123/teimg/blob/main/functions/file/%5B%5Bpath%5D%5D.js): file routes · `functions/file/[[path]].js`
- [Upload pipeline](https://github.com/artin0123/teimg/blob/main/functions/upload/index.js): upload routes · Handles regular, multipart, and chunked uploads. · `functions/upload/index.js`
- [WebDAV gateway](https://github.com/artin0123/teimg/blob/main/functions/dav/%5B%5Bpath%5D%5D.js): protocol route · `functions/dav/[[path]].js`
- [Management API](https://github.com/artin0123/teimg/blob/main/functions/api/manage/list.js): management routes · Lists and mutates files, metadata, quota, and backups. · `functions/api/manage/list.js`
- [Auth API](https://github.com/artin0123/teimg/blob/main/functions/api/auth/login.js): auth routes · `functions/api/auth/login.js`
- [Public listing](https://github.com/artin0123/teimg/blob/main/functions/api/public/list.js): public route · `functions/api/public/list.js`
- [Random images](https://github.com/artin0123/teimg/blob/main/functions/random/index.js): public route · `functions/random/index.js`
- [Bing wallpaper](https://github.com/artin0123/teimg/blob/main/functions/api/bing/wallpaper/index.js): external content route · `functions/api/bing/wallpaper/index.js`

### Core services

- [File access policy](https://github.com/artin0123/teimg/blob/main/functions/file/_middleware.js): route middleware · `functions/file/_middleware.js`
- [WebDAV access policy](https://github.com/artin0123/teimg/blob/main/functions/dav/_middleware.js): route middleware · `functions/dav/_middleware.js`
- [Sessions and tokens](https://github.com/artin0123/teimg/blob/main/functions/utils/auth/authCore.js): auth service · `functions/utils/auth/authCore.js`
- [Metadata security](https://github.com/artin0123/teimg/blob/main/functions/utils/metadata/metadataSecurity.js): metadata service · Manages channel configuration, credentials, and visibility policy. · `functions/utils/metadata/metadataSecurity.js`
- [Cache purge](https://github.com/artin0123/teimg/blob/main/functions/utils/purgeCache.js): cache invalidation · `functions/utils/purgeCache.js`

### Storage and state

- [Database abstraction](https://github.com/artin0123/teimg/blob/main/functions/utils/databaseAdapter.js): metadata persistence · `functions/utils/databaseAdapter.js`
- [Cloudflare D1](https://github.com/artin0123/teimg/blob/main/functions/utils/d1Database.js): database adapter · `functions/utils/d1Database.js`
- [SQLite D1 emulation](https://github.com/artin0123/teimg/blob/main/deploy/server/sqliteD1.js): server database adapter · `deploy/server/sqliteD1.js`
- [External storage channels](https://github.com/artin0123/teimg/blob/main/functions/utils/storage/telegramAPI.js): storage provider adapter · Telegram, Discord, Hugging Face, and WebDAV content backends. · `functions/utils/storage/telegramAPI.js`
- [R2 storage adapter](https://github.com/artin0123/teimg/blob/main/deploy/server/r2Storage.js): storage provider adapter · `deploy/server/r2Storage.js`
- [Database schema](https://github.com/artin0123/teimg/blob/main/database/init.sql): SQL schema · `database/init.sql`

## Connections

- Compiled SPA → Cloudflare Worker: served by
- Compiled SPA → Docker Node server: served by
- Cloudflare Worker → File delivery: routes requests
- Cloudflare Worker → Upload pipeline: routes requests
- Cloudflare Worker → WebDAV gateway: routes requests
- Docker Node server → File delivery: runs route tree
- Docker Node server → Upload pipeline: runs route tree
- File delivery → File access policy: protected by
- WebDAV gateway → WebDAV access policy: protected by
- Upload pipeline → Sessions and tokens: authenticates through
- Management API → Sessions and tokens: authenticates through
- WebDAV access policy → Sessions and tokens: validates access
- Auth API → Sessions and tokens: creates and checks sessions
- Upload pipeline → External storage channels: stores content
- Upload pipeline → Metadata security: records upload state
- File delivery → External storage channels: retrieves content
- File delivery → Database abstraction: resolves metadata
- WebDAV gateway → External storage channels: reads and writes objects
- Management API → Database abstraction: queries and mutates
- Management API → External storage channels: mutates remote objects
- Public listing → Database abstraction: lists indexed files
- Random images → Database abstraction: selects indexed files
- Metadata security → Database abstraction: persists configuration
- Database abstraction → Cloudflare D1: uses on Cloudflare
- Database abstraction → SQLite D1 emulation: uses on server
- Database schema → Cloudflare D1: defines schema
- Docker Node server → R2 storage adapter: adapts R2 access
- Management API → Cache purge: purges after mutations
