From 0d8532c3c71703af0b37cebd17b2748c1cfa408a Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 7 Aug 2024 17:14:56 -0500 Subject: [PATCH] minor change --- .../Files/IFileBasicService.cs | 4 ++- .../Services/Basic/FileBasicService.Common.cs | 21 ++++++++++++++- .../Instruct/FileInstructService.Pdf.cs | 12 +++------ .../Controllers/ConversationController.cs | 2 +- .../Services/TencentCosService.Common.cs | 27 +++++++++++++++++-- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IFileBasicService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IFileBasicService.cs index 4a985950..50d9413a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IFileBasicService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IFileBasicService.cs @@ -53,7 +53,9 @@ public interface IFileBasicService #region Common string GetDirectory(string conversationId); byte[] GetFileBytes(string fileStorageUrl); - bool SavefileToPath(string filePath, Stream stream); + bool SaveFileStreamToPath(string filePath, Stream stream); + bool SaveFileBytesToPath(string filePath, byte[] bytes); + string GetParentDir(string dir, int level = 1); bool ExistDirectory(string? dir); void CreateDirectory(string dir); void DeleteDirectory(string dir); diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Basic/FileBasicService.Common.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Basic/FileBasicService.Common.cs index df0417fb..a1208a31 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Basic/FileBasicService.Common.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Basic/FileBasicService.Common.cs @@ -22,8 +22,10 @@ public partial class FileBasicService return bytes; } - public bool SavefileToPath(string filePath, Stream stream) + public bool SaveFileStreamToPath(string filePath, Stream stream) { + if (string.IsNullOrEmpty(filePath)) return false; + using (var fileStream = new FileStream(filePath, FileMode.Create)) { stream.CopyTo(fileStream); @@ -31,6 +33,23 @@ public partial class FileBasicService return true; } + public bool SaveFileBytesToPath(string filePath, byte[] bytes) + { + using (var fs = new FileStream(filePath, FileMode.Create)) + { + fs.Write(bytes, 0, bytes.Length); + fs.Flush(); + fs.Close(); + } + return true; + } + + public string GetParentDir(string dir, int level = 1) + { + var segs = dir.Split(Path.DirectorySeparatorChar); + return string.Join(Path.DirectorySeparatorChar, segs.SkipLast(level)); + } + public string BuildDirectory(params string[] segments) { var relativePath = Path.Combine(segments); diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs index 4fbe01aa..d4413983 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.Pdf.cs @@ -82,13 +82,8 @@ public partial class FileInstructService DeleteIfExistDirectory(fileDir); var pdfDir = _fileBasic.BuildDirectory(fileDir, $"{guid}.{extension}"); - using (var fs = new FileStream(pdfDir, FileMode.Create)) - { - fs.Write(bytes, 0, bytes.Length); - fs.Close(); - locs.Add(pdfDir); - Thread.Sleep(100); - } + _fileBasic.SaveFileBytesToPath(pdfDir, bytes); + locs.Add(pdfDir); } } catch (Exception ex) @@ -113,8 +108,7 @@ public partial class FileInstructService { try { - var segs = file.Split(Path.DirectorySeparatorChar); - var dir = string.Join(Path.DirectorySeparatorChar, segs.SkipLast(1)); + var dir = _fileBasic.GetParentDir(file); var folder = _fileBasic.BuildDirectory(dir, "screenshots"); var urls = await converter.ConvertPdfToImages(file, folder); images.AddRange(urls); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index a0d65f8a..d81f069a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -357,7 +357,7 @@ public class ConversationController : ControllerBase var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var filePath = Path.Combine(dir, fileName); - fileService.SavefileToPath(filePath, file.OpenReadStream()); + fileService.SaveFileStreamToPath(filePath, file.OpenReadStream()); } return Ok(new { message = "File uploaded successfully." }); diff --git a/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.Common.cs b/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.Common.cs index 61b58c6a..9de15321 100644 --- a/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.Common.cs +++ b/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.Common.cs @@ -1,3 +1,5 @@ +using System.IO; + namespace BotSharp.Plugin.TencentCos.Services; public partial class TencentCosService @@ -21,7 +23,7 @@ public partial class TencentCosService return Array.Empty(); } - public bool SavefileToPath(string filePath, Stream stream) + public bool SaveFileStreamToPath(string filePath, Stream stream) { if (string.IsNullOrEmpty(filePath)) return false; @@ -31,11 +33,32 @@ public partial class TencentCosService } catch (Exception ex) { - _logger.LogWarning($"Error when saving file to path: {ex.Message}\r\n{ex.InnerException}"); + _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; + } + } + + public string GetParentDir(string dir, int level = 1) + { + var segs = dir.Split("/"); + return string.Join("/", segs.SkipLast(level)); + } + public string BuildDirectory(params string[] segments) { return string.Join("/", segments);