backbone-bucket 0.3.0

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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
# Bucket File Storage System - Technical Domain Documentation

**Version**: 1.0
**Date**: 2025-01-20
**Author**: StartApp Engineering Team
**Purpose**: Authoritative technical reference for implementing Bucket domain layer

---

## Table of Contents

1. [Introduction]#1-introduction
2. [Domain-Driven Design Overview]#2-domain-driven-design-overview
3. [Entities (Aggregate Roots)]#3-entities-aggregate-roots
4. [Value Objects and Enums]#4-value-objects-and-enums
5. [Domain Events]#5-domain-events
6. [State Machines]#6-state-machines
7. [Domain Services]#7-domain-services
8. [Use Cases (CQRS)]#8-use-cases-cqrs
9. [Repositories]#9-repositories
10. [Specifications]#10-specifications
11. [Workflows]#11-workflows
12. [API Endpoints]#12-api-endpoints
13. [Database Schema]#13-database-schema
14. [Security Model]#14-security-model

---

## 1. Introduction

### 1.1 Purpose

This document provides **complete technical specifications** for the Bucket File Storage System domain layer. It serves as:

- **Implementation guide** for repositories, use cases, and services
- **Migration blueprint** for PostgreSQL database
- **API contract** for both Backbone generic CRUD and domain-specific endpoints
- **Security reference** for virus scanning and access control

### 1.2 Architecture Principles

**Domain-Driven Design (DDD)**:
- Clean separation between domain, application, infrastructure, and presentation layers
- Schema-first approach for all domain definitions
- CQRS for read/write separation
- Event-driven architecture for file lifecycle events

**File Storage Architecture**:
- Multi-backend storage support (Local, S3, GCS, Azure Blob)
- Bucket-based organization (similar to S3)
- Quota management with tiered plans
- Virus scanning and security enforcement

**Two-Layer API Architecture**:
- **Layer 1**: Backbone Generic CRUD (auto-generated endpoints)
- **Layer 2**: Domain-Specific Use Cases (upload, download, share)

---

## 2. Domain-Driven Design Overview

### 2.1 Bounded Context

**Bucket File Storage Bounded Context** encompasses:
- File storage and retrieval operations
- Bucket organization and management
- User quota tracking and enforcement
- File sharing and access control
- Virus scanning and threat detection
- Image compression and thumbnail generation
- File versioning and history
- Access logging and audit trail

### 2.2 Ubiquitous Language

| Term | Definition |
|------|------------|
| **StoredFile** | A file uploaded and stored in the system |
| **Bucket** | A container for organizing files (similar to S3 bucket) |
| **UserQuota** | Storage limits and usage tracking for a user |
| **FileShare** | A sharing configuration for file access |
| **FileVersion** | A historical version of a file |
| **Thumbnail** | A compressed preview image of a file |
| **AccessLog** | Record of file access for audit purposes |
| **Storage Key** | Unique identifier for locating a file in storage |
| **Threat Level** | Security classification from virus scan |
| **Soft Delete** | Marking file as deleted without physical removal |
| **Quarantine** | Isolating potentially dangerous files |

### 2.3 Aggregates

**Primary Aggregates**:

1. **StoredFile Aggregate**
   - Root: StoredFile
   - Contains: Thumbnails (one-to-many)
   - Contains: FileVersions (one-to-many)
   - References: Bucket, UserQuota

2. **Bucket Aggregate**
   - Root: Bucket
   - Contains: StoredFiles (one-to-many)
   - Manages: Storage policies, MIME type restrictions

3. **UserQuota Aggregate**
   - Root: UserQuota
   - Tracks: Storage usage, file counts
   - Enforces: Storage limits, file size limits

4. **FileShare Aggregate**
   - Root: FileShare
   - References: StoredFile
   - Controls: Access permissions, download limits

5. **AccessLog Aggregate**
   - Root: AccessLog (append-only, immutable)
   - Tracks: All file operations

---

## 3. Entities (Aggregate Roots)

### 3.1 StoredFile Entity

**Location**: `libs/modules/bucket/src/domain/entity/stored_file.rs`

**Description**: Core aggregate root for file storage, tracking file metadata, security status, and compression state.

**Attributes**:

```rust
pub struct StoredFile {
    // Identity
    pub id: Uuid,
    pub bucket_id: Uuid,
    pub owner_id: Uuid,

    // File metadata
    pub path: String,                      // Virtual path within bucket
    pub original_name: String,             // Original filename
    pub size_bytes: i64,                   // File size in bytes
    pub mime_type: String,                 // MIME type
    pub checksum: Option<String>,          // File hash for integrity

    // Storage
    pub storage_key: String,               // Physical storage location

    // Compression
    pub is_compressed: bool,
    pub original_size: Option<i64>,        // Size before compression
    pub compression_algorithm: Option<String>,

    // Security
    pub is_scanned: bool,                  // Virus scan completed
    pub scan_result: Option<serde_json::Value>,
    pub threat_level: Option<ThreatLevel>,

    // Thumbnail
    pub has_thumbnail: bool,
    pub thumbnail_path: Option<String>,

    // Status
    pub status: FileStatus,

    // Versioning
    pub version: i32,
    pub previous_version_id: Option<Uuid>,

    // Access tracking
    pub download_count: i32,
    pub last_accessed_at: Option<DateTime<Utc>>,

    // Extensibility
    pub metadata: serde_json::Value,
}
```

**Business Rules**:
- Files must belong to an active bucket
- Files must pass virus scan before becoming Active
- Quarantined files cannot be downloaded
- Original name preserved, storage key generated
- Checksum required for integrity verification
- Compression only applied to eligible file types

**Invariants**:
- `id` is immutable after creation
- `bucket_id` and `owner_id` are immutable
- `storage_key` is immutable (files are copy-on-write)
- `size_bytes` must be > 0
- `version` starts at 1 and only increments
- Files in `Deleted` status cannot be re-activated directly

**Custom Methods**:

```rust
impl StoredFile {
    /// Check if file is safe to download
    pub fn is_safe(&self) -> bool;

    /// Check if file needs processing (scanning/compression)
    pub fn needs_processing(&self) -> bool;

    /// Check if file can be accessed
    pub fn is_accessible(&self) -> bool;

    /// Record a download access
    pub fn record_access(&mut self);

    /// Soft delete the file
    pub fn soft_delete(&mut self);

    /// Restore from deleted state
    pub fn restore(&mut self);

    /// Mark file as scanned with result
    pub fn mark_scanned(&mut self, threat_level: ThreatLevel, scan_result: Option<Value>);

    /// Set compression information
    pub fn set_compressed(&mut self, original_size: i64, algorithm: &str);

    /// Quarantine file after threat detection
    pub fn quarantine(&mut self, scan_result: Value, threat_level: ThreatLevel);
}
```

**PostgreSQL Table**: `stored_files`

---

### 3.2 Bucket Entity

**Location**: `libs/modules/bucket/src/domain/entity/bucket.rs`

**Description**: Container for organizing files with configurable policies.

**Attributes**:

```rust
pub struct Bucket {
    // Identity
    pub id: Uuid,
    pub owner_id: Uuid,

    // Naming
    pub name: String,                      // Display name
    pub slug: String,                      // URL-safe identifier
    pub description: Option<String>,

    // Type and status
    pub bucket_type: BucketType,
    pub status: BucketStatus,

    // Storage configuration
    pub storage_backend: StorageBackend,
    pub root_path: String,                 // Base path in storage

    // Statistics
    pub file_count: i32,
    pub total_size_bytes: i64,

    // Policies
    pub max_file_size: Option<i64>,        // Per-file size limit
    pub allowed_mime_types: Vec<String>,   // Allowed MIME patterns
    pub auto_delete_after_days: Option<i32>,

    // Extensibility
    pub metadata: serde_json::Value,
}
```

**Business Rules**:
- Slug must be unique and URL-safe
- Only Active buckets can receive uploads
- Readonly buckets allow downloads only
- Archived buckets are frozen (no operations)
- MIME type patterns support wildcards (e.g., `image/*`)
- Max file size enforced on upload

