Adjust file storage hard-coded logic.
This commit is contained in:
parent
de3f8dee6d
commit
a283abb869
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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." });
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue