BotSharp/src/Infrastructure/BotSharp.Core/Files/Services/BotSharpFileService.Common.cs

47 lines
1.3 KiB
C#
Raw Normal View History

2024-07-18 22:07:14 +00:00
using Microsoft.AspNetCore.StaticFiles;
using System.IO;
2024-07-18 20:17:40 +00:00
namespace BotSharp.Core.Files.Services;
public partial class BotSharpFileService
{
2024-07-18 22:07:14 +00:00
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;
}
2024-07-18 20:17:40 +00:00
}