feat: 适配了新的upload_file消息格式

master
ArgonarioD 2023-07-05 16:08:54 +08:00
parent 8cf7ef4c52
commit 94e28b5725
4 changed files with 16 additions and 11 deletions

View File

@ -19,7 +19,7 @@ public class FileController : ControllerBase {
var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId);
var existingRanges = await _knowledgeFileHandler.GetAllPartFileRanges(fileId);
return Ok(new FileStatus(metadata.IsCompleted, metadata.Md5!, metadata.Size!.Value, existingRanges));
return Ok(new FileStatus(metadata.IsCompleted, metadata.Md5, metadata.Size, existingRanges));
} catch (FileNotFoundException) {
return NotFound();
}
@ -39,8 +39,8 @@ public class FileController : ControllerBase {
}
}
[HttpPost("metadata")]
public async Task<IActionResult> PostFileMetadata(string fileId, [FromBody] ClientFileMetadata metadata) {
// [HttpPost("metadata")]
/*public async Task<IActionResult> PostFileMetadata(string fileId, [FromBody] ClientFileMetadata metadata) {
var currentData = await _knowledgeFileHandler.GetFileMetadata(fileId);
if (currentData.Ticket != metadata.Ticket) {
return this.ProblemFromCode(ErrorCodes.TicketMismatch);
@ -54,7 +54,7 @@ public class FileController : ControllerBase {
currentData.Size = metadata.Size;
await _knowledgeFileHandler.SaveFileMetadata(fileId, currentData);
return Ok();
}
}*/
[HttpPost]
public async Task<IActionResult> UploadFilePart(string fileId, [FromForm] UploadFileDto dto) {
@ -84,11 +84,11 @@ public class FileController : ControllerBase {
}
}
public record ClientFileMetadata(
/*public record ClientFileMetadata(
string Ticket,
string Md5,
ulong Size
);
);*/
public record UploadFileDto(
string Ticket,

View File

@ -27,7 +27,7 @@ public class KnowledgeFileHandler {
var range = fileName.Split('.')[2].Split('-');
return Tuple.Create(ulong.Parse(range[0]), ulong.Parse(range[1]));
}
public async Task<List<Tuple<ulong, ulong>>> GetAllPartFileRanges(string fileId) {
var result = (await GetAllPartFiles(fileId)).Select(ConvertPartFileNameToRange).ToList();
result.Sort((a, b) => (int)(a.Item1 - b.Item1));
@ -49,6 +49,7 @@ public class KnowledgeFileHandler {
if (slices.Count == 0) {
throw new InvalidDataException($"0-{metadata.Size}");
}
if (slices.First().Item1 != 0) {
throw new InvalidDataException($"0-{slices.First().Item1}");
}

View File

@ -43,7 +43,9 @@ public class UpdateFileMessageConsumerHandler : BackgroundService {
var msg = result.Message.Value!;
await _knowledgeFileHandler.SaveFileMetadata(msg.Id, new FileMetadata {
IsCompleted = false,
Ticket = msg.Ticket
Ticket = msg.Ticket,
Md5 = msg.Md5,
Size = msg.Size
});
} catch (ConsumeException e) {
_logger.LogError("Consume Exception: {Exception}", e);
@ -59,5 +61,7 @@ public class UpdateFileMessageConsumerHandler : BackgroundService {
public record FileTicket(
[JsonProperty("ticket")] string Ticket,
[JsonProperty("id")] string Id
[JsonProperty("id")] string Id,
[JsonProperty("md5")] string Md5,
[JsonProperty("size")] ulong Size
);

View File

@ -3,6 +3,6 @@
public class FileMetadata {
public bool IsCompleted { get; set; } = false;
public string Ticket { get; set; } = null!;
public string? Md5 { get; set; }
public ulong? Size { get; set; }
public string Md5 { get; set; } = null!;
public ulong Size { get; set; } = 0;
}