UploadMapFileCommandHandler.cs
4.61 KB
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
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Application.Services;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 上传地图文件命令处理器
/// </summary>
public class UploadMapFileCommandHandler : IConsumer<UploadMapFileCommand>
{
private readonly ILogger<UploadMapFileCommandHandler> _logger;
private readonly IFileStorageService _fileStorageService;
private readonly IMapFileRepository _mapFileRepository;
private readonly IMapRepository _mapRepository;
// 允许的文件类型(地图图片文件 + PGM地图文件)
private static readonly string[] AllowedExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".webp", ".pgm" };
// 最大文件大小:50MB
private const long MaxFileSize = 50 * 1024 * 1024;
public UploadMapFileCommandHandler(
ILogger<UploadMapFileCommandHandler> logger,
IFileStorageService fileStorageService,
IMapFileRepository mapFileRepository,
IMapRepository mapRepository)
{
_logger = logger;
_fileStorageService = fileStorageService;
_mapFileRepository = mapFileRepository;
_mapRepository = mapRepository;
}
public async Task Consume(ConsumeContext<UploadMapFileCommand> context)
{
var command = context.Message;
try
{
// 验证地图ID
if (!Guid.TryParse(command.MapId, out Guid mapId))
{
await context.RespondAsync(ApiResponse.Failed("无效的地图ID格式"));
return;
}
// 验证地图是否存在
var map = await _mapRepository.GetByIdAsync(mapId, context.CancellationToken);
if (map == null)
{
await context.RespondAsync(ApiResponse.Failed($"未找到ID为 {mapId} 的地图"));
return;
}
// 验证文件
var (isValid, errorMessage) = _fileStorageService.ValidateFile(
command.FileName,
command.FileSize,
AllowedExtensions,
MaxFileSize);
if (!isValid)
{
await context.RespondAsync(ApiResponse.Failed(errorMessage));
return;
}
// 检查该地图是否已经有文件,如果有则删除旧文件
var existingMapFile = await _mapFileRepository.GetByMapIdAsync(mapId, context.CancellationToken);
if (existingMapFile != null)
{
// 删除旧的物理文件
await _fileStorageService.DeleteFileAsync(existingMapFile.FilePath, context.CancellationToken);
// 删除旧的数据库记录
await _mapFileRepository.DeleteAsync(existingMapFile, context.CancellationToken);
await _mapFileRepository.SaveChangesAsync(context.CancellationToken);
}
// 保存文件到磁盘
var fileResult = await _fileStorageService.SaveFileAsync(
command.FileStream,
command.FileName,
"maps",
context.CancellationToken);
// 创建地图文件实体
var mapFile = new MapFile
{
Id = Guid.NewGuid().ToString(),
FileName = fileResult.FileName,
FileSize = fileResult.FileSize,
FilePath = fileResult.RelativePath,
Opacity = command.Opacity,
Scale = command.Scale,
Rotation = command.Rotation,
OffsetX = command.OffsetX,
OffsetY = command.OffsetY,
UploadTime = DateTime.Now,
MapId = mapId,
Active = true
};
// 保存到数据库
await _mapFileRepository.AddAsync(mapFile, context.CancellationToken);
await _mapFileRepository.SaveChangesAsync(context.CancellationToken);
_logger.LogInformation(
"地图文件上传成功: MapId={MapId}, FileName={FileName}, Size={Size}",
mapId,
command.FileName,
fileResult.FileSize);
await context.RespondAsync(ApiResponse.Successful("文件上传成功"));
}
catch (Exception ex)
{
_logger.LogError(ex, "上传地图文件失败: MapId={MapId}", command.MapId);
await context.RespondAsync(ApiResponse.Failed($"上传失败: {ex.Message}"));
}
}
}