Compare commits

...

2 Commits

4 changed files with 21 additions and 8 deletions

View File

@ -53,10 +53,10 @@ public class FileController : ControllerBase {
return Ok(); return Ok();
} }
[HttpPost("complete/{ticket}")] [HttpPost("complete")]
public async Task<IActionResult> FilePostComplete(string fileId, string ticket) { public async Task<IActionResult> FilePostComplete(string fileId, [FromForm] CompleteFileDto dto) {
var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId); var metadata = await _knowledgeFileHandler.GetFileMetadata(fileId);
if (metadata.Ticket != ticket) { if (metadata.Ticket != dto.Ticket) {
return this.ProblemFromCode(ErrorCodes.TicketMismatch); return this.ProblemFromCode(ErrorCodes.TicketMismatch);
} }
@ -80,6 +80,10 @@ public record UploadFileDto(
IFormFile File IFormFile File
); );
public record CompleteFileDto(
string Ticket
);
internal record FileStatus( internal record FileStatus(
bool IsCompleted, bool IsCompleted,
string Md5, string Md5,

View File

@ -38,8 +38,8 @@ public class KnowledgeFileHandler {
return $"{FilePath}/{fileId}.part.{rangeStart}-{rangeEnd}"; return $"{FilePath}/{fileId}.part.{rangeStart}-{rangeEnd}";
} }
public string GetFileName(string fileId) { public string GetFileName(string fileId, string suffix) {
return $"{FilePath}/{fileId}"; return $"{FilePath}/{fileId}.{suffix}";
} }
public async Task SaveFileSlice(string fileId, IFormFile file, ulong rangeStart, ulong rangeEnd) { public async Task SaveFileSlice(string fileId, IFormFile file, ulong rangeStart, ulong rangeEnd) {
@ -69,7 +69,7 @@ public class KnowledgeFileHandler {
} }
} }
var fileStream = File.Create($"{FilePath}/{fileId}"); var fileStream = File.Create(GetFileName(fileId, metadata.Suffix));
foreach (var slice in slices) { foreach (var slice in slices) {
var sliceStream = File.OpenRead(GetSliceFileName(fileId, slice.Item1, slice.Item2)); var sliceStream = File.OpenRead(GetSliceFileName(fileId, slice.Item1, slice.Item2));
await sliceStream.CopyToAsync(fileStream); await sliceStream.CopyToAsync(fileStream);
@ -79,10 +79,13 @@ public class KnowledgeFileHandler {
} }
fileStream.Close(); fileStream.Close();
metadata.IsCompleted = true;
await SaveFileMetadata(fileId, metadata);
} }
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 fileStream = File.OpenRead($"{FilePath}/{fileId}"); var metadata = await GetFileMetadata(fileId);
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]);
await fileStream.ReadExactlyAsync(buffer); await fileStream.ReadExactlyAsync(buffer);
fileStream.Close(); fileStream.Close();

View File

@ -29,7 +29,7 @@ public class TencentCosHandler {
public async Task UploadFile(string fileId, string fileSuffix) { public async Task UploadFile(string fileId, string fileSuffix) {
var uploadTask = new COSXMLUploadTask(_tencentCosOptions.Bucket, $"{fileId}.{fileSuffix}"); var uploadTask = new COSXMLUploadTask(_tencentCosOptions.Bucket, $"{fileId}.{fileSuffix}");
uploadTask.SetSrcPath(_knowledgeFileHandler.GetFileName(fileId)); uploadTask.SetSrcPath(_knowledgeFileHandler.GetFileName(fileId, fileSuffix));
await _transferManager.UploadAsync(uploadTask); await _transferManager.UploadAsync(uploadTask);
} }
} }

View File

@ -2,6 +2,7 @@ using System.Text.Json;
using AicsKnowledgeBase_file.Handlers; using AicsKnowledgeBase_file.Handlers;
using AicsKnowledgeBase_file.Models; using AicsKnowledgeBase_file.Models;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.FileProviders;
Directory.CreateDirectory("./files"); Directory.CreateDirectory("./files");
@ -41,6 +42,11 @@ if (app.Environment.IsDevelopment()) {
app.MapGet("/hello", () => "hello"); app.MapGet("/hello", () => "hello");
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "files")),
RequestPath = "/static"
});
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.MapControllers(); app.MapControllers();