shgx_tz_mom/ZR.Service/System/SysFileService.cs

132 lines
4.9 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure.Attribute;
2021-11-29 13:46:55 +08:00
using Microsoft.AspNetCore.Http;
using System.IO;
2021-09-16 19:35:17 +08:00
using ZR.Service.System.IService;
2021-11-29 13:46:55 +08:00
using ZR.Common;
using Infrastructure;
using System;
using System.Text;
using System.Security.Cryptography;
using System.Net;
2021-12-16 11:29:03 +08:00
using ZR.Model.System;
using ZR.Repository.System;
using Infrastructure.Extensions;
2022-03-23 10:22:25 +08:00
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
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");
2021-12-16 11:29:03 +08:00
private readonly SysFileRepository SysFileRepository;
2022-03-23 10:22:25 +08:00
private OptionsSetting OptionsSetting;
public SysFileService(SysFileRepository repository, IOptions<OptionsSetting> options)
2021-12-16 11:29:03 +08:00
{
SysFileRepository = repository;
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>
/// <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-03-23 10:22:25 +08:00
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
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-03-26 20:23:47 +08:00
await formFile.CopyToAsync(stream);//await 不能少
2022-03-23 10:22:25 +08:00
}
string accessPath = string.Concat(OptionsSetting.Upload.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-03-26 20:23:47 +08:00
FileUrl = finalFilePath,
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;
2022-03-10 21:39:46 +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();
}
MD5CryptoServiceProvider md5 = new();
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
}
}