shgx_tz_mom/ZR.Service/System/SysFileService.cs

104 lines
3.4 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;
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-19 08:04:08 +08:00
public SysFileService(SysFileRepository repository)
2021-12-16 11:29:03 +08:00
{
SysFileRepository = repository;
}
2021-11-29 13:46:55 +08:00
/// <summary>
/// 上传文件到阿里云
/// </summary>
/// <param name="picdir"></param>
/// <param name="formFile"></param>
/// <returns></returns>
public (bool, string, string) SaveFile(string picdir, IFormFile formFile)
2021-11-29 13:46:55 +08:00
{
2022-03-10 21:39:46 +08:00
return SaveFile(picdir, formFile, "", "");
}
2022-03-10 21:39:46 +08:00
/// <summary>
/// 存储文件
/// </summary>
/// <param name="picdir">文件夹</param>
/// <param name="formFile"></param>
/// <param name="customFileName">自定义文件名</param>
/// <param name="bucketName">存储桶</param>
/// <returns></returns>
public (bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName)
{
// eg: uploads/2020/08/18
2022-03-10 21:39:46 +08:00
//string dir = GetdirPath(picdir.ToString());
string tempName = customFileName.IsEmpty() ? HashFileName() : customFileName;
2021-11-29 13:46:55 +08:00
string fileExt = Path.GetExtension(formFile.FileName);
2022-03-10 21:39:46 +08:00
string fileName = tempName + fileExt;
string webUrl = string.Concat(domainUrl, "/", picdir, "/", fileName);
2021-11-29 13:46:55 +08:00
2022-03-10 21:39:46 +08:00
HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), Path.Combine(picdir, fileName), bucketName);
2021-11-29 13:46:55 +08:00
return (statusCode == HttpStatusCode.OK, webUrl, fileName);
2021-11-29 13:46:55 +08:00
}
public string GetdirPath(string path = "")
{
DateTime date = DateTime.Now;
int year = date.Year;
int month = date.Month;
int day = date.Day;
//int hour = date.Hour;
string timeDir = $"{year}/{month}/{day}";// date.ToString("yyyyMM/dd/HH/");
if (!string.IsNullOrEmpty(path))
{
timeDir = path + "/" + timeDir;
}
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
2021-12-16 11:29:03 +08:00
public long InsertFile(SysFile file)
{
try
{
2022-03-10 21:39:46 +08:00
return Insertable(file).ExecuteReturnSnowflakeId();//单条插入返回雪花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
}
}