**Invariants**:
- `id` is immutable
- `slug` is immutable after creation
- `storage_backend` is immutable
- `file_count` and `total_size_bytes` must be >= 0
- Only one status transition at a time

**Custom Methods**:

```rust
impl Bucket {
    /// Validate if upload is allowed
    pub fn can_upload(&self, size: i64, mime_type: &str) -> Result<(), String>;

    /// Update bucket statistics
    pub fn update_stats(&mut self, size_delta: i64, count_delta: i32);

    /// Check if bucket is accessible
    pub fn is_accessible(&self) -> bool;

    /// Lock bucket (readonly)
    pub fn lock(&mut self);

    /// Unlock bucket
    pub fn unlock(&mut self);

    /// Archive bucket
    pub fn archive(&mut self);

    /// Restore from archived
    pub fn restore(&mut self);

    /// Check if bucket is empty
    pub fn is_empty(&self) -> bool;

    /// Calculate average file size
    pub fn average_file_size(&self) -> i64;
}
```

**PostgreSQL Table**: `buckets`

---

### 3.3 UserQuota Entity

**Location**: `libs/modules/bucket/src/domain/entity/user_quota.rs`

**Description**: Storage limits and usage tracking for a user.

**Attributes**:

```rust
pub struct UserQuota {
    // Identity
    pub id: Uuid,
    pub user_id: Uuid,

    // Limits
    pub limit_bytes: i64,                  // Total storage limit
    pub max_file_size: Option<i64>,        // Per-file size limit
    pub max_file_count: Option<i32>,       // Max number of files

    // Usage
    pub used_bytes: i64,
    pub file_count: i32,

    // Tier
    pub tier: String,                      // free, pro, enterprise

    // Warnings
    pub warning_threshold_percent: i32,    // Default: 80
    pub last_warning_sent_at: Option<DateTime<Utc>>,

    // Peak tracking
    pub peak_usage_bytes: i64,
    pub peak_usage_at: Option<DateTime<Utc>>,

    // Extensibility
    pub metadata: serde_json::Value,
}
```

**Business Rules**:
- Each user has exactly one quota record
- Usage cannot exceed limit
- Warnings sent when threshold exceeded
- Peak usage tracked for analytics
- Tier determines default limits

**Tier Limits**:
| Tier | Storage | Max File Size | Max Files |
|------|---------|---------------|-----------|
| free | 1 GB | 50 MB | 1,000 |
| pro | 100 GB | 500 MB | 50,000 |
| enterprise | 1 TB | 5 GB | Unlimited |

**Invariants**:
- `used_bytes` <= `limit_bytes`
- `file_count` >= 0
- `warning_threshold_percent` between 0-100

**Custom Methods**:

```rust
impl UserQuota {
    /// Check if space available for bytes
    pub fn has_space_for(&self, bytes: i64) -> bool;

    /// Get usage percentage
    pub fn usage_percent(&self) -> f64;

    /// Check if over warning threshold
    pub fn is_over_warning_threshold(&self) -> bool;

    /// Add usage (file upload)
    pub fn add_usage(&mut self, bytes: i64) -> Result<(), QuotaExceeded>;

    /// Subtract usage (file delete)
    pub fn subtract_usage(&mut self, bytes: i64);

    /// Get remaining bytes
    pub fn remaining_bytes(&self) -> i64;

    /// Validate file can be uploaded
    pub fn can_upload_file(&self, size: i64) -> Result<(), QuotaExceeded>;

    /// Update peak if current > peak
    pub fn update_peak(&mut self);
}
```

**PostgreSQL Table**: `user_quotas`

---

### 3.4 FileShare Entity

**Location**: `libs/modules/bucket/src/domain/entity/file_share.rs`

**Description**: Sharing configuration for file access.

**Attributes**:

```rust
pub struct FileShare {
    // Identity
    pub id: Uuid,
    pub file_id: Uuid,
    pub owner_id: Uuid,

    // Access token
    pub token: String,                     // URL-safe random token

    // Share type
    pub share_type: ShareType,             // Link, User, Password

    // Target users (for User share type)
    pub shared_with: Vec<Uuid>,

    // Password protection
    pub password_hash: Option<String>,

    // Permissions
    pub permission: SharePermission,       // View, Download, Edit

    // Limits
    pub expires_at: Option<DateTime<Utc>>,
    pub max_downloads: Option<i32>,
    pub download_count: i32,

    // Status
    pub is_active: bool,

    // Revocation
    pub revoked_at: Option<DateTime<Utc>>,
    pub revoked_by: Option<Uuid>,
}
```

**Business Rules**:
- Token must be unique and URL-safe (32+ chars)
- Password shares require password_hash
- User shares require at least one user in shared_with
- Download count cannot exceed max_downloads
- Expired shares are not accessible
- Revoked shares cannot be reactivated

**Invariants**:
- `token` is immutable
- `download_count` <= `max_downloads` (if set)
- `revoked_at` is null if `is_active` is true

**Custom Methods**:

```rust
impl FileShare {
    /// Check if share is still valid
    pub fn is_valid(&self) -> bool;

    /// Check if share has expired
    pub fn is_expired(&self) -> bool;

    /// Check if downloads remaining
    pub fn has_downloads_remaining(&self) -> bool;

    /// Check if user can access
    pub fn can_access(&self, user_id: Option<Uuid>) -> bool;

    /// Verify password
    pub fn verify_password(&self, password: &str) -> bool;

    /// Record download
    pub fn record_download(&mut self) -> bool;

    /// Revoke share
    pub fn revoke(&mut self, by_user_id: Uuid);

    /// Generate share URL
    pub fn generate_url(&self, base_url: &str) -> String;
}
```

**PostgreSQL Table**: `file_shares`

---

### 3.5 FileVersion Entity

**Location**: `libs/modules/bucket/src/domain/entity/file_version.rs`

**Description**: Historical version of a file.

**Attributes**:

```rust
pub struct FileVersion {
    // Identity
    pub id: Uuid,
    pub file_id: Uuid,

    // Version info
    pub version_number: i32,
    pub is_current: bool,

    // Storage
    pub storage_key: String,
    pub storage_backend: String,
    pub size_bytes: i64,
    pub checksum: Option<String>,

    // Metadata
    pub mime_type: String,
    pub original_name: String,
    pub metadata: serde_json::Value,

    // Status
    pub status: FileStatus,

    // Timestamps
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
    pub created_by: Uuid,
}
```

**Business Rules**:
- Only one version can be current at a time
- Versions are append-only (no updates)
- Expired versions auto-delete
- Storage key must be unique per version

**Invariants**:
- `version_number` >= 1
- `storage_key` is immutable
- Only one version per file_id with `is_current = true`

**Custom Methods**:

```rust
impl FileVersion {
    /// Check if version is expired
    pub fn is_expired(&self) -> bool;

    /// Check if version can be restored
    pub fn can_restore(&self) -> bool;

    /// Set as current version
    pub fn set_as_current(&mut self);

    /// Unset current flag
    pub fn unset_current(&mut self);

    /// Soft delete version
    pub fn soft_delete(&mut self);

    /// Get version label (e.g., "v1", "v2")
    pub fn version_label(&self) -> String;

    /// Check if version was restored
    pub fn is_restored(&self) -> bool;

    /// Get checksum or placeholder
    pub fn get_checksum(&self) -> &str;
}
```

**PostgreSQL Table**: `file_versions`

---

### 3.6 Thumbnail Entity

**Location**: `libs/modules/bucket/src/domain/entity/thumbnail.rs`

**Description**: Compressed preview image of a file.

**Attributes**:

```rust
pub struct Thumbnail {
    // Identity
    pub id: Uuid,
    pub file_id: Uuid,

    // Dimensions
    pub width: i32,
    pub height: i32,

    // Storage
    pub storage_key: String,
    pub storage_backend: String,
    pub size_bytes: i64,
    pub mime_type: String,
    pub format: String,                    // jpeg, webp, png

    // Generation
    pub generated_at: DateTime<Utc>,
    pub generation_time_ms: i32,

    // CDN
    pub cdn_url: String,
    pub cache_until: DateTime<Utc>,

    // Quality
    pub quality: i32,                      // 1-100

    // Versioning
    pub source_version: i32,               // File version this was generated from
    pub is_stale: bool,                    // Needs regeneration

    // Metadata
    pub metadata: serde_json::Value,
}
```

