minor change

This commit is contained in:
Jicheng Lu 2024-08-07 17:14:56 -05:00
parent ec5535c72d
commit 0d8532c3c7
5 changed files with 52 additions and 14 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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." });

View file

@ -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<byte>();
}
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);