backbone-bucket 0.4.3

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Bucket Module

**Domain:** File Storage & Content Management
**Type:** Backbone domain module (library crate)
**Architecture:** Domain-Driven Design — 4 layers (`domain` / `application` / `infrastructure` / `presentation`)
**Source of truth:** YAML schemas under [`schema/models/`](schema/models)

Most of this crate is generated from the schemas. Hand-written
additions live behind `// <<< CUSTOM` markers (for generated files) or
in sibling `*_custom.rs` files that the generator never touches.

---

## Table of contents

1. [What you get]#what-you-get
2. [Module setup]#module-setup
3. [Bucket usage — step by step]#bucket-usage--step-by-step
   - [1. Create a bucket]#1-create-a-bucket
   - [2. Read a bucket]#2-read-a-bucket
   - [3. List & search buckets]#3-list--search-buckets
   - [4. Update a bucket]#4-update-a-bucket
   - [5. Soft-delete, restore, empty trash]#5-soft-delete-restore-empty-trash
   - [6. State transitions]#6-state-transitions-lock--unlock--archive--
   - [7. Bulk & upsert]#7-bulk--upsert
   - [8. Counts]#8-counts
4. [File upload (multipart HTTP)]#file-upload-multipart-http
5. [File serving]#file-serving
6. [Configuration]#configuration
7. [Architecture]#architecture
8. [Testing]#testing
9. [Regeneration & custom code]#regeneration--custom-code
10. [Documentation index]#documentation-index

---

## What you get

| Surface | Description | Where it lives |
|---|---|---|
| **CRUD router** | 12+ standard endpoints per entity (list, create, get, update, patch, soft delete, bulk, upsert, trash, restore, empty, count) | `BucketModule::crud_router()` |
| **Upload router** | `multipart/form-data` HTTP entry — single-shot + resumable (initiate → parts → complete) | `BucketModule::upload_router::<A>()` |
| **Serving router** | Auth-aware delivery: 302 to presigned URL, byte stream, or JSON | `BucketModule::serving_router::<A>()` |
| **gRPC** (feature `grpc`) | Mirror of the CRUD surface | `presentation::grpc` |

**13 generated entities:** Bucket, StoredFile, FileVersion, FileShare,
FileLock, FileComment, ContentHash, UploadSession, ConversionJob,
ProcessingJob, Thumbnail, UserQuota, AccessLog.

**7 custom services** (regen-safe): `LockingService`,
`DeduplicationService`, `MultipartUploadService`, `ConversionService`,
`CdnService`, `VideoThumbnailService`, `DocumentPreviewService`.

---

## Module setup

```rust
use std::sync::Arc;
use sqlx::PgPool;
use backbone_bucket::{
    BucketModule, BucketConfig, StorageConfig, ServingConfig, ServingMode,
    UploadConfig,
    storage::{ObjectStorage, S3Storage, LocalStorage},
    auth::{HasOwnerId, AuthzPolicy, DefaultOwnerOnlyPolicy},
};
use url::Url;
use std::time::Duration;

// 1. Database
let pool: PgPool = sqlx::PgPool::connect(&std::env::var("DATABASE_URL")?).await?;

// 2. Storage backend — LocalStorage for dev, S3Storage in prod
let storage: Arc<dyn ObjectStorage> = Arc::new(
    LocalStorage::new(
        "/var/data/bucket".into(),
        Url::parse("http://localhost:3000/cdn/")?,
        "BUCKET_SIGNING_SECRET",   // env var name
    )?
);

// 3. Bucket-module runtime config
let config = BucketConfig {
    enabled: true,
    storage: StorageConfig::Local {
        root: "/var/data/bucket".into(),
        base_url: Url::parse("http://localhost:3000/cdn/")?,
        signing_secret_env: "BUCKET_SIGNING_SECRET".into(),
    },
    serving: ServingConfig {
        default_mode: ServingMode::Redirect,
        public_prefix: "public/".into(),
        presigned_ttl: Duration::from_secs(300),
    },
};

// 4. Build the module
let bucket = BucketModule::builder()
    .with_database(pool)
    .with_storage(storage)
    .with_config(config)
    .build()?;

// 5a. Simple wiring — one merged router (CRUD + upload + serving).
//     `MyUser` must implement Axum's `FromRequestParts` (so it's an
//     `AuthExtractor`) AND `HasOwnerId` (so `owner_id` is derived from
//     auth, never trusted from the request body).
let policy = Arc::new(DefaultOwnerOnlyPolicy);
let opts = RouterOptions::new(policy);
let app = axum::Router::new()
    .nest("/api/v1/bucket", bucket.router::<MyUser>(opts)?);

// 5b. Advanced wiring — mount each surface independently when you need
//     different prefixes, middleware, or auth policies per router.
let app = axum::Router::new()
    .nest("/api/v1/bucket", bucket.crud_router())
    .nest("/api/v1/bucket", bucket.upload_router::<MyUser>(UploadConfig::default())?)
    .nest("/cdn",           bucket.serving_router::<MyUser>(policy)?);
```

