Files
shgx_tz_mes_backend_sync/ZR.Service/System/SysFileService.cs

137 lines
5.1 KiB
C#
Raw Normal View History

2022-06-09 08:37:31 +08:00
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
2021-11-29 13:46:55 +08:00
using Microsoft.AspNetCore.Http;
2022-06-09 08:37:31 +08:00
using Microsoft.Extensions.Options;
2021-11-29 13:46:55 +08:00
using System;
2022-06-09 08:37:31 +08:00
using System.IO;
2021-11-29 13:46:55 +08:00
using System.Net;
2022-06-09 08:37:31 +08:00
using System.Security.Cryptography;
using System.Text;
2022-03-23 10:22:25 +08:00
using System.Threading.Tasks;
2022-06-09 08:37:31 +08:00
using ZR.Common;
using ZR.Model.System;
using ZR.Service.System.IService;
2021-08-23 16:57:25 +08:00
namespace ZR.Service.System
{
/// <summary>
/// 文件管理
/// </summary>
[AppService(ServiceType = typeof(ISysFileService), ServiceLifetime = LifeTime.Transient)]
2021-12-16 11:29:03 +08:00
public class SysFileService : BaseService<SysFile>, ISysFileService
2021-08-23 16:57:25 +08:00
{
2022-02-23 18:30:17 +08:00
private string domainUrl = AppSettings.GetConfig("ALIYUN_OSS:domainUrl");
private readonly ISysConfigService SysConfigService;
2022-03-23 10:22:25 +08:00
private OptionsSetting OptionsSetting;
public SysFileService(ISysConfigService sysConfigService, IOptions<OptionsSetting> options)
2021-12-16 11:29:03 +08:00
{
SysConfigService = sysConfigService;
2022-03-23 10:22:25 +08:00
OptionsSetting = options.Value;
2021-12-16 11:29:03 +08:00
}
2021-11-29 13:46:55 +08:00
/// <summary>
2022-03-23 10:22:25 +08:00
/// 存储本地
2021-11-29 13:46:55 +08:00
/// </summary>
2022-05-28 19:17:07 +08:00
/// <param name="fileDir">存储文件夹</param>
/// <param name="rootPath">存储根目录</param>
/// <param name="fileName">自定文件名</param>
/// <param name="formFile">上传的文件流</param>
/// <param name="userName"></param>
2021-11-29 13:46:55 +08:00
/// <returns></returns>
2022-03-23 14:01:45 +08:00
public async Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
2021-11-29 13:46:55 +08:00
{
2022-03-23 10:22:25 +08:00
string fileExt = Path.GetExtension(formFile.FileName);
2022-03-23 14:01:45 +08:00
fileName = (fileName.IsEmpty() ? HashFileName() : fileName) + fileExt;
2022-06-09 08:37:31 +08:00
2022-03-23 14:01:45 +08:00
string filePath = GetdirPath(fileDir);
2022-03-23 10:22:25 +08:00
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
}
using (var stream = new FileStream(finalFilePath, FileMode.Create))
{
2022-05-28 19:17:07 +08:00
await formFile.CopyToAsync(stream);
2022-03-23 10:22:25 +08:00
}
2022-06-09 08:37:31 +08:00
string uploadUrl = OptionsSetting.Upload.UploadUrl;
string accessPath = string.Concat(uploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
2022-03-26 20:23:47 +08:00
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, userName)
2022-03-23 10:22:25 +08:00
{
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
FileType = formFile.ContentType,
2022-05-28 19:17:07 +08:00
FileUrl = finalFilePath.Replace("\\", "/"),
2022-03-26 20:23:47 +08:00
AccessUrl = accessPath
2022-03-23 10:22:25 +08:00
};
file.Id = await InsertFile(file);
return file;
}
2022-03-10 21:39:46 +08:00
/// <summary>
2022-03-23 10:22:25 +08:00
/// 上传文件到阿里云
2022-03-10 21:39:46 +08:00
/// </summary>
2022-03-23 14:01:45 +08:00
/// <param name="file"></param>
2022-03-10 21:39:46 +08:00
/// <param name="formFile"></param>
/// <returns></returns>
2022-03-23 14:01:45 +08:00
public async Task<SysFile> SaveFileToAliyun(SysFile file, IFormFile formFile)
{
2022-03-23 14:01:45 +08:00
file.FileName = (file.FileName.IsEmpty() ? HashFileName() : file.FileName) + file.FileExt;
file.StorePath = GetdirPath(file.StorePath);
string finalPath = Path.Combine(file.StorePath, file.FileName);
HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), finalPath, "");
if (statusCode != HttpStatusCode.OK) return file;
2021-11-29 13:46:55 +08:00
2022-03-23 14:01:45 +08:00
file.StorePath = file.StorePath;
file.FileUrl = finalPath;
file.AccessUrl = string.Concat(domainUrl, "/", file.StorePath.Replace("\\", "/"), "/", file.FileName);
file.Id = await InsertFile(file);
2021-11-29 13:46:55 +08:00
2022-03-23 14:01:45 +08:00
return file;
2021-11-29 13:46:55 +08:00
}
2022-03-23 14:01:45 +08:00
/// <summary>
/// 获取文件存储目录
/// </summary>
/// <param name="storePath"></param>
/// <param name="byTimeStore">是否按年月日存储</param>
/// <returns></returns>
public string GetdirPath(string storePath = "", bool byTimeStore = true)
2021-11-29 13:46:55 +08:00
{
DateTime date = DateTime.Now;
2022-03-23 14:01:45 +08:00
string timeDir = date.ToString("yyyyMMdd");
2021-11-29 13:46:55 +08:00
2022-03-23 14:01:45 +08:00
if (!string.IsNullOrEmpty(storePath))
2021-11-29 13:46:55 +08:00
{
2022-03-23 14:01:45 +08:00
timeDir = Path.Combine(storePath, timeDir);
2021-11-29 13:46:55 +08:00
}
return timeDir;
}
public string HashFileName(string str = null)
{
if (string.IsNullOrEmpty(str))
{
str = Guid.NewGuid().ToString();
}
2022-03-06 14:26:05 +08:00
MD5 md5 = MD5.Create();
2021-11-29 13:46:55 +08:00
return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
}
2021-08-23 16:57:25 +08:00
2022-03-23 10:22:25 +08:00
public Task<long> InsertFile(SysFile file)
2021-12-16 11:29:03 +08:00
{
try
{
2022-03-23 10:22:25 +08:00
return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
2021-12-16 11:29:03 +08:00
}
catch (Exception ex)
{
Console.WriteLine("存储图片失败" + ex.Message);
2022-03-10 21:39:46 +08:00
throw new Exception(ex.Message);
2021-12-16 11:29:03 +08:00
}
}
2021-08-23 16:57:25 +08:00
}
}