1
This commit is contained in:
120
Gatedge.ScanCode/Services/K3FileService.cs
Normal file
120
Gatedge.ScanCode/Services/K3FileService.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Gatedge.K3Cloud.Utils;
|
||||
using Gatedge.K3Cloud.Utils.Model.K3Request;
|
||||
using Gatedge.ScanCode.Models.Vo.BaseData;
|
||||
using Gatedge.ScanCode.Options;
|
||||
using Gatedge.ScanCode.Services.IServices;
|
||||
|
||||
namespace Gatedge.ScanCode.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 金蝶k3服务类
|
||||
/// </summary>
|
||||
public class K3FileService : IK3FileService
|
||||
{
|
||||
/// <summary>
|
||||
/// 金蝶云星空工具类
|
||||
/// </summary>
|
||||
private K3CloudApiUtils _utils;
|
||||
private readonly FileOption _fileOption;
|
||||
private readonly ILogger<K3FileService> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化工具类
|
||||
/// </summary>
|
||||
/// <param name="fileOption"></param>
|
||||
/// <param name="logger"></param>
|
||||
public K3FileService(FileOption fileOption, ILogger<K3FileService> logger)
|
||||
{
|
||||
_fileOption = fileOption;
|
||||
_logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// 下载物料
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
private List<byte> DonwloadFile(AttachmentFile file)
|
||||
{
|
||||
var base64File = DonwloadFile(file, 0);
|
||||
return base64File;
|
||||
}
|
||||
/// <summary>
|
||||
/// 下载物料
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
private List<byte> DonwloadFile(AttachmentFile file, long startIndex)
|
||||
{
|
||||
FileParam fileParam = new FileParam()
|
||||
{
|
||||
FileId = file.FileID,
|
||||
StartIndex = startIndex,
|
||||
};
|
||||
var fileResponse = _utils.FileDonwload(fileParam);
|
||||
var fileBytes = Convert.FromBase64String(fileResponse.FilePart).ToList();
|
||||
if (fileResponse.IsLast == false)
|
||||
{
|
||||
var nextStartIndex = Convert.ToInt64(fileResponse.StartIndex);
|
||||
fileBytes.AddRange(DonwloadFile(file, nextStartIndex));
|
||||
return fileBytes;
|
||||
}
|
||||
return fileBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base64字符串转文件并保存
|
||||
/// </summary>
|
||||
/// <param name="dataBytes">base64字符串</param>
|
||||
/// <param name="fileName">保存的文件名</param>
|
||||
/// <returns>是否转换并保存成功</returns>
|
||||
private bool Base64StringToFile(List<byte> dataBytes, string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
string path = _fileOption.DonwloadPath; //文件保存路径
|
||||
string fullPath = _fileOption.DonwloadPath + '/' + fileName;
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
File.WriteAllBytes(fullPath, dataBytes.ToArray());
|
||||
return File.Exists(fullPath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("异常类型: \t" + e.GetType());
|
||||
_logger.LogError("异常描述:\t" + e.Message);
|
||||
_logger.LogError("异常方法:\t" + e.TargetSite);
|
||||
_logger.LogError("异常堆栈:\t" + e.StackTrace);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件路径,如果没有就会先下载,再返回路径
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
public string GetFilePath(AttachmentFile file)
|
||||
{
|
||||
string filePath = _fileOption.DonwloadPath + "/" + file.FileName;
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
var fileBase64String = DonwloadFile(file);
|
||||
var isSave = Base64StringToFile(fileBase64String, file.FileName);
|
||||
if (!isSave)
|
||||
{
|
||||
throw new Exception("文件保存错误,请联系管理员");
|
||||
}
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置K3Util
|
||||
/// </summary>
|
||||
/// <param name="utils"></param>
|
||||
public void SetK3Util(K3CloudApiUtils utils)
|
||||
{
|
||||
_utils = utils;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user