### Loading config from the environment

For first-time wiring, prefer `BucketConfig::from_env()` over
hand-building the struct:

```rust
// Reads BUCKET_STORAGE_BACKEND, BUCKET_STORAGE_ROOT, BUCKET_BASE_URL, …
// (S3 path) BUCKET_S3_ENDPOINT, BUCKET_S3_REGION, BUCKET_S3_PRIVATE_BUCKET, …
// See `BucketConfig::from_env` rustdoc for the full list.
let config = BucketConfig::from_env()?;
```

`BucketConfig::default()` produces a `LocalStorage` dev shape rooted at
`/tmp/bucket` with redirect-mode serving — useful for tests and the
example project.

---

## Bucket usage — step by step

All examples use `BASE = http://localhost:3000/api/v1/bucket` (matches
the `nest` above). Swap in your own prefix. Auth is whatever your
`AuthExtractor` requires; the examples assume `Authorization: Bearer
<jwt>`.

### Bucket fields (cheat sheet)

| Field | Type | Notes |
|---|---|---|
| `name` | string (req, ≤ 255) | Display name |
| `slug` | string (req, unique, ≤ 255, lowercased) | URL-safe identifier — **how you usually look it up** |
| `description` | string? | Free text |
| `owner_id` | uuid (req) | FK → User |
| `bucket_type` | enum | `user` (default) / `shared` / `system` / `temp` |
| `status` | enum | `active` (default) / `readonly` / `locked` / `archived` / `deleted` |
| `storage_backend` | enum | `local` (default) / `s3` / … |
| `root_path` | string (req, ≤ 1024) | Path inside the backend |
| `max_file_size` | int64? | bytes; `null` = no limit |
| `allowed_mime_types` | string[] | empty = all |
| `auto_delete_after_days` | int? | for `temp` buckets |
| `enable_cdn` / `enable_versioning` / `enable_deduplication` | bool | toggles |

Full schema: [schema/models/bucket.model.yaml](schema/models/bucket.model.yaml).

### 1. Create a bucket

```bash
curl -X POST "$BASE/buckets" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "name": "Product Images",
    "slug": "product-images",
    "description": "Public marketing imagery",
    "owner_id": "00000000-0000-0000-0000-000000000001",
    "bucket_type": "shared",
    "storage_backend": "s3",
    "root_path": "product-images",
    "enable_cdn": true,
    "enable_versioning": true,
    "enable_deduplication": true,
    "metadata": {}
  }'
```

Response: `201 Created` + the persisted bucket (UUID `id`, audit metadata, defaults filled in).

### 2. Read a bucket

By `id`:
```bash
curl -H "Authorization: Bearer $TOKEN" "$BASE/buckets/<id>"
```

By `slug` (no dedicated endpoint — slug is unique, so filter the list):
```bash
curl -H "Authorization: Bearer $TOKEN" \
  "$BASE/buckets?search=product-images&page_size=1"
```

### 3. List & search buckets

The list endpoint accepts these query parameters (all optional):

| Param | Default | Meaning |
|---|---|---|
| `page` | 1 | 1-based page index |
| `page_size` | 20 | Items per page (≤ 100) |
| `sort_by` | `created_at` | Column name |
| `sort_direction` | `desc` | `asc` / `desc` |
| `search` | — | Substring match against text fields (`name`, `slug`, `description`) |
| `status` | — | Filter by status enum |
| `tags` | — | Comma-separated tag filter |
| `created_by` | — | Filter by creator user id |

