Adjust file storage hard-coded logic.

This commit is contained in:
Gil Zhang 2024-07-31 00:32:21 +08:00
parent de3f8dee6d
commit a283abb869
4 changed files with 24 additions and 7 deletions

View file

@ -1,3 +1,5 @@
using System.IO;
namespace BotSharp.Abstraction.Files;
public interface IBotSharpFileService
@ -74,5 +76,7 @@ public interface IBotSharpFileService
(string, byte[]) GetFileInfoFromData(string data);
string GetDirectory(string conversationId);
string GetFileContentType(string filePath);
byte[] GetFileBytes(string fileStorageUrl);
bool SavefileToPath(string filePath, Stream stream);
#endregion
}

View file

@ -43,4 +43,21 @@ public partial class BotSharpFileService
return contentType;
}
public byte[] GetFileBytes(string fileStorageUrl)
{
using var stream = File.OpenRead(fileStorageUrl);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return bytes;
}
public bool SavefileToPath(string filePath, Stream stream)
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
stream.CopyTo(fileStream);
}
return true;
}
}

View file

@ -356,10 +356,7 @@ public class ConversationController : ControllerBase
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var filePath = Path.Combine(dir, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
}
fileService.SavefileToPath(filePath, file.OpenReadStream());
}
return Ok(new { message = "File uploaded successfully." });

View file

@ -158,9 +158,8 @@ public class UserController : ControllerBase
#region Private methods
private FileContentResult BuildFileResult(string file)
{
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var bytes = fileService.GetFileBytes(file);
return File(bytes, "application/octet-stream", Path.GetFileName(file));
}
#endregion