BotSharp/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.Common.cs

79 lines
2 KiB
C#
Raw Normal View History

namespace BotSharp.Plugin.TencentCos.Services;
public partial class TencentCosService
{
public string GetDirectory(string conversationId)
{
return $"{CONVERSATION_FOLDER}/{conversationId}/attachments/";
}
2024-08-08 18:37:07 +00:00
public byte[] GetFileBytes(string fileStorageUrl)
{
try
{
2024-08-08 18:37:07 +00:00
return _cosClient.BucketClient.DownloadFileBytes(fileStorageUrl);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when get file bytes: {ex.Message}\r\n{ex.InnerException}");
}
return Array.Empty<byte>();
}
2024-08-07 22:14:56 +00:00
public bool SaveFileStreamToPath(string filePath, Stream stream)
{
if (string.IsNullOrEmpty(filePath)) return false;
try
{
return _cosClient.BucketClient.UploadStream(filePath, stream);
}
catch (Exception ex)
{
2024-08-07 22:14:56 +00:00
_logger.LogWarning($"Error when saving file stream to path: {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
public bool SaveFileBytesToPath(string filePath, byte[] bytes)
{
if (string.IsNullOrEmpty(filePath)) return false;
try
{
return _cosClient.BucketClient.UploadBytes(filePath, bytes);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when saving file bytes to path: {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
2024-08-07 21:56:57 +00:00
2024-08-07 22:14:56 +00:00
public string GetParentDir(string dir, int level = 1)
{
var segs = dir.Split("/");
return string.Join("/", segs.SkipLast(level));
}
2024-08-07 21:56:57 +00:00
public string BuildDirectory(params string[] segments)
{
return string.Join("/", segments);
}
public void CreateDirectory(string dir)
{
}
public bool ExistDirectory(string? dir)
{
return !string.IsNullOrEmpty(dir) && _cosClient.BucketClient.DirExists(dir);
}
public void DeleteDirectory(string dir)
{
_cosClient.BucketClient.DeleteDir(dir);
}
}