```bash
# Page 2, 50 per page, alphabetical by name
curl -H "Authorization: Bearer $TOKEN" \
  "$BASE/buckets?page=2&page_size=50&sort_by=name&sort_direction=asc"

# All active 'shared' buckets owned by a user, name contains "image"
curl -H "Authorization: Bearer $TOKEN" \
  "$BASE/buckets?status=active&search=image&created_by=00000000-0000-0000-0000-000000000001"
```

Response shape:
```json
{
  "success": true,
  "data": [ /* bucket objects */ ],
  "pagination": { "page": 2, "page_size": 50, "total": 137, "total_pages": 3 }
}
```

### 4. Update a bucket

Full update (`PUT` — send the whole representation):
```bash
curl -X PUT "$BASE/buckets/<id>" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{ "name": "Product Images (v2)", "slug": "product-images", "...": "..." }'
```

Partial update (`PATCH` — send only the changed fields):
```bash
curl -X PATCH "$BASE/buckets/<id>" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{ "enable_cdn": false, "max_file_size": 104857600 }'
```

### 5. Soft-delete, restore, empty trash

```bash
# Soft delete — sets metadata.deleted_at; bucket disappears from list
curl -X DELETE -H "Authorization: Bearer $TOKEN" "$BASE/buckets/<id>"

# Browse the trash
curl -H "Authorization: Bearer $TOKEN" "$BASE/buckets/trash"

# Bring it back
curl -X POST -H "Authorization: Bearer $TOKEN" "$BASE/buckets/<id>/restore"

# Permanent delete one bucket from trash
curl -X DELETE -H "Authorization: Bearer $TOKEN" "$BASE/buckets/trash/<id>"

# Permanent delete EVERYTHING in trash
curl -X DELETE -H "Authorization: Bearer $TOKEN" "$BASE/buckets/empty"
```

### 6. State transitions (lock / unlock / archive / …)

The Bucket state machine adds explicit transitions on top of CRUD.
They live under `/buckets/:id/transitions/<action>`:

| Action | Effect |
|---|---|
| `lock` | `active` → `locked` (no writes) |
| `unlock` | `locked` → `active` |
| `archive` | `active` → `archived` (read-only, hidden from default list) |
| `restore` | `archived` → `active` |
| `delete` | Triggers state-machine-managed delete (audited) |

```bash
curl -X POST -H "Authorization: Bearer $TOKEN" \
  "$BASE/buckets/<id>/transitions/archive"
```

### 7. Bulk & upsert

```bash
# Bulk create (one round-trip, transactional)
curl -X POST "$BASE/buckets/bulk" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{ "items": [ { "name": "A", "slug": "a", ... }, { "name": "B", "slug": "b", ... } ] }'

# Upsert by slug (or whatever unique key the schema declares)
curl -X POST "$BASE/buckets/upsert" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{ "slug": "product-images", "name": "Product Images", ... }'
```

### 8. Counts

```bash
curl -H "Authorization: Bearer $TOKEN" "$BASE/buckets/count"          # active
curl -H "Authorization: Bearer $TOKEN" "$BASE/buckets/trash/count"    # in trash
```

> The same 12-endpoint shape applies to every other entity: just swap
> `buckets` for `stored-files`, `file-shares`, `file-locks`,
> `upload-sessions`, etc. See the OpenAPI spec
> ([docs/openapi/bucket-v2.yaml]docs/openapi/bucket-v2.yaml) for the
> per-entity field lists.

---

## File upload (multipart HTTP)

Two flows, both mounted by `upload_router::<A>(UploadConfig)`. `A` must
implement `AuthExtractor + HasOwnerId` — the authenticated identity
supplies `owner_id` (never trusted from the request body).

### Single-shot (one request)

```bash
curl -X POST "$BASE/uploads" \
  -H "Authorization: Bearer $TOKEN" \
  -F "bucket_id=<bucket-uuid>" \
  -F "path=images/2026/hero.png" \
  -F "file=@./hero.png;type=image/png"
```

Body limit defaults to 256 MiB (override via `UploadConfig::single_shot_limit`).

### Resumable (initiate → parts → complete)

