|
| 1 | +# FileClassificationSolutions 项目结构 |
| 2 | + |
| 3 | +这是一个基于 Rust 的文件分类解决方案,采用模块化架构设计,包含核心库、命令行界面和 Web API 等多个组件。 |
| 4 | + |
| 5 | +## 项目整体结构 |
| 6 | + |
| 7 | +``` |
| 8 | +FileClassificationSolutions/ |
| 9 | +├── .github/ # GitHub 相关配置 |
| 10 | +│ └── workflows/ # CI/CD 工作流配置 |
| 11 | +├── file_classification_cli/ # 命令行界面应用 |
| 12 | +│ ├── src/bin/ # 各个独立的命令行工具 |
| 13 | +│ └── Cargo.toml # CLI 包配置 |
| 14 | +├── file_classification_core/ # 核心库 |
| 15 | +│ ├── src/ |
| 16 | +│ │ ├── internal/ # 数据访问层 |
| 17 | +│ │ ├── model/ # 数据模型和数据库模式 |
| 18 | +│ │ ├── service/ # 业务逻辑层 |
| 19 | +│ │ └── utils/ # 工具函数 |
| 20 | +│ └── Cargo.toml # 核心库包配置 |
| 21 | +├── file_classification_webapi/ # Web API (旧版) |
| 22 | +│ ├── src/bin/ # 各个独立的API端点 |
| 23 | +│ └── Cargo.toml # Web API 包配置 |
| 24 | +├── file_classification_webapi2/ # Web API (新版) |
| 25 | +│ ├── src/ |
| 26 | +│ │ ├── handlers/ # 请求处理函数 |
| 27 | +│ │ └── utils/ # Web API 工具函数 |
| 28 | +│ └── Cargo.toml # Web API2 包配置 |
| 29 | +├── migrations/ # 数据库迁移脚本 |
| 30 | +└── nix/ # Nix 包管理配置 |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +## 核心模块详解 |
| 35 | + |
| 36 | +### file_classification_core (核心库) |
| 37 | + |
| 38 | +这是整个项目的业务逻辑核心,包含了数据模型、数据库访问层和业务服务。 |
| 39 | + |
| 40 | +#### 目录结构 |
| 41 | +``` |
| 42 | +file_classification_core/ |
| 43 | +├── src/ |
| 44 | +│ ├── internal/ # 数据访问层 (DAO) |
| 45 | +│ │ ├── file_group.rs # 文件组关联数据访问 |
| 46 | +│ │ ├── files.rs # 文件数据访问 |
| 47 | +│ │ ├── group_tag.rs # 组标签关联数据访问 |
| 48 | +│ │ ├── groups.rs # 组数据访问 |
| 49 | +│ │ └── tags.rs # 标签数据访问 |
| 50 | +│ ├── model/ # 数据模型 |
| 51 | +│ │ ├── models.rs # 所有数据结构定义 |
| 52 | +│ │ └── schema.rs # 数据库模式 (由 Diesel 生成) |
| 53 | +│ ├── service/ # 业务逻辑层 |
| 54 | +│ │ ├── file_group.rs # 文件组关联业务逻辑 |
| 55 | +│ │ ├── files.rs # 文件业务逻辑 |
| 56 | +│ │ ├── group_tag.rs # 组标签关联业务逻辑 |
| 57 | +│ │ ├── groups.rs # 组业务逻辑 |
| 58 | +│ │ └── tags.rs # 标签业务逻辑 |
| 59 | +│ └── utils/ # 工具函数 |
| 60 | +│ ├── database.rs # 数据库连接管理 |
| 61 | +│ └── errors.rs # 错误处理 |
| 62 | +└── Cargo.toml # 包配置文件 |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +### file_classification_cli (命令行界面) |
| 67 | + |
| 68 | +提供命令行工具来操作文件分类系统。 |
| 69 | + |
| 70 | +#### 目录结构 |
| 71 | +``` |
| 72 | +file_classification_cli/ |
| 73 | +├── src/bin/ # 各个独立的命令行工具 |
| 74 | +│ ├── create_file_group.rs # 创建文件组关联 |
| 75 | +│ ├── create_group.rs # 创建组 |
| 76 | +│ ├── create_group_tag.rs # 创建组标签关联 |
| 77 | +│ ├── create_tag.rs # 创建标签 |
| 78 | +│ ├── delete_file.rs # 删除文件 |
| 79 | +│ ├── delete_file_group.rs # 删除文件组关联 |
| 80 | +│ ├── delete_group.rs # 删除组 |
| 81 | +│ ├── delete_group_tag.rs # 删除组标签关联 |
| 82 | +│ ├── delete_tag.rs # 删除标签 |
| 83 | +│ ├── list_file_groups_by_conditions.rs # 条件查询文件组关联 |
| 84 | +│ ├── list_files.rs # 列出文件 |
| 85 | +│ ├── list_files_by_conditions.rs # 条件查询文件 |
| 86 | +│ ├── list_group_tags_by_conditions.rs # 条件查询组标签关联 |
| 87 | +│ ├── list_groups.rs # 列出组 |
| 88 | +│ ├── list_groups_by_conditions.rs # 条件查询组 |
| 89 | +│ ├── list_tags.rs # 列出标签 |
| 90 | +│ ├── list_tags_by_conditions.rs # 条件查询标签 |
| 91 | +│ ├── update_files_by_conditions.rs # 条件更新文件 |
| 92 | +│ ├── update_groups_by_conditions.rs # 条件更新组 |
| 93 | +│ └── update_tags_by_conditions.rs # 条件更新标签 |
| 94 | +└── Cargo.toml # 包配置文件 |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +### file_classification_webapi2 (Web API - 新版) |
| 99 | + |
| 100 | +基于 Actix-web 框架构建的 RESTful API 服务。 |
| 101 | + |
| 102 | +#### 目录结构 |
| 103 | +``` |
| 104 | +file_classification_webapi2/ |
| 105 | +├── src/ |
| 106 | +│ ├── handlers/ # API 请求处理函数 |
| 107 | +│ │ ├── file_groups.rs # 文件组关联 API 处理 |
| 108 | +│ │ ├── files.rs # 文件 API 处理 |
| 109 | +│ │ ├── group_tags.rs # 组标签关联 API 处理 |
| 110 | +│ │ ├── groups.rs # 组 API 处理 |
| 111 | +│ │ ├── mod.rs # 模块声明 |
| 112 | +│ │ └── tags.rs # 标签 API 处理 |
| 113 | +│ ├── utils/ # Web API 工具函数 |
| 114 | +│ │ ├── database.rs # 数据库连接池 |
| 115 | +│ │ ├── mod.rs # 模块声明 |
| 116 | +│ │ └── models.rs # API 数据传输对象 |
| 117 | +│ └── main.rs # 应用入口点 |
| 118 | +└── Cargo.toml # 包配置文件 |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | +### 数据库迁移 |
| 123 | + |
| 124 | +``` |
| 125 | +migrations/ |
| 126 | +└── 2024-10-01-193345_FileClassification/ |
| 127 | + ├── up.sql # 数据库表创建脚本 |
| 128 | + └── down.sql # 数据库表删除脚本 |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +数据库包含以下表: |
| 133 | +- `files`: 存储文件信息 |
| 134 | +- `groups`: 存储文件组信息 |
| 135 | +- `file_groups`: 文件和组的多对多关联关系 |
| 136 | +- `tags`: 存储标签信息 |
| 137 | +- `group_tags`: 组和标签的多对多关联关系 |
| 138 | + |
| 139 | +### 数据模型关系 |
| 140 | + |
| 141 | +```mermaid |
| 142 | +erDiagram |
| 143 | + files ||--o{ file_groups : has |
| 144 | + groups ||--o{ file_groups : has |
| 145 | + groups ||--o{ group_tags : has |
| 146 | + tags ||--o{ group_tags : has |
| 147 | + |
| 148 | + files { |
| 149 | + int id PK |
| 150 | + string type |
| 151 | + string path |
| 152 | + int reference_count |
| 153 | + int group_id FK |
| 154 | + } |
| 155 | + |
| 156 | + groups { |
| 157 | + int id PK |
| 158 | + string name |
| 159 | + int reference_count |
| 160 | + boolean is_primary |
| 161 | + int click_count |
| 162 | + int share_count |
| 163 | + timestamp create_time |
| 164 | + timestamp modify_time |
| 165 | + } |
| 166 | + |
| 167 | + file_groups { |
| 168 | + int file_id PK,FK |
| 169 | + int group_id PK,FK |
| 170 | + } |
| 171 | + |
| 172 | + tags { |
| 173 | + int id PK |
| 174 | + string name |
| 175 | + int reference_count |
| 176 | + } |
| 177 | + |
| 178 | + group_tags { |
| 179 | + int group_id PK,FK |
| 180 | + int tag_id PK,FK |
| 181 | + } |
| 182 | +``` |
| 183 | + |
| 184 | + |
| 185 | +## 架构特点 |
| 186 | + |
| 187 | +1. **分层架构**: 项目采用清晰的分层架构,将数据访问、业务逻辑和表示层分离 |
| 188 | +2. **模块化设计**: 不同的功能模块被组织在独立的 crate 中 |
| 189 | +3. **多种访问方式**: 提供 CLI 和 Web API 两种访问方式 |
| 190 | +4. **强类型安全**: 利用 Rust 的类型系统保证代码安全 |
| 191 | +5. **错误处理**: 统一的错误处理机制 |
| 192 | +6. **数据库抽象**: 使用 Diesel ORM 进行数据库操作 |
| 193 | +7. **可扩展性**: 易于添加新的功能模块和访问接口 |
| 194 | + |
| 195 | +这个项目结构设计支持对文件进行分类管理,通过组和标签的方式组织文件,并提供多种访问接口以适应不同使用场景。 |
0 commit comments