40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
namespace BotSharp.Plugin.TencentCos.Services;
|
|
|
|
public partial class TencentCosService
|
|
{
|
|
public bool SaveSpeechFile(string conversationId, string fileName, BinaryData data)
|
|
{
|
|
try
|
|
{
|
|
var file = $"{CONVERSATION_FOLDER}/{conversationId}/{TEXT_TO_SPEECH_FOLDER}/{fileName}";
|
|
var exist = _cosClient.BucketClient.DoesObjectExist(file);
|
|
if (exist)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return _cosClient.BucketClient.UploadBytes(file, data.ToArray());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, $"Error when saving speech file. {fileName} (conv id: {conversationId})");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public BinaryData GetSpeechFile(string conversationId, string fileName)
|
|
{
|
|
var dir = $"{CONVERSATION_FOLDER}/{conversationId}/{TEXT_TO_SPEECH_FOLDER}";
|
|
var key = $"{dir}/{fileName}";
|
|
var file = _cosClient.BucketClient.GetDirFile(dir, key);
|
|
|
|
if (string.IsNullOrWhiteSpace(file))
|
|
{
|
|
return BinaryData.Empty;
|
|
}
|
|
|
|
var bytes = _cosClient.BucketClient.DownloadFileBytes(file);
|
|
return BinaryData.FromBytes(bytes);
|
|
}
|
|
}
|