```bash
# 1. Initiate
SESSION=$(curl -s -X POST "$BASE/uploads/sessions" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{
    "bucket_id": "<bucket-uuid>",
    "path": "videos/raw/large.mp4",
    "filename": "large.mp4",
    "mime_type": "video/mp4",
    "file_size": 1073741824,
    "chunk_size": 5242880
  }' | jq -r '.session_id')

# 2. Upload parts (1-based, in any order)
curl -X POST "$BASE/uploads/sessions/$SESSION/parts/1" \
  -H "Authorization: Bearer $TOKEN" \
  -F "chunk=@./part-1.bin;type=application/octet-stream"
# ... part 2, 3, …

# 3. Finalize — assembles, persists stored_files row, deletes staged parts
curl -X POST "$BASE/uploads/sessions/$SESSION/complete" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{}'

# Or abort
curl -X DELETE -H "Authorization: Bearer $TOKEN" \
  "$BASE/uploads/sessions/$SESSION"
```

Chunk limit defaults to 16 MiB; tune via `UploadConfig::chunk_limit`.

Full OpenAPI: see the `/uploads/*` operations in
[docs/openapi/bucket-v2.yaml](docs/openapi/bucket-v2.yaml).

---

## File serving

Mount `serving_router` to deliver bytes under pretty URLs:

```rust
let policy = Arc::new(DefaultOwnerOnlyPolicy);
let app = app.nest("/cdn", bucket.serving_router::<MyUser>(policy)?);
```

Three response strategies, configured via `ServingConfig::default_mode`:

| Mode | What the handler returns |
|---|---|
| `Redirect` (default) | `302` to a short-lived presigned URL |
| `Stream` | `200` with the bytes proxied through the service |
| `SignedUrl` | `200 {"url": "..."}` JSON |

Full reference (storage backends, key naming, public-prefix routing,
auth slots): [docs/serving.md](docs/serving.md).

---

## Configuration

### `application.yml`

```yaml
database:
  url: postgresql://root:password@localhost:5432/bucket
  max_connections: 20
  min_connections: 5

bucket:
  enabled: true
  storage:
    backend: local         # or s3
    root: /var/data/bucket
    base_url: http://localhost:3000/cdn/
    signing_secret_env: BUCKET_SIGNING_SECRET
  serving:
    default_mode: redirect # redirect | stream | signed_url
    public_prefix: "public/"
    presigned_ttl_secs: 300
```

### Environment

| Variable | Purpose |
|---|---|
| `DATABASE_URL` | Overrides the configured database URL |
| `BUCKET_SIGNING_SECRET` | HMAC secret for `LocalStorage` presigned URLs (name is configurable) |
| `AWS_*` | Picked up by `S3Storage` via `aws-config` |

Knobs and their defaults are documented in
[docs/CONFIGURATION_THRESHOLDS.md](docs/CONFIGURATION_THRESHOLDS.md).

---

## Architecture

```
bucket/
├── schema/                    SOURCE OF TRUTH
│   ├── models/*.model.yaml    entity DSL (generated → everything else)
│   ├── hooks/                 lifecycle hooks
│   ├── workflows/             saga / flow definitions
│   └── openapi/               generated index
│
├── src/
│   ├── domain/                entities, value objects, state machines, repos (traits)
│   ├── application/
│   │   ├── service/           {entity}_service.rs (type alias) + {entity}_service_custom.rs
│   │   ├── workflows/         multipart upload, file processing, share creation, …
│   │   └── triggers/ validator/ usecases/ events/
│   ├── infrastructure/
│   │   └── persistence/       newtype repos over GenericCrudRepository
│   ├── presentation/
│   │   └── http/
│   │       ├── {entity}_handler.rs   BackboneCrudHandler-driven CRUD
│   │       ├── serving.rs            custom — mode-B file delivery
│   │       └── upload.rs             custom — multipart HTTP upload
│   ├── storage/               ObjectStorage trait + Local/S3 backends (custom)
│   ├── auth/                  AuthExtractor / AuthzPolicy / HasOwnerId (custom)
│   ├── config/                BucketConfig (custom)
│   ├── bucket_module.rs       custom — crud_router / upload_router / serving_router
│   └── lib.rs                 generated re-exports + custom `// <<<` block
│
├── migrations/                <timestamp>_*.up.sql / .down.sql (sqlx convention)
├── docs/                      human-maintained docs (this README + serving.md + bucket-spec.md …)
├── tests/                     integration + workflow + bench tests
└── examples/serving/          runnable wiring example
```

Why this shape: the [project CLAUDE.md](CLAUDE.md) has the
non-negotiable rules and naming conventions.

---

## Testing

```bash
# Unit tests (lib)
cargo test --lib