**Business Rules**:
- One thumbnail per file per size
- Standard sizes: 64, 128, 256, 512, 1024
- Regenerated when source file changes
- CDN URLs have TTL for cache invalidation

**Invariants**:
- `width` and `height` > 0
- `quality` between 1-100
- `source_version` must match file version when generated

**Custom Methods**:

```rust
impl Thumbnail {
    /// Check if thumbnail needs regeneration
    pub fn needs_regeneration(&self) -> bool;

    /// Mark as stale
    pub fn mark_stale(&mut self);

    /// Mark as fresh after regeneration
    pub fn mark_fresh(&mut self, source_version: i32);

    /// Check if cache expired
    pub fn is_cache_expired(&self) -> bool;

    /// Check if has valid CDN URL
    pub fn has_cdn_url(&self) -> bool;

    /// Get aspect ratio
    pub fn aspect_ratio(&self) -> f64;

    /// Get dimensions tuple
    pub fn dimensions(&self) -> (i32, i32);

    /// Get dimensions string (e.g., "256x256")
    pub fn dimensions_string(&self) -> String;

    /// Get size category (small, medium, large)
    pub fn category_size(&self) -> &str;

    /// Check if for current file version
    pub fn is_current_version(&self, file_version: i32) -> bool;
}
```

**PostgreSQL Table**: `thumbnails`

---

### 3.7 AccessLog Entity

**Location**: `libs/modules/bucket/src/domain/entity/access_log.rs`

**Description**: Immutable record of file access for audit purposes.

**Attributes**:

```rust
pub struct AccessLog {
    // Identity
    pub id: Uuid,
    pub file_id: Uuid,

    // Actor
    pub user_id: Option<Uuid>,             // Null for anonymous access
    pub share_id: Option<Uuid>,            // If accessed via share

    // Action
    pub action: AccessAction,

    // Context
    pub ip_address: Option<String>,
    pub user_agent: Option<String>,
    pub referrer: Option<String>,
    pub location: Option<serde_json::Value>,  // GeoIP data

    // Result
    pub success: bool,
    pub error_code: Option<String>,
    pub error_message: Option<String>,

    // Performance
    pub duration_ms: Option<i32>,
    pub bytes_transferred: Option<i64>,

    // Timestamp
    pub accessed_at: DateTime<Utc>,

    // Metadata
    pub metadata: serde_json::Value,
}
```

**Business Rules**:
- Append-only: No updates or deletes
- Retention: 90 days hot, 7 years archived
- All access attempts logged (success and failure)
- Anonymous access still tracked

**Invariants**:
- All fields immutable after creation
- `accessed_at` set on creation, never updated
- Either `user_id` or `share_id` should be set (or both null for anonymous)

**Custom Methods**:

```rust
impl AccessLog {
    /// Check if access was successful
    pub fn is_successful(&self) -> bool;

    /// Check if access was by owner
    pub fn is_by_owner(&self) -> bool;

    /// Check if access was via share link
    pub fn is_via_share(&self) -> bool;

    /// Check if anonymous access
    pub fn is_anonymous(&self) -> bool;

    /// Check if download action
    pub fn is_download(&self) -> bool;

    /// Check if upload action
    pub fn is_upload(&self) -> bool;

    /// Get formatted duration
    pub fn duration_string(&self) -> String;

    /// Get formatted bytes transferred
    pub fn bytes_transferred_string(&self) -> String;

    /// Get location summary
    pub fn location_summary(&self) -> Option<String>;
}
```

**PostgreSQL Table**: `access_logs`

---

## 4. Value Objects and Enums

### 4.1 FileStatus Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileStatus {
    Uploading,      // File upload in progress
    Processing,     // Being processed (scanning, compression)
    Active,         // Ready for access
    Quarantined,    // Flagged by virus scan
    Deleted,        // Soft deleted
    Archived,       // Long-term storage
}
```

**State Transitions**:
- `Uploading` -> `Processing` (upload complete)
- `Processing` -> `Active` (scan passed)
- `Processing` -> `Quarantined` (threat detected)
- `Active` -> `Deleted` (soft delete)
- `Active` -> `Archived` (archive)
- `Deleted` -> `Active` (restore)
- `Archived` -> `Active` (unarchive)
- `Quarantined` -> `Active` (manual review passed)

---

### 4.2 BucketStatus Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BucketStatus {
    Active,         // Fully operational
    Readonly,       // Downloads only, no uploads
    Locked,         // No operations allowed
    Archived,       // Long-term storage, readonly
}
```

---

### 4.3 BucketType Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BucketType {
    User,           // User's personal bucket
    Team,           // Shared team bucket
    Project,        // Project-specific bucket
    System,         // System/application bucket
    Public,         // Publicly accessible bucket
}
```

---

### 4.4 StorageBackend Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StorageBackend {
    Local,          // Local filesystem
    S3,             // Amazon S3
    Gcs,            // Google Cloud Storage
    AzureBlob,      // Azure Blob Storage
    Minio,          // MinIO (S3-compatible)
}
```

---

### 4.5 ThreatLevel Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThreatLevel {
    Safe,           // No threats detected
    Low,            // Minor concerns
    Medium,         // Potential threats, quarantine recommended
    High,           // Dangerous, block access
    Critical,       // Malware confirmed, immediate action required
}
```

---

### 4.6 ShareType Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShareType {
    Link,           // Public link (anyone with URL)
    User,           // Specific users only
    Password,       // Password-protected link
}
```

---

### 4.7 SharePermission Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SharePermission {
    View,           // Preview only
    Download,       // Download allowed
    Edit,           // Modify allowed
}
```

---

### 4.8 AccessAction Enum

```rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessAction {
    Download,       // File downloaded
    View,           // File viewed/previewed
    Preview,        // Thumbnail/preview accessed
    Share,          // File shared
    ShareAccess,    // Access via share link
    Upload,         // File uploaded
    Delete,         // File deleted
    Rename,         // File renamed
    Move,           // File moved
    Copy,           // File copied
    Restore,        // File restored from trash
    MetadataUpdate, // Metadata modified
}
```

---

### 4.9 QuotaExceeded Error

```rust
#[derive(Debug, Clone)]
pub struct QuotaExceeded {
    pub user_id: Uuid,
    pub used: i64,
    pub limit: i64,
    pub requested: i64,
}

impl std::fmt::Display for QuotaExceeded {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Quota exceeded for user {}: {} + {} > {}",
            self.user_id, self.used, self.requested, self.limit)
    }
}
```

---

## 5. Domain Events

### 5.1 File Events

```rust
// File lifecycle events
pub struct FileUploadedEvent {
    pub file_id: Uuid,
    pub bucket_id: Uuid,
    pub owner_id: Uuid,
    pub file_name: String,
    pub size_bytes: i64,
    pub mime_type: String,
    pub uploaded_at: DateTime<Utc>,
}

pub struct FileDownloadedEvent {
    pub file_id: Uuid,
    pub user_id: Option<Uuid>,
    pub share_id: Option<Uuid>,
    pub ip_address: Option<String>,
    pub downloaded_at: DateTime<Utc>,
}

pub struct FileDeletedEvent {
    pub file_id: Uuid,
    pub deleted_by: Uuid,
    pub deletion_type: DeletionType,  // Soft, Hard
    pub deleted_at: DateTime<Utc>,
}

pub struct FileRestoredEvent {
    pub file_id: Uuid,
    pub restored_by: Uuid,
    pub restored_at: DateTime<Utc>,
}

pub struct FileMovedEvent {
    pub file_id: Uuid,
    pub from_bucket: Uuid,
    pub to_bucket: Uuid,
    pub from_path: String,
    pub to_path: String,
    pub moved_by: Uuid,
    pub moved_at: DateTime<Utc>,
}
```

### 5.2 Security Events

```rust
pub struct FileScannedEvent {
    pub file_id: Uuid,
    pub threat_level: ThreatLevel,
    pub threats_found: Vec<String>,
    pub scan_engine: String,
    pub scanned_at: DateTime<Utc>,
}

