You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
13 KiB
C#
334 lines
13 KiB
C#
using DS.Module.Core;
|
|
using DS.Module.SqlSugar;
|
|
using DS.Module.UserModule;
|
|
using DS.WMS.Core.Sys.Dtos;
|
|
using DS.WMS.Core.Sys.Entity;
|
|
using DS.WMS.Core.Sys.Interface;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using NLog;
|
|
using NPOI.HPSF;
|
|
using NPOI.SS.Formula.Functions;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DS.WMS.Core.Sys.Method
|
|
{
|
|
public class SysFileService : ISysFileService
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ISqlSugarClient db;
|
|
private readonly IUser user;
|
|
private readonly IWebHostEnvironment _environment;
|
|
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
public SysFileService(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
_environment = _serviceProvider.GetRequiredService<IWebHostEnvironment>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加系统附件
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <param name="req"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<string>> AddFile(IFormFile file, [FromForm] SysFileReq req)
|
|
{
|
|
//未上传文件
|
|
if (file == null || file.Length == 0)
|
|
{
|
|
return await Task.FromResult(DataResult<string>.Failed("附件不存在!"));
|
|
}
|
|
|
|
var limitFiles = AppSetting.app(new string[] { "FileSettings", "FileType" });
|
|
var originalFilename = file.FileName; // 文件原始名称
|
|
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
|
|
if (!limitFiles.Contains(fileSuffix))
|
|
{
|
|
return await Task.FromResult(DataResult<string>.Failed("不允许的文件类型!"));
|
|
}
|
|
var basePath = AppSetting.app(new string[] { "FileSettings", "BasePath" });
|
|
var relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
|
|
var dirAbs = string.Empty;
|
|
var fileRelaPath = string.Empty;
|
|
var fileAbsPath = string.Empty;
|
|
if (string.IsNullOrEmpty(basePath))
|
|
{
|
|
dirAbs = Path.Combine(_environment.WebRootPath, relativePath);
|
|
}
|
|
else
|
|
{
|
|
dirAbs = Path.Combine(basePath, relativePath);
|
|
}
|
|
|
|
if (!Directory.Exists(dirAbs))
|
|
Directory.CreateDirectory(dirAbs);
|
|
|
|
|
|
// 先存库获取Id
|
|
var id = SnowFlakeSingle.Instance.NextId();
|
|
var fileSaveName = $"{id}{fileSuffix}".ToLower();
|
|
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
|
|
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
|
|
var newFile = new SysFile
|
|
{
|
|
Id = id,
|
|
FileName = originalFilename,
|
|
FilePath = fileRelaPath,
|
|
TypeCode = req.TypeCode,
|
|
TypeName = req.TypeName,
|
|
LinkId = req.LinkId,
|
|
|
|
};
|
|
await db.Insertable(newFile).ExecuteCommandAsync();
|
|
|
|
using (var stream = File.Create(fileAbsPath))
|
|
{
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
|
|
return await Task.FromResult(DataResult<string>.Success(fileRelaPath));
|
|
}
|
|
/// <summary>
|
|
/// 获取系统附件
|
|
/// </summary>
|
|
/// <param name="id">业务id</param>
|
|
/// <returns></returns>
|
|
public DataResult<List<SysFileRes>> GetSysFileList(string id)
|
|
{
|
|
|
|
var data = db.Queryable<SysFile>()
|
|
.Where(a => a.LinkId == long.Parse(id))
|
|
.Select<SysFileRes>()
|
|
.ToList();
|
|
return DataResult<List<SysFileRes>>.Success(data, MultiLanguageConst.DataQuerySuccess);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存文件并返回文件完整路径
|
|
/// </summary>
|
|
/// <param name="fileDictKey">文件目录KEY</param>
|
|
/// <param name="fileBytes">文件二进制流</param>
|
|
/// <param name="batchNo">批次号</param>
|
|
/// <param name="fileName">文件名称</param>
|
|
/// <param name="attachFileType">附件类型</param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<string>> SaveFileDirect(string fileDictKey, byte[] fileBytes, string batchNo,
|
|
string fileName, string attachFileType)
|
|
{
|
|
string fileRoot = AppSetting.app(new string[] { "FileSettings", "BasePath" });
|
|
string relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
|
|
|
|
/*
|
|
if (string.IsNullOrWhiteSpace(fileRoot))
|
|
fileRoot = _environment.WebRootPath;
|
|
|
|
if (!string.IsNullOrWhiteSpace(attachFileType))
|
|
relativePath += $"\\{attachFileType}";
|
|
|
|
if (!string.IsNullOrWhiteSpace(fileDictKey))
|
|
relativePath += $"\\{fileDictKey}";
|
|
|
|
relativePath += $"\\{DateTime.Now.ToString("yyyyMMddHHmmssfff")}";
|
|
|
|
string filePath = $"{fileRoot}\\{relativePath}";
|
|
|
|
string fileFullName = $"{filePath}\\{fileName}";
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
relativePath = relativePath.Replace("\\", "/");
|
|
filePath = filePath.Replace("\\", "/");
|
|
fileFullName = fileFullName.Replace("\\", "/");
|
|
}*/
|
|
|
|
var dirAbs = string.Empty;
|
|
var fileRelaPath = string.Empty;
|
|
var fileAbsPath = string.Empty;
|
|
if (string.IsNullOrEmpty(fileRoot))
|
|
{
|
|
dirAbs = Path.Combine(_environment.WebRootPath ?? "", relativePath);
|
|
}
|
|
else
|
|
{
|
|
dirAbs = Path.Combine(fileRoot, relativePath);
|
|
}
|
|
|
|
if (!Directory.Exists(dirAbs))
|
|
Directory.CreateDirectory(dirAbs);
|
|
// 先存库获取Id
|
|
var id = SnowFlakeSingle.Instance.NextId();
|
|
|
|
var fileSuffix = Path.GetExtension(fileName).ToLower(); // 文件后缀
|
|
|
|
var fileSaveName = $"{id}{fileSuffix}".ToLower();
|
|
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
|
|
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
|
|
|
|
Logger.Log(NLog.LogLevel.Info, "批次={no} 生成文件保存路径完成 路由={filePath} 服务器系统={system}", batchNo, fileAbsPath,
|
|
RuntimeInformation.OSDescription);
|
|
|
|
//预先创建目录
|
|
if (!Directory.Exists(dirAbs))
|
|
{
|
|
Directory.CreateDirectory(dirAbs);
|
|
}
|
|
|
|
await File.WriteAllBytesAsync(fileAbsPath, fileBytes);
|
|
|
|
//string bookFilePath = string.Empty;
|
|
|
|
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
//{
|
|
// bookFilePath = System.Text.RegularExpressions.Regex.Match(dirAbs, relativePath.Replace("/", "\\/") + ".*").Value;
|
|
//}
|
|
//else
|
|
//{
|
|
// bookFilePath = System.Text.RegularExpressions.Regex.Match(dirAbs, relativePath.Replace("\\", "\\\\") + ".*").Value;
|
|
//}
|
|
|
|
return DataResult<string>.Success(fileSaveName);
|
|
}
|
|
|
|
#region 转移文件
|
|
/// <summary>
|
|
/// 转移文件
|
|
/// </summary>
|
|
/// <param name="fileDictKey">文件目录KEY</param>
|
|
/// <param name="sourceFilePath">源文件完整路径</param>
|
|
/// <param name="batchNo">批次号</param>
|
|
/// <param name="isLocalTempFile">是否生成本地文件</param>
|
|
/// <param name="attachFileType">附件类型 bcfiles-BC文件</param>
|
|
/// <param name="isKeepSource">是否保留原文件</param>
|
|
/// <returns>返回新的文件路径</returns>
|
|
public async Task<DataResult<string>> MoveFile(string fileDictKey, string sourceFilePath, string batchNo,
|
|
bool isLocalTempFile = false, string attachFileType = "bcfiles", bool isKeepSource = false)
|
|
{
|
|
string fileRoot = AppSetting.app(new string[] { "FileSettings", "BasePath" });
|
|
|
|
string relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
|
|
|
|
/*
|
|
if (string.IsNullOrWhiteSpace(fileRoot))
|
|
fileRoot = _environment.WebRootPath;
|
|
|
|
if (!string.IsNullOrWhiteSpace(attachFileType))
|
|
relativePath += $"\\{attachFileType}";
|
|
|
|
if (!string.IsNullOrWhiteSpace(fileDictKey))
|
|
relativePath += $"\\{fileDictKey}";
|
|
|
|
relativePath += $"\\{DateTime.Now.ToString("yyyyMMddHHmmss")}";
|
|
|
|
string filePath = $"{fileRoot}\\{relativePath}";
|
|
|
|
string fileFullName = $"{filePath}\\{new System.IO.FileInfo(sourceFilePath).Name}";
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
relativePath = relativePath.Replace("\\", "/");
|
|
filePath = filePath.Replace("\\", "/");
|
|
fileFullName = fileFullName.Replace("\\", "/");
|
|
}
|
|
*/
|
|
var dirAbs = string.Empty;
|
|
var fileRelaPath = string.Empty;
|
|
var fileAbsPath = string.Empty;
|
|
if (string.IsNullOrEmpty(fileRoot))
|
|
{
|
|
dirAbs = Path.Combine(_environment.WebRootPath ?? "", relativePath);
|
|
}
|
|
else
|
|
{
|
|
dirAbs = Path.Combine(fileRoot, relativePath);
|
|
}
|
|
|
|
if (!Directory.Exists(dirAbs))
|
|
Directory.CreateDirectory(dirAbs);
|
|
// 先存库获取Id
|
|
var id = SnowFlakeSingle.Instance.NextId();
|
|
|
|
var fileSuffix = Path.GetExtension(sourceFilePath).ToLower(); // 文件后缀
|
|
|
|
var fileSaveName = $"{id}{fileSuffix}".ToLower();
|
|
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
|
|
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
|
|
|
|
Logger.Log(NLog.LogLevel.Info, $"批次={batchNo} 生成文件保存路径完成 路由={fileAbsPath} 服务器系统={RuntimeInformation.OSDescription}");
|
|
|
|
////预先创建目录
|
|
//if (!Directory.Exists(filePath))
|
|
//{
|
|
// Directory.CreateDirectory(filePath);
|
|
//}
|
|
|
|
if (sourceFilePath.StartsWith("http", StringComparison.OrdinalIgnoreCase) ||
|
|
sourceFilePath.StartsWith("https", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
//var bcStream = await sourceFilePath.GetAsStreamAsync();
|
|
|
|
//using (var fileStream = File.Create(fileFullName))
|
|
//{
|
|
// await bcStream.CopyToAsync(fileStream);
|
|
//}
|
|
}
|
|
else
|
|
{
|
|
System.IO.FileStream file = new System.IO.FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);
|
|
|
|
using (var fileStream = File.Create(fileAbsPath))
|
|
{
|
|
await file.CopyToAsync(fileStream);
|
|
}
|
|
|
|
file.Close();
|
|
|
|
|
|
try
|
|
{
|
|
if (!isKeepSource)
|
|
{
|
|
//删除原文件
|
|
System.IO.File.Delete(sourceFilePath);
|
|
}
|
|
}
|
|
catch (Exception delEx)
|
|
{
|
|
Logger.Log(NLog.LogLevel.Info, $"批次={batchNo} 删除文件异常 filepath={sourceFilePath} ex={delEx.Message}");
|
|
}
|
|
}
|
|
|
|
Logger.Log(NLog.LogLevel.Info, $"批次={batchNo} 完成文件保存 filepath={sourceFilePath}");
|
|
|
|
string bookFilePath = string.Empty;
|
|
|
|
//if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
//{
|
|
// bookFilePath = System.Text.RegularExpressions.Regex.Match(fileFullName, relativePath.Replace("/", "\\/") + ".*").Value;
|
|
//}
|
|
//else
|
|
//{
|
|
// bookFilePath = System.Text.RegularExpressions.Regex.Match(fileFullName, relativePath.Replace("\\", "\\\\") + ".*").Value;
|
|
//}
|
|
|
|
return DataResult<string>.Success(fileSaveName);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|