# Unit + handler-level tests for the upload module
cargo test --lib upload::

# HTTP integration suite — points reqwest at a running server
API_BASE_URL=http://localhost:3000 \
API_AUTH_TOKEN=$(jwt-mint) \
BUCKET_TEST_BUCKET_ID=00000000-0000-0000-0000-000000000001 \
  cargo test --test integration_tests
```

Integration tests skip (rather than fail) when `API_BASE_URL` is
unreachable, so they are safe to leave on in CI without a deployed
backend.

### In-process tests via `InMemoryStorage`

Consumers writing their own tests against the upload / serving routers
should avoid `LocalStorage` (filesystem) and S3 (credentials). Turn on
the `test-utils` feature instead — it ships an `InMemoryStorage` that
implements `ObjectStorage` against a `DashMap`:

```toml
# consumer Cargo.toml
[dev-dependencies]
backbone-bucket = { version = "*", features = ["test-utils"] }
```

```rust
use backbone_bucket::InMemoryStorage;
use std::sync::Arc;

let storage: Arc<dyn backbone_bucket::ObjectStorage> = Arc::new(InMemoryStorage::new());
// ... wire into BucketModule::builder().with_storage(storage)
```

`InMemoryStorage::len()`, `.contains_key(k)`, and `.clear()` are
available for assertions and per-test isolation. Presigned URLs are
synthetic (`memory://...`) and never call out to the network.

---

## Regeneration & custom code

Two rules keep custom code safe across regeneration:

1. **Inside generated files**, put hand-written code between
   `// <<< CUSTOM` and `// END CUSTOM` markers. Everything outside is
   overwritten by `metaphor schema schema generate`.
2. **For larger surfaces**, create a sibling file like
   `account_service_custom.rs` — files that don't match the generated
   filename pattern are never touched.

The custom-safe surfaces in this module today:

- [`src/bucket_module.rs`]src/bucket_module.rs — `crud_router`, `upload_router`, `serving_router`
- [`src/presentation/http/upload.rs`]src/presentation/http/upload.rs — multipart HTTP handler
- [`src/presentation/http/serving.rs`]src/presentation/http/serving.rs — mode-B serving handler
- [`src/storage/`]src/storage/, [`src/auth/`]src/auth/, [`src/config/`]src/config/
- `application/service/*_custom.rs` files
- `tests/integration/tests/upload_multipart_test.rs` (registered via a `// <<< CUSTOM` block in `tests/integration/tests/mod.rs`)

Regenerate everything:

```bash
metaphor schema schema validate       # validate schema YAML
metaphor schema schema generate       # regenerate code from schema
metaphor make entity <Name>           # add a new entity from schema
metaphor migration generate <Entity> <module>   # new migration
metaphor dev test                     # run the suite
metaphor lint check
```

---

## Documentation index

| File | What's inside |
|---|---|
| [`docs/README.md`]docs/README.md | Doc map |
| [`docs/brd.md`]docs/brd.md | Business requirements (917 lines) |
| [`docs/bucket-spec.md`]docs/bucket-spec.md | Module specification |
| [`docs/bucket-plan.md`]docs/bucket-plan.md | Implementation plan |
| [`docs/domain.md`]docs/domain.md | Entity reference (generated from schemas) |
| [`docs/serving.md`]docs/serving.md | File serving — storage + auth + URL shapes |
| [`docs/CONFIGURATION_THRESHOLDS.md`]docs/CONFIGURATION_THRESHOLDS.md | Tunable defaults |
| [`docs/MIGRATION_V2.md`]docs/MIGRATION_V2.md | V1 → V2 migration notes |
| [`docs/code-quality.md`]docs/code-quality.md | Quality / lint conventions |
| [`docs/TRAIT_ABSTRACTION_ANALYSIS.md`]docs/TRAIT_ABSTRACTION_ANALYSIS.md | Design rationale for the trait surface |
| [`docs/openapi/bucket-v2.yaml`]docs/openapi/bucket-v2.yaml | Authoritative OpenAPI 3.0 spec (CRUD + uploads) |
| [`schema/models/`]schema/models | Per-entity schema YAML (the actual source of truth) |
| [`CHANGELOG.md`]CHANGELOG.md | Version history |
| [`CLAUDE.md`]CLAUDE.md | Conventions / rules for AI-assisted edits |

---

## License

Part of the Backbone Framework.