pub struct FileQuarantinedEvent {
    pub file_id: Uuid,
    pub threat_level: ThreatLevel,
    pub reason: String,
    pub quarantined_at: DateTime<Utc>,
}

pub struct FileClearedEvent {
    pub file_id: Uuid,
    pub cleared_by: Uuid,
    pub reason: String,
    pub cleared_at: DateTime<Utc>,
}
```

### 5.3 Share Events

```rust
pub struct FileSharedEvent {
    pub share_id: Uuid,
    pub file_id: Uuid,
    pub shared_by: Uuid,
    pub share_type: ShareType,
    pub permission: SharePermission,
    pub expires_at: Option<DateTime<Utc>>,
    pub shared_at: DateTime<Utc>,
}

pub struct ShareAccessedEvent {
    pub share_id: Uuid,
    pub file_id: Uuid,
    pub accessed_by: Option<Uuid>,
    pub ip_address: Option<String>,
    pub accessed_at: DateTime<Utc>,
}

pub struct ShareRevokedEvent {
    pub share_id: Uuid,
    pub revoked_by: Uuid,
    pub reason: Option<String>,
    pub revoked_at: DateTime<Utc>,
}
```

### 5.4 Quota Events

```rust
pub struct QuotaWarningEvent {
    pub user_id: Uuid,
    pub usage_percent: f64,
    pub used_bytes: i64,
    pub limit_bytes: i64,
    pub warned_at: DateTime<Utc>,
}

pub struct QuotaExceededEvent {
    pub user_id: Uuid,
    pub requested_bytes: i64,
    pub used_bytes: i64,
    pub limit_bytes: i64,
    pub operation: String,
    pub occurred_at: DateTime<Utc>,
}
```

---

## 6. State Machines

### 6.1 File Status State Machine

```
                    ┌──────────────┐
                    │  Uploading   │
                    └──────┬───────┘
                           │ upload_complete
                           ▼
                    ┌──────────────┐
                    │  Processing  │
                    └──────┬───────┘
                           │
          ┌────────────────┼────────────────┐
          │ scan_clean     │ threat_found   │
          ▼                ▼                │
   ┌──────────────┐ ┌──────────────┐        │
   │    Active    │ │ Quarantined  │        │
   └──────┬───────┘ └──────┬───────┘        │
          │                │                │
    ┌─────┴─────┐    manual_clear           │
    │           │          │                │
soft_delete  archive       │                │
    │           │          ▼                │
    ▼           ▼   ┌──────────────┐        │
┌──────────┐ ┌──────────────┐              │
│ Deleted  │ │  Archived    │              │
└──────┬───┘ └──────┬───────┘              │
       │            │                       │
    restore     unarchive                   │
       │            │                       │
       └────────────┴───────────────────────┘
                    │
                    ▼
             ┌──────────────┐
             │    Active    │
             └──────────────┘
```

### 6.2 Bucket Status State Machine

```
        ┌──────────────┐
        │    Active    │◄───────────────┐
        └──────┬───────┘                │
               │                        │
     ┌─────────┼─────────┐              │
     │ lock    │ archive │              │
     ▼         │         ▼              │
┌──────────┐   │   ┌──────────────┐     │
│ Readonly │   │   │   Archived   │     │
└──────┬───┘   │   └──────┬───────┘     │
       │       │          │             │
    lock       │      restore           │
       │       │          │             │
       ▼       │          │             │
┌──────────┐   │          │             │
│  Locked  │───┼──────────┴─────────────┘
└──────────┘   │         unlock
               │
            readonly
               │
               ▼
        ┌──────────┐
        │ Readonly │
        └──────────┘
```

### 6.3 Share State Machine

```
┌──────────────┐
│   Created    │
└──────┬───────┘
       │ activate
       ▼
┌──────────────┐
│    Active    │◄─────────────────────────┐
└──────┬───────┘                          │
       │                                  │
       ├──────────────┬──────────────┐    │
       │ expire       │ max_downloads│    │
       │              │ reached      │    │
       ▼              ▼              │    │
┌──────────────┐ ┌──────────────┐   │    │
│   Expired    │ │  Exhausted   │   │    │
└──────────────┘ └──────────────┘   │    │
                                    │    │
                              revoke│    │extend
                                    │    │
                                    ▼    │
                             ┌──────────────┐
                             │   Revoked    │
                             └──────────────┘
```

---

## 7. Domain Services

### 7.1 StorageService

**Location**: `libs/modules/bucket/src/domain/services/storage_service.rs`

**Description**: Handles physical file storage operations.

```rust
pub struct StorageService {
    root_path: PathBuf,
    buckets_dir: PathBuf,
    trash_dir: PathBuf,
    thumbnails_dir: PathBuf,
}

impl StorageService {
    /// Store file content
    pub async fn store_file(&self, bucket: &str, path: &str, content: &[u8])
        -> Result<String, StorageError>;

    /// Read file content
    pub async fn read_file(&self, storage_key: &str)
        -> Result<Vec<u8>, StorageError>;

    /// Delete file
    pub async fn delete_file(&self, storage_key: &str)
        -> Result<(), StorageError>;

    /// Move file to trash
    pub async fn move_to_trash(&self, storage_key: &str, file_id: Uuid)
        -> Result<String, StorageError>;

    /// Restore from trash
    pub async fn restore_from_trash(&self, file_id: Uuid, original_key: &str)
        -> Result<(), StorageError>;

    /// Store thumbnail
    pub async fn store_thumbnail(&self, file_id: Uuid, content: &[u8])
        -> Result<String, StorageError>;

    /// Generate unique storage key
    pub fn generate_storage_key(&self, bucket: &str, path: &str) -> String;
}
```

---

### 7.2 VirusScannerService

**Location**: `libs/modules/bucket/src/domain/services/virus_scanner.rs`

**Description**: Scans files for viruses and malware.

```rust
pub struct VirusScanResult {
    pub threat_level: ThreatLevel,
    pub threats: Vec<String>,
    pub scan_details: serde_json::Value,
}

pub struct VirusScannerService {
    blocked_extensions: Vec<String>,
}

impl VirusScannerService {
    /// Scan file content
    pub fn scan(&self, content: &[u8], filename: &str) -> VirusScanResult;

    /// Check if scan result should block file
    pub fn should_block(&self, result: &VirusScanResult) -> bool;

    /// Check if scan result should quarantine file
    pub fn should_quarantine(&self, result: &VirusScanResult) -> bool;
}
```

**Scanning Rules**:
1. **Extension blocking**: exe, bat, scr, dll, sys, vbs, ps1, cmd, msi, com
2. **Magic byte detection**: MZ (Windows), ELF (Linux), Mach-O (macOS)
3. **Script detection**: JavaScript, VBScript embedded in documents
4. **Archive scanning**: ZIP, RAR contents scanned recursively

---

### 7.3 ImageCompressorService

**Location**: `libs/modules/bucket/src/domain/services/image_compressor.rs`

**Description**: Compresses images and generates thumbnails.

```rust
pub struct CompressionResult {
    pub content: Vec<u8>,
    pub original_size: u64,
    pub compressed_size: u64,
    pub was_compressed: bool,
    pub algorithm: Option<String>,
}

pub struct ImageCompressorService {
    quality: u8,                     // Default: 85
    min_size_for_compression: u64,   // Default: 100KB
}

impl ImageCompressorService {
    /// Compress image
    pub fn compress(&self, content: &[u8]) -> Result<CompressionResult, ImageCompressionError>;

    /// Generate thumbnail
    pub fn generate_thumbnail(&self, content: &[u8], size: u32)
        -> Result<Vec<u8>, ImageCompressionError>;

    /// Check if content is an image
    pub fn is_image(&self, content: &[u8]) -> bool;

