fix: 获取文件状态以及文件分片上传无法正常工作的bug

master
ArgonarioD 2023-07-06 02:57:06 +08:00
parent 85f47330bd
commit b800161895
2 changed files with 8 additions and 6 deletions

View File

@ -19,9 +19,10 @@ public class FileController : ControllerBase {
public async Task<IActionResult> GetFileStatus(string fileId) {
try {
var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId);
var existingRanges = await _knowledgeFileHandler.GetAllPartFileRanges(fileId);
var existingRanges = (await _knowledgeFileHandler.GetAllPartFileRanges(fileId))
.Select((range, _) => $"{range.Item1}-{range.Item2}");
return Ok(new FileStatus(metadata.IsCompleted, metadata.Md5, metadata.Size, existingRanges));
return Ok(new FileStatus(metadata.IsCompleted, metadata.Md5, metadata.Size, existingRanges.ToList()));
} catch (FileNotFoundException) {
return NotFound();
}
@ -83,5 +84,5 @@ internal record FileStatus(
bool IsCompleted,
string Md5,
ulong Size,
List<Tuple<ulong, ulong>> ExistingRanges
List<string> ExistingRanges
);

View File

@ -20,11 +20,11 @@ public class KnowledgeFileHandler {
}
public async Task<string[]> GetAllPartFiles(string fileId) {
return Directory.GetFiles($"{FilePath}/{fileId}.part.*");
return Directory.GetFiles(FilePath, $"{fileId}.part.*");
}
public Tuple<ulong, ulong> ConvertPartFileNameToRange(string fileName) {
var range = fileName.Split('.')[2].Split('-');
var range = fileName.Split('.').Last().Split('-');
return Tuple.Create(ulong.Parse(range[0]), ulong.Parse(range[1]));
}
@ -43,7 +43,8 @@ public class KnowledgeFileHandler {
}
public async Task SaveFileSlice(string fileId, IFormFile file, ulong rangeStart, ulong rangeEnd) {
await file.CopyToAsync(File.Create(GetSliceFileName(fileId, rangeStart, rangeEnd)));
await using var fileStream = File.Create(GetSliceFileName(fileId, rangeStart, rangeEnd));
await file.CopyToAsync(fileStream);
}
/// <exception cref="InvalidDataException">缺失分片时抛出其Message内容是缺失分片的Range</exception>