Skip to content
bitzorcas
EN

Reference

存储构建块

文件存储抽象 — IFileStorage 端口与 LocalFileStorage 默认实现、FileAssetService 用于上传/下载/管理,以及云存储适配器路线图。

Last updated

存储构建块提供文件存储抽象和本地文件系统实现。Files 平台模块在此基础之上构建上传/下载/管理操作。

IFileStorage 端口

public interface IFileStorage
{
Task<string> SaveAsync(
Stream stream,
string contentType,
string extension,
CancellationToken ct = default);
Task<Stream?> ReadAsync(
string path,
CancellationToken ct = default);
Task DeleteAsync(
string path,
CancellationToken ct = default);
}

LocalFileStorage

默认实现将文件保存到本地目录:

public class LocalFileStorage : IFileStorage
{
// 生成唯一路径:{basePath}/{year}/{month}/{guid}.{extension}
// 返回相对路径存储到数据库
}

配置

{
"Storage": {
"BasePath": "./storage/files"
}
}

文件命名约定

文件按日期组织以防止目录膨胀:

./storage/files/
├── 2026/
│ ├── 06/
│ │ ├── a1b2c3d4.pdf
│ │ ├── e5f6g7h8.png
│ │ └── i9j0k1l2.csv
│ └── 07/
│ └── ...

FileAssetService

Files 平台模块提供更高级别的文件管理:

操作端点描述
上传POST /api/files上传文件及元数据
下载GET /api/files/{id}按 asset ID 下载
删除DELETE /api/files/{id}软删除文件资源
列表GET /api/files分页文件列表

FileAsset 实体

public class FileAssetEntity : TenantSoftDeleteEntityBase
{
public string FileName { get; set; }
public string ContentType { get; set; }
public long FileSizeBytes { get; set; }
public string StoragePath { get; set; }
public string? Description { get; set; }
}

模块集成

文件存储与其他模块集成:

  • TicketsFileAssetTicketAttachmentAccessService 将文件资源关联到工单
  • ChatFileAssetChatAttachmentAccessService 将文件资源关联到聊天消息
  • Webhooks — Webhook 载荷可引用文件附件

路线图:云存储适配器

计划的云存储实现:

适配器目标
S3 StorageAWS S3、MinIO、兼容提供商
Azure BlobAzure Blob Storage
阿里云 OSS阿里云对象存储

所有都将实现 IFileStorage — 可通过配置切换。