    /// Get image dimensions
    pub fn get_dimensions(&self, content: &[u8])
        -> Result<(u32, u32), ImageCompressionError>;
}
```

**Compression Settings**:
| Format | Quality | Algorithm |
|--------|---------|-----------|
| JPEG | 85 | MozJPEG |
| PNG | Lossless | oxipng |
| WebP | 80 | libwebp |
| GIF | - | gifsicle |

---

### 7.4 FileUploadService

**Location**: `libs/modules/bucket/src/domain/services/file_upload_service.rs`

**Description**: Orchestrates the complete file upload workflow.

```rust
pub struct FileUploadService {
    storage: Arc<StorageService>,
    scanner: Arc<VirusScannerService>,
    compressor: Arc<ImageCompressorService>,
    auto_compress_images: bool,
    thumbnail_size: u32,
}

pub struct UploadRequest {
    pub bucket_id: Uuid,
    pub owner_id: Uuid,
    pub path: String,
    pub filename: String,
    pub content: Vec<u8>,
    pub mime_type: String,
    pub metadata: Option<serde_json::Value>,
}

pub struct UploadResult {
    pub file: StoredFile,
    pub was_compressed: bool,
    pub compression_ratio: Option<f64>,
    pub scan_result: VirusScanResult,
}

impl FileUploadService {
    /// Upload file with full workflow
    pub async fn upload(
        &self,
        request: UploadRequest,
        bucket: &Bucket,
        quota: &mut UserQuota,
    ) -> Result<UploadResult, UploadError>;

    /// Upload new version of existing file
    pub async fn upload_version(
        &self,
        request: UploadRequest,
        bucket: &Bucket,
        quota: &mut UserQuota,
        previous_file: &StoredFile,
    ) -> Result<UploadResult, UploadError>;
}
```

**Upload Workflow**:
1. Validate bucket restrictions
2. Check user quota
3. Scan for viruses
4. Compress if image and enabled
5. Store file
6. Generate thumbnail if image
7. Create entity
8. Update quota
9. Return result (or quarantine)

---

### 7.5 AccessLoggerService

**Location**: `libs/modules/bucket/src/domain/services/access_logger.rs`

**Description**: Creates access log entries for audit trail.

```rust
pub struct AccessLoggerService;

impl AccessLoggerService {
    /// Log download action
    pub fn log_download(
        &self,
        file_id: Uuid,
        user_id: Option<Uuid>,
        share_id: Option<Uuid>,
        ip_address: Option<String>,
        user_agent: Option<String>,
    ) -> AccessLog;

    /// Log view action
    pub fn log_view(
        &self,
        file_id: Uuid,
        user_id: Option<Uuid>,
        share_id: Option<Uuid>,
    ) -> AccessLog;

    /// Log upload action
    pub fn log_upload(
        &self,
        file_id: Uuid,
        user_id: Uuid,
        size_bytes: i64,
    ) -> AccessLog;

    /// Log share action
    pub fn log_share(
        &self,
        file_id: Uuid,
        share_id: Uuid,
        user_id: Uuid,
    ) -> AccessLog;
}
```

---

## 8. Use Cases (CQRS)

### 8.1 Commands (Write Operations)

#### File Commands

```rust
// Upload file
pub struct UploadFileCommand {
    pub bucket_id: Uuid,
    pub owner_id: Uuid,
    pub path: String,
    pub filename: String,
    pub content: Vec<u8>,
    pub mime_type: String,
    pub metadata: Option<serde_json::Value>,
}

// Delete file
pub struct DeleteFileCommand {
    pub file_id: Uuid,
    pub deleted_by: Uuid,
    pub hard_delete: bool,
}

// Restore file
pub struct RestoreFileCommand {
    pub file_id: Uuid,
    pub restored_by: Uuid,
}

// Move file
pub struct MoveFileCommand {
    pub file_id: Uuid,
    pub target_bucket_id: Uuid,
    pub target_path: String,
    pub moved_by: Uuid,
}

// Copy file
pub struct CopyFileCommand {
    pub file_id: Uuid,
    pub target_bucket_id: Uuid,
    pub target_path: String,
    pub copied_by: Uuid,
}

// Rename file
pub struct RenameFileCommand {
    pub file_id: Uuid,
    pub new_name: String,
    pub renamed_by: Uuid,
}
```

#### Bucket Commands

```rust
// Create bucket
pub struct CreateBucketCommand {
    pub name: String,
    pub slug: String,
    pub owner_id: Uuid,
    pub bucket_type: BucketType,
    pub storage_backend: StorageBackend,
    pub max_file_size: Option<i64>,
    pub allowed_mime_types: Vec<String>,
}

// Update bucket
pub struct UpdateBucketCommand {
    pub bucket_id: Uuid,
    pub name: Option<String>,
    pub description: Option<String>,
    pub max_file_size: Option<i64>,
    pub allowed_mime_types: Option<Vec<String>>,
}

// Lock bucket
pub struct LockBucketCommand {
    pub bucket_id: Uuid,
    pub locked_by: Uuid,
}

// Archive bucket
pub struct ArchiveBucketCommand {
    pub bucket_id: Uuid,
    pub archived_by: Uuid,
}
```

#### Share Commands

```rust
// Create share
pub struct CreateShareCommand {
    pub file_id: Uuid,
    pub owner_id: Uuid,
    pub share_type: ShareType,
    pub permission: SharePermission,
    pub password: Option<String>,
    pub shared_with: Vec<Uuid>,
    pub expires_at: Option<DateTime<Utc>>,
    pub max_downloads: Option<i32>,
}

// Revoke share
pub struct RevokeShareCommand {
    pub share_id: Uuid,
    pub revoked_by: Uuid,
    pub reason: Option<String>,
}

// Update share
pub struct UpdateShareCommand {
    pub share_id: Uuid,
    pub permission: Option<SharePermission>,
    pub expires_at: Option<DateTime<Utc>>,
    pub max_downloads: Option<i32>,
}
```

### 8.2 Queries (Read Operations)

#### File Queries

```rust
// Get file by ID
pub struct GetFileByIdQuery {
    pub file_id: Uuid,
}

// List files in bucket
pub struct ListFilesQuery {
    pub bucket_id: Uuid,
    pub path: Option<String>,
    pub status: Option<FileStatus>,
    pub mime_type_filter: Option<String>,
    pub page: i32,
    pub limit: i32,
    pub sort_by: String,
    pub sort_order: SortOrder,
}

// Search files
pub struct SearchFilesQuery {
    pub owner_id: Uuid,
    pub query: String,
    pub bucket_id: Option<Uuid>,
    pub mime_type: Option<String>,
    pub min_size: Option<i64>,
    pub max_size: Option<i64>,
    pub page: i32,
    pub limit: i32,
}

// Get file versions
pub struct GetFileVersionsQuery {
    pub file_id: Uuid,
    pub include_deleted: bool,
}
```

#### Bucket Queries

```rust
// Get bucket by ID
pub struct GetBucketByIdQuery {
    pub bucket_id: Uuid,
}

// Get bucket by slug
pub struct GetBucketBySlugQuery {
    pub slug: String,
}

// List user buckets
pub struct ListUserBucketsQuery {
    pub user_id: Uuid,
    pub bucket_type: Option<BucketType>,
    pub status: Option<BucketStatus>,
    pub page: i32,
    pub limit: i32,
}

// Get bucket statistics
pub struct GetBucketStatsQuery {
    pub bucket_id: Uuid,
}
```

#### Quota Queries

```rust
// Get user quota
pub struct GetUserQuotaQuery {
    pub user_id: Uuid,
}

// Get quota usage history
pub struct GetQuotaUsageHistoryQuery {
    pub user_id: Uuid,
    pub start_date: DateTime<Utc>,
    pub end_date: DateTime<Utc>,
    pub granularity: Granularity,  // Day, Week, Month
}
```

---

## 9. Repositories

### 9.1 StoredFileRepository

```rust
#[async_trait]
pub trait StoredFileRepository {
    async fn save(&self, file: &StoredFile) -> Result<StoredFile, RepositoryError>;
    async fn find_by_id(&self, id: Uuid) -> Result<Option<StoredFile>, RepositoryError>;
    async fn find_by_bucket(&self, bucket_id: Uuid, path: Option<&str>, page: i32, limit: i32)
        -> Result<(Vec<StoredFile>, i64), RepositoryError>;
    async fn find_by_owner(&self, owner_id: Uuid, page: i32, limit: i32)
        -> Result<(Vec<StoredFile>, i64), RepositoryError>;
    async fn search(&self, query: &SearchFilesQuery)
        -> Result<(Vec<StoredFile>, i64), RepositoryError>;
    async fn update(&self, file: &StoredFile) -> Result<StoredFile, RepositoryError>;
    async fn delete(&self, id: Uuid) -> Result<(), RepositoryError>;
    async fn count_by_bucket(&self, bucket_id: Uuid) -> Result<i64, RepositoryError>;
    async fn sum_size_by_bucket(&self, bucket_id: Uuid) -> Result<i64, RepositoryError>;
}
```

### 9.2 BucketRepository

```rust
#[async_trait]
pub trait BucketRepository {
    async fn save(&self, bucket: &Bucket) -> Result<Bucket, RepositoryError>;
    async fn find_by_id(&self, id: Uuid) -> Result<Option<Bucket>, RepositoryError>;
    async fn find_by_slug(&self, slug: &str) -> Result<Option<Bucket>, RepositoryError>;
    async fn find_by_owner(&self, owner_id: Uuid, page: i32, limit: i32)
        -> Result<(Vec<Bucket>, i64), RepositoryError>;
    async fn update(&self, bucket: &Bucket) -> Result<Bucket, RepositoryError>;
    async fn delete(&self, id: Uuid) -> Result<(), RepositoryError>;
    async fn slug_exists(&self, slug: &str) -> Result<bool, RepositoryError>;
}
```

### 9.3 UserQuotaRepository

```rust
#[async_trait]
pub trait UserQuotaRepository {
    async fn save(&self, quota: &UserQuota) -> Result<UserQuota, RepositoryError>;
    async fn find_by_user(&self, user_id: Uuid) -> Result<Option<UserQuota>, RepositoryError>;
    async fn update(&self, quota: &UserQuota) -> Result<UserQuota, RepositoryError>;
    async fn find_over_threshold(&self, threshold_percent: f64)
        -> Result<Vec<UserQuota>, RepositoryError>;
}
```

### 9.4 FileShareRepository

```rust
#[async_trait]
pub trait FileShareRepository {
    async fn save(&self, share: &FileShare) -> Result<FileShare, RepositoryError>;
    async fn find_by_id(&self, id: Uuid) -> Result<Option<FileShare>, RepositoryError>;
    async fn find_by_token(&self, token: &str) -> Result<Option<FileShare>, RepositoryError>;
    async fn find_by_file(&self, file_id: Uuid) -> Result<Vec<FileShare>, RepositoryError>;
    async fn find_active_by_file(&self, file_id: Uuid) -> Result<Vec<FileShare>, RepositoryError>;
    async fn update(&self, share: &FileShare) -> Result<FileShare, RepositoryError>;
    async fn delete(&self, id: Uuid) -> Result<(), RepositoryError>;
    async fn revoke_all_for_file(&self, file_id: Uuid, revoked_by: Uuid) -> Result<i64, RepositoryError>;
}
```

### 9.5 AccessLogRepository

```rust
#[async_trait]
pub trait AccessLogRepository {
    async fn save(&self, log: &AccessLog) -> Result<AccessLog, RepositoryError>;
    async fn find_by_file(&self, file_id: Uuid, page: i32, limit: i32)
        -> Result<(Vec<AccessLog>, i64), RepositoryError>;
    async fn find_by_user(&self, user_id: Uuid, page: i32, limit: i32)
        -> Result<(Vec<AccessLog>, i64), RepositoryError>;
    async fn find_by_action(&self, action: AccessAction, start: DateTime<Utc>, end: DateTime<Utc>)
        -> Result<Vec<AccessLog>, RepositoryError>;
    async fn count_downloads_by_file(&self, file_id: Uuid) -> Result<i64, RepositoryError>;
}
```

---

## 10. Specifications

### 10.1 File Upload Specification

```rust
pub struct FileUploadSpecification {
    pub max_file_size: i64,
    pub allowed_mime_types: Vec<String>,
    pub require_virus_scan: bool,
    pub auto_compress_images: bool,
    pub generate_thumbnails: bool,
}

impl FileUploadSpecification {
    pub fn is_satisfied_by(&self, file: &UploadRequest, bucket: &Bucket, quota: &UserQuota) -> bool {
        self.check_size(file.content.len() as i64)
            && self.check_mime_type(&file.mime_type)
            && bucket.can_upload(file.content.len() as i64, &file.mime_type).is_ok()
            && quota.has_space_for(file.content.len() as i64)
    }
}
```

### 10.2 File Access Specification

```rust
pub struct FileAccessSpecification {
    pub require_active_status: bool,
    pub require_safe_scan: bool,
    pub check_owner: bool,
}

impl FileAccessSpecification {
    pub fn can_access(&self, file: &StoredFile, user_id: Option<Uuid>) -> bool {
        if self.require_active_status && file.status != FileStatus::Active {
            return false;
        }
        if self.require_safe_scan && !file.is_safe() {
            return false;
        }
        if self.check_owner {
            if let Some(uid) = user_id {
                return file.owner_id == uid;
            }
            return false;
        }
        true
    }
}
```

### 10.3 Share Validity Specification

```rust
pub struct ShareValiditySpecification {
    pub check_expiry: bool,
    pub check_downloads: bool,
    pub check_revoked: bool,
}

