64 lines
1.7 KiB
C#
64 lines
1.7 KiB
C#
using Microsoft.AspNetCore.StaticFiles;
|
|
using System.IO;
|
|
|
|
namespace BotSharp.Core.Files.Services;
|
|
|
|
public partial class BotSharpFileService
|
|
{
|
|
public string GetDirectory(string conversationId)
|
|
{
|
|
var dir = Path.Combine(_dbSettings.FileRepository, CONVERSATION_FOLDER, conversationId, "attachments");
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
public (string, byte[]) GetFileInfoFromData(string data)
|
|
{
|
|
if (string.IsNullOrEmpty(data))
|
|
{
|
|
return (string.Empty, new byte[0]);
|
|
}
|
|
|
|
var typeStartIdx = data.IndexOf(':');
|
|
var typeEndIdx = data.IndexOf(';');
|
|
var contentType = data.Substring(typeStartIdx + 1, typeEndIdx - typeStartIdx - 1);
|
|
|
|
var base64startIdx = data.IndexOf(',');
|
|
var base64Str = data.Substring(base64startIdx + 1);
|
|
|
|
return (contentType, Convert.FromBase64String(base64Str));
|
|
}
|
|
|
|
public string GetFileContentType(string filePath)
|
|
{
|
|
string contentType;
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
if (!provider.TryGetContentType(filePath, out contentType))
|
|
{
|
|
contentType = string.Empty;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|