This commit is contained in:
chfei committed 2026-09-18 18:57:24 +08:00
1 parent f2caec6083
commit 75d0e85218
16 files changed
+316 -320

No files matched your search

+24 -19
View File
@@ -1,4 +1,4 @@
using MAX.Core.DependencyInjection;
using MAX.Core.DependencyInjection;
using Microsoft.Extensions.Configuration;
using System.IO.Compression;
using System.Text;
@@ -8,7 +8,25 @@ namespace Store.Modules.CloudProduct.Services
/// <summary>
/// 商品原始数据文件存储:按 goodsId % 100 分目录,文件名即商品ID,GZip 压缩保存。
/// </summary>
public class RawDataStorage : IScopedDependency
public interface IRawDataStorage : IScopedDependency
{
/// <summary>
/// 保存原始数据,返回保存后的完整路径。
/// </summary>
Task<string> SaveAsync(long goodsId, string rawData);
/// <summary>
/// 读取并解压原始数据;文件不存在时返回 null。
/// </summary>
Task<string?> ReadAsync(long goodsId);
/// <summary>
/// 判断指定商品的原始数据文件是否存在。
/// </summary>
bool Exists(long goodsId);
}
internal class RawDataStorage : IRawDataStorage
{
private readonly string _rootPath;
@@ -21,9 +39,7 @@ namespace Store.Modules.CloudProduct.Services
_rootPath = Path.GetFullPath(configured);
}
/// <summary>
/// 保存原始数据,返回保存后的完整路径。
/// </summary>
/// <inheritdoc />
public async Task<string> SaveAsync(long goodsId, string rawData)
{
var filePath = GetFilePath(goodsId);
@@ -38,9 +54,7 @@ namespace Store.Modules.CloudProduct.Services
return filePath;
}
/// <summary>
/// 读取并解压原始数据;文件不存在时返回 null。
/// </summary>
/// <inheritdoc />
public async Task<string?> ReadAsync(long goodsId)
{
var filePath = GetFilePath(goodsId);
@@ -53,9 +67,7 @@ namespace Store.Modules.CloudProduct.Services
return await reader.ReadToEndAsync();
}
/// <summary>
/// 判断指定商品的原始数据文件是否存在。
/// </summary>
/// <inheritdoc />
public bool Exists(long goodsId)
{
return File.Exists(GetFilePath(goodsId));
@@ -66,12 +78,5 @@ namespace Store.Modules.CloudProduct.Services
var subDir = (goodsId % 100).ToString();
return Path.Combine(_rootPath, subDir, goodsId.ToString());
}
public async Task<string?> ReadAsync(string filePath)
{
await using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
await using var gzip = new GZipStream(fileStream, CompressionMode.Decompress);
using var reader = new StreamReader(gzip, new UTF8Encoding(false));
return await reader.ReadToEndAsync();
}
}
}
}