impl ShareValiditySpecification {
    pub fn is_valid(&self, share: &FileShare) -> bool {
        if self.check_revoked && !share.is_active {
            return false;
        }
        if self.check_expiry && share.is_expired() {
            return false;
        }
        if self.check_downloads && !share.has_downloads_remaining() {
            return false;
        }
        true
    }
}
```

---

## 11. Workflows

### 11.1 File Upload Workflow

**Trigger**: `UploadFileCommand`

**Steps**:
1. **Validate Request**: Check file size, MIME type
2. **Check Quota**: Verify user has sufficient storage
3. **Scan File**: Run virus scanner
4. **Block if Threat**: Return error if critical/high threat
5. **Compress Image**: If applicable and enabled
6. **Store File**: Write to storage backend
7. **Generate Thumbnail**: If image
8. **Create Record**: Save StoredFile entity
9. **Update Quota**: Increment used bytes
10. **Emit Event**: FileUploadedEvent

**Compensations**:
- On quota failure: Delete stored file
- On scan failure: Mark as quarantined
- On storage failure: Return error, no quota update

### 11.2 File Version Cleanup Workflow

**Trigger**: Scheduled (daily at 2 AM)

**Steps**:
1. **Find Expired Versions**: Query versions past retention
2. **For Each Version**:
   - Delete from storage
   - Delete version record
   - Update bucket stats
3. **Log Cleanup Stats**: Record deleted count/size
4. **Emit Event**: VersionCleanupCompletedEvent

### 11.3 Quota Warning Workflow

**Trigger**: `FileUploadedEvent`

**Steps**:
1. **Get User Quota**: Load current quota
2. **Check Threshold**: If usage > warning_threshold
3. **Check Last Warning**: If > 24 hours since last warning
4. **Send Warning**: Email/notification to user
5. **Update Last Warning**: Set last_warning_sent_at
6. **Emit Event**: QuotaWarningEvent

---

## 12. API Endpoints

### 12.1 Layer 1: Backbone Generic CRUD

**All 7 entities get standard endpoints**:

```
GET    /api/v1/{collection}              - List (paginated, filtered, sorted)
POST   /api/v1/{collection}              - Create
GET    /api/v1/{collection}/:id          - Get by ID
PUT    /api/v1/{collection}/:id          - Full update
PATCH  /api/v1/{collection}/:id          - Partial update
DELETE /api/v1/{collection}/:id          - Soft delete
POST   /api/v1/{collection}/bulk         - Bulk create
POST   /api/v1/{collection}/upcreate     - Upsert
GET    /api/v1/{collection}/trash        - List deleted
POST   /api/v1/{collection}/:id/restore  - Restore
DELETE /api/v1/{collection}/empty        - Empty trash (hard delete)
```

**Collections**:
1. `files`
2. `buckets`
3. `user-quotas`
4. `file-shares`
5. `file-versions`
6. `thumbnails`
7. `access-logs` (read-only: GET list, GET :id only)

### 12.2 Layer 2: Domain-Specific Endpoints

#### File Operations

```
POST   /api/v1/files/upload              - Upload file (multipart)
POST   /api/v1/files/upload-chunk        - Chunked upload
GET    /api/v1/files/:id/download        - Download file
GET    /api/v1/files/:id/preview         - Preview file (images)
GET    /api/v1/files/:id/thumbnail/:size - Get thumbnail
POST   /api/v1/files/:id/move            - Move file
POST   /api/v1/files/:id/copy            - Copy file
POST   /api/v1/files/:id/rename          - Rename file
GET    /api/v1/files/:id/versions        - Get file versions
POST   /api/v1/files/:id/restore-version - Restore specific version
POST   /api/v1/files/search              - Search files
GET    /api/v1/files/:id/access-logs     - Get access history
```

#### Bucket Operations

```
GET    /api/v1/buckets/:slug             - Get bucket by slug
GET    /api/v1/buckets/:id/files         - List bucket files
GET    /api/v1/buckets/:id/stats         - Get bucket statistics
POST   /api/v1/buckets/:id/lock          - Lock bucket
POST   /api/v1/buckets/:id/unlock        - Unlock bucket
POST   /api/v1/buckets/:id/archive       - Archive bucket
POST   /api/v1/buckets/:id/restore       - Restore bucket
```

#### Share Operations

```
POST   /api/v1/files/:id/share           - Create share
GET    /api/v1/shares/:token             - Access shared file (public)
GET    /api/v1/shares/:token/download    - Download via share
POST   /api/v1/shares/:token/verify      - Verify password
GET    /api/v1/files/:id/shares          - List file shares
DELETE /api/v1/shares/:id                - Revoke share
```

#### Quota Operations

```
GET    /api/v1/users/:id/quota           - Get user quota
GET    /api/v1/users/:id/quota/usage     - Get usage history
GET    /api/v1/users/:id/storage-stats   - Get storage breakdown
```

#### Admin Operations

```
POST   /api/v1/admin/files/:id/scan      - Re-scan file
POST   /api/v1/admin/files/:id/clear     - Clear quarantine
GET    /api/v1/admin/quarantined         - List quarantined files
POST   /api/v1/admin/cleanup             - Trigger cleanup job
GET    /api/v1/admin/stats               - System statistics
```

---

## 13. Database Schema

### 13.1 PostgreSQL Tables

#### stored_files Table

```sql
CREATE TABLE stored_files (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    bucket_id UUID NOT NULL REFERENCES buckets(id),
    owner_id UUID NOT NULL,

    path VARCHAR(1000) NOT NULL,
    original_name VARCHAR(255) NOT NULL,
    size_bytes BIGINT NOT NULL CHECK (size_bytes > 0),
    mime_type VARCHAR(255) NOT NULL,
    checksum VARCHAR(64),
    storage_key VARCHAR(500) NOT NULL,

    is_compressed BOOLEAN NOT NULL DEFAULT FALSE,
    original_size BIGINT,
    compression_algorithm VARCHAR(50),

    is_scanned BOOLEAN NOT NULL DEFAULT FALSE,
    scan_result JSONB,
    threat_level VARCHAR(20) CHECK (threat_level IN ('safe', 'low', 'medium', 'high', 'critical')),

    has_thumbnail BOOLEAN NOT NULL DEFAULT FALSE,
    thumbnail_path VARCHAR(500),

    status VARCHAR(20) NOT NULL DEFAULT 'uploading'
        CHECK (status IN ('uploading', 'processing', 'active', 'quarantined', 'deleted', 'archived')),

    version INTEGER NOT NULL DEFAULT 1,
    previous_version_id UUID REFERENCES stored_files(id),

    download_count INTEGER NOT NULL DEFAULT 0,
    last_accessed_at TIMESTAMPTZ,

    metadata JSONB NOT NULL DEFAULT '{}',

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT stored_files_storage_key_unique UNIQUE (storage_key)
);

CREATE INDEX idx_stored_files_bucket ON stored_files(bucket_id, path);
CREATE INDEX idx_stored_files_owner ON stored_files(owner_id);
CREATE INDEX idx_stored_files_status ON stored_files(status);
CREATE INDEX idx_stored_files_mime_type ON stored_files(mime_type);
CREATE INDEX idx_stored_files_created_at ON stored_files(created_at DESC);
CREATE INDEX idx_stored_files_name_search ON stored_files USING gin(original_name gin_trgm_ops);
```

#### buckets Table

```sql
CREATE TABLE buckets (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    owner_id UUID NOT NULL,

    name VARCHAR(100) NOT NULL,
    slug VARCHAR(100) NOT NULL,
    description TEXT,

    bucket_type VARCHAR(20) NOT NULL DEFAULT 'user'
        CHECK (bucket_type IN ('user', 'team', 'project', 'system', 'public')),
    status VARCHAR(20) NOT NULL DEFAULT 'active'
        CHECK (status IN ('active', 'readonly', 'locked', 'archived')),

    storage_backend VARCHAR(20) NOT NULL DEFAULT 'local'
        CHECK (storage_backend IN ('local', 's3', 'gcs', 'azure_blob', 'minio')),
    root_path VARCHAR(500) NOT NULL,

    file_count INTEGER NOT NULL DEFAULT 0,
    total_size_bytes BIGINT NOT NULL DEFAULT 0,

    max_file_size BIGINT,
    allowed_mime_types TEXT[] NOT NULL DEFAULT '{}',
    auto_delete_after_days INTEGER,

    metadata JSONB NOT NULL DEFAULT '{}',

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT buckets_slug_unique UNIQUE (slug),
    CONSTRAINT buckets_file_count_positive CHECK (file_count >= 0),
    CONSTRAINT buckets_size_positive CHECK (total_size_bytes >= 0)
);

CREATE INDEX idx_buckets_owner ON buckets(owner_id);
CREATE INDEX idx_buckets_status ON buckets(status);
CREATE INDEX idx_buckets_type ON buckets(bucket_type);
```

#### user_quotas Table

```sql
CREATE TABLE user_quotas (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL,

    limit_bytes BIGINT NOT NULL DEFAULT 1073741824,  -- 1GB
    max_file_size BIGINT,
    max_file_count INTEGER,

    used_bytes BIGINT NOT NULL DEFAULT 0,
    file_count INTEGER NOT NULL DEFAULT 0,

    tier VARCHAR(20) NOT NULL DEFAULT 'free',

    warning_threshold_percent INTEGER NOT NULL DEFAULT 80,
    last_warning_sent_at TIMESTAMPTZ,

    peak_usage_bytes BIGINT NOT NULL DEFAULT 0,
    peak_usage_at TIMESTAMPTZ,

    metadata JSONB NOT NULL DEFAULT '{}',

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT user_quotas_user_unique UNIQUE (user_id),
    CONSTRAINT user_quotas_usage_valid CHECK (used_bytes <= limit_bytes),
    CONSTRAINT user_quotas_threshold_valid CHECK (warning_threshold_percent BETWEEN 0 AND 100)
);

CREATE INDEX idx_user_quotas_tier ON user_quotas(tier);
CREATE INDEX idx_user_quotas_usage_percent ON user_quotas((used_bytes::float / limit_bytes::float));
```

#### file_shares Table

```sql
CREATE TABLE file_shares (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES stored_files(id) ON DELETE CASCADE,
    owner_id UUID NOT NULL,

    token VARCHAR(64) NOT NULL,

    share_type VARCHAR(20) NOT NULL DEFAULT 'link'
        CHECK (share_type IN ('link', 'user', 'password')),

    shared_with UUID[] NOT NULL DEFAULT '{}',

    password_hash VARCHAR(255),

    permission VARCHAR(20) NOT NULL DEFAULT 'view'
        CHECK (permission IN ('view', 'download', 'edit')),

    expires_at TIMESTAMPTZ,
    max_downloads INTEGER,
    download_count INTEGER NOT NULL DEFAULT 0,

    is_active BOOLEAN NOT NULL DEFAULT TRUE,

    revoked_at TIMESTAMPTZ,
    revoked_by UUID,

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT file_shares_token_unique UNIQUE (token),
    CONSTRAINT file_shares_download_valid CHECK (
        max_downloads IS NULL OR download_count <= max_downloads
    ),
    CONSTRAINT file_shares_revoked_valid CHECK (
        (is_active = TRUE AND revoked_at IS NULL) OR
        (is_active = FALSE AND revoked_at IS NOT NULL)
    )
);

CREATE INDEX idx_file_shares_file ON file_shares(file_id);
CREATE INDEX idx_file_shares_owner ON file_shares(owner_id);
CREATE INDEX idx_file_shares_active ON file_shares(is_active, expires_at);
```

#### file_versions Table

```sql
CREATE TABLE file_versions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES stored_files(id) ON DELETE CASCADE,

    version_number INTEGER NOT NULL,
    is_current BOOLEAN NOT NULL DEFAULT FALSE,

    storage_key VARCHAR(500) NOT NULL,
    storage_backend VARCHAR(20) NOT NULL,
    size_bytes BIGINT NOT NULL,
    checksum VARCHAR(64),

    mime_type VARCHAR(255) NOT NULL,
    original_name VARCHAR(255) NOT NULL,
    metadata JSONB NOT NULL DEFAULT '{}',

    status VARCHAR(20) NOT NULL DEFAULT 'active'
        CHECK (status IN ('active', 'deleted', 'archived')),

    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    expires_at TIMESTAMPTZ,
    created_by UUID NOT NULL,

    CONSTRAINT file_versions_storage_key_unique UNIQUE (storage_key),
    CONSTRAINT file_versions_number_positive CHECK (version_number >= 1)
);

CREATE INDEX idx_file_versions_file ON file_versions(file_id, version_number DESC);
CREATE INDEX idx_file_versions_current ON file_versions(file_id) WHERE is_current = TRUE;
CREATE INDEX idx_file_versions_expires ON file_versions(expires_at) WHERE expires_at IS NOT NULL;
```

#### thumbnails Table

```sql
CREATE TABLE thumbnails (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL REFERENCES stored_files(id) ON DELETE CASCADE,

    width INTEGER NOT NULL CHECK (width > 0),
    height INTEGER NOT NULL CHECK (height > 0),

    storage_key VARCHAR(500) NOT NULL,
    storage_backend VARCHAR(20) NOT NULL,
    size_bytes BIGINT NOT NULL CHECK (size_bytes > 0),
    mime_type VARCHAR(100) NOT NULL,
    format VARCHAR(20) NOT NULL,

    generated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    generation_time_ms INTEGER NOT NULL,

    cdn_url VARCHAR(500) NOT NULL,
    cache_until TIMESTAMPTZ NOT NULL,

    quality INTEGER NOT NULL CHECK (quality BETWEEN 1 AND 100),

    source_version INTEGER NOT NULL,
    is_stale BOOLEAN NOT NULL DEFAULT FALSE,

    metadata JSONB NOT NULL DEFAULT '{}',

    CONSTRAINT thumbnails_storage_key_unique UNIQUE (storage_key),
    CONSTRAINT thumbnails_file_size_unique UNIQUE (file_id, width, height)
);

CREATE INDEX idx_thumbnails_file ON thumbnails(file_id);
CREATE INDEX idx_thumbnails_stale ON thumbnails(is_stale) WHERE is_stale = TRUE;
CREATE INDEX idx_thumbnails_cache ON thumbnails(cache_until);
```

#### access_logs Table

```sql
CREATE TABLE access_logs (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    file_id UUID NOT NULL,

    user_id UUID,
    share_id UUID,

    action VARCHAR(20) NOT NULL
        CHECK (action IN ('download', 'view', 'preview', 'share', 'share_access',
                          'upload', 'delete', 'rename', 'move', 'copy', 'restore', 'metadata_update')),

    ip_address INET,
    user_agent VARCHAR(500),
    referrer VARCHAR(500),
    location JSONB,

    success BOOLEAN NOT NULL DEFAULT TRUE,
    error_code VARCHAR(50),
    error_message TEXT,

    duration_ms INTEGER,
    bytes_transferred BIGINT,

    accessed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    metadata JSONB NOT NULL DEFAULT '{}'
);

CREATE INDEX idx_access_logs_file ON access_logs(file_id, accessed_at DESC);
CREATE INDEX idx_access_logs_user ON access_logs(user_id, accessed_at DESC);
CREATE INDEX idx_access_logs_action ON access_logs(action, accessed_at DESC);
CREATE INDEX idx_access_logs_time ON access_logs(accessed_at DESC);

-- Partitioning for large-scale deployments
-- CREATE TABLE access_logs_y2025m01 PARTITION OF access_logs
--     FOR VALUES FROM ('2025-01-01') TO ('2025-02-01');
```

---

## 14. Security Model

### 14.1 File Security Layers

1. **Upload Security**
   - MIME type validation
   - File size limits
   - Extension blocking
   - Virus scanning
   - Content inspection

2. **Access Security**
   - Owner verification
   - Share token validation
   - Password protection
   - Expiration checking
   - Download limits

3. **Storage Security**
   - Encrypted at rest
   - Secure key generation
   - Access logging
   - Quarantine isolation

### 14.2 Blocked File Types

| Category | Extensions |
|----------|------------|
| Executables | exe, bat, cmd, com, msi, scr |
| Scripts | vbs, vbe, js, jse, ws, wsf, ps1 |
| System | dll, sys, drv, ocx |
| Archives (risky) | cab, hta, msc, inf |

### 14.3 Magic Byte Detection

| Signature | Type | Action |
|-----------|------|--------|
| `MZ` | Windows PE | Block |
| `\x7fELF` | Linux ELF | Block |
| `\xCA\xFE\xBA\xBE` | macOS Mach-O | Block |
| `PK` | ZIP (scan contents) | Scan |
| `Rar!` | RAR (scan contents) | Scan |

### 14.4 Threat Response Matrix

| Threat Level | Action | User Notification |
|--------------|--------|-------------------|
| Safe | Allow | None |
| Low | Allow with warning | Warning badge |
| Medium | Quarantine | Email + in-app alert |
| High | Block | Email + admin alert |
| Critical | Block + alert admin | Email + SMS to admin |

---

## 15. Implementation Checklist

### Phase 1: Core Entities
- [x] StoredFile entity with custom methods
- [x] Bucket entity with custom methods
- [x] UserQuota entity with custom methods
- [x] FileShare entity with custom methods
- [x] FileVersion entity with custom methods
- [x] Thumbnail entity with custom methods
- [x] AccessLog entity with custom methods
- [x] All enums (10 total)

### Phase 2: Domain Services
- [x] StorageService (local filesystem)
- [x] VirusScannerService
- [x] ImageCompressorService
- [x] FileUploadService (orchestration)
- [x] AccessLoggerService

### Phase 3: Database
- [ ] PostgreSQL migrations
- [ ] Indexes and constraints
- [ ] Repository implementations

### Phase 4: API Layer
- [ ] HTTP handlers
- [ ] Request/response validation
- [ ] Error handling
- [ ] Authentication middleware

### Phase 5: Integration
- [ ] S3/cloud storage backends
- [ ] CDN integration
- [ ] Email notifications
- [ ] Admin dashboard

---

## 16. Conclusion

This technical domain documentation provides **complete specifications** for implementing the Bucket File Storage System. All 7 entities, 10 enums, domain services, and workflows are fully documented with:

- **Entity definitions** with business rules and invariants
- **State machines** for file and bucket lifecycle
- **Domain services** for storage, scanning, and compression
- **PostgreSQL schemas** with constraints and indexes
- **API endpoint specifications** (Backbone + Domain-specific)
- **Security model** for file protection

**Document Version**: 1.0 (Complete)
**Last Updated**: 2025-01-20
**Status**: Ready for implementation

---