fix: 下载接口的range无效

master
ArgonarioD 2023-07-06 16:52:27 +08:00
parent 3812a46e2f
commit cde421e735
3 changed files with 23 additions and 9 deletions

View File

@ -44,6 +44,7 @@ public class FileController : ControllerBase {
[HttpPost] [HttpPost]
public async Task<IActionResult> UploadFilePart(string fileId, [FromForm] UploadFileDto dto) { public async Task<IActionResult> UploadFilePart(string fileId, [FromForm] UploadFileDto dto) {
try {
var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId); var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId);
if (metadata.Ticket != dto.Ticket) { if (metadata.Ticket != dto.Ticket) {
return this.ProblemFromCode(ErrorCodes.TicketMismatch); return this.ProblemFromCode(ErrorCodes.TicketMismatch);
@ -51,6 +52,9 @@ public class FileController : ControllerBase {
await _knowledgeFileHandler.SaveFileSlice(fileId, dto.File, dto.RangeStart, dto.RangeEnd); await _knowledgeFileHandler.SaveFileSlice(fileId, dto.File, dto.RangeStart, dto.RangeEnd);
return Ok(); return Ok();
} catch (FileNotFoundException e) {
return Problem(statusCode: StatusCodes.Status404NotFound, title: e.Message);
}
} }
[HttpPost("complete")] [HttpPost("complete")]

View File

@ -85,10 +85,10 @@ public class KnowledgeFileHandler {
public async Task<Memory<byte>> ReadRanged(string fileId, ulong rangeStart, ulong rangeEnd) { public async Task<Memory<byte>> ReadRanged(string fileId, ulong rangeStart, ulong rangeEnd) {
var metadata = await GetFileMetadata(fileId); var metadata = await GetFileMetadata(fileId);
var fileStream = File.OpenRead(GetFileName(fileId, metadata.Suffix)); await using var fileStream = File.OpenRead(GetFileName(fileId, metadata.Suffix));
var buffer = new Memory<byte>(new byte[rangeEnd - rangeStart]); var buffer = new Memory<byte>(new byte[rangeEnd - rangeStart]);
fileStream.Seek((long)rangeStart, SeekOrigin.Begin);
await fileStream.ReadExactlyAsync(buffer); await fileStream.ReadExactlyAsync(buffer);
fileStream.Close();
return buffer; return buffer;
} }
} }

View File

@ -15,7 +15,17 @@ public class UnitTest1 {
// Console.WriteLine("--- 127.0.0.1:8090 ---"); // Console.WriteLine("--- 127.0.0.1:8090 ---");
// var resp = await client.GetAsync("https://127.0.0.1:8090/hello"); // var resp = await client.GetAsync("https://127.0.0.1:8090/hello");
var resp = await client.GetAsync("https://api.hammer-hfut.tk:233/aics/file/hello"); var content = new MultipartFormDataContent();
content.Add(new StringContent("$5$mq5AaInP$veRAiN732QWi0JIChKnFpArb8LqvJJMBn4t/QBf4DB."), "ticket");
content.Add(new StringContent("149"), "rangeStart");
content.Add(new StringContent("297"), "rangeEnd");
content.Add(
new StreamContent(File.OpenRead(@"C:\Users\arnor\Downloads\auto组第二次汇报.pptx")),
"file",
"file.txt"
);
var resp = await client.PostAsync("https://api.hammer-hfut.tk:233/aics/file/File/8d7c236a-9518-4b57-a143-3fe040cf7954", content);
// var resp = await client.PostAsync("https://127.0.0.1:8081/File/8d7c236a-9518-4b57-a143-3fe040cf7954", content);
var headers = new StringBuilder(); var headers = new StringBuilder();
foreach (var (headerName, headerValues) in resp.Headers) { foreach (var (headerName, headerValues) in resp.Headers) {
headers.AppendLine($"{headerName}: {string.Join(", ", headerValues)}"); headers.AppendLine($"{headerName}: {string.Join(", ", headerValues)}");