From f5ff0b0492e2799226234dfd8633877b8f9a81d7 Mon Sep 17 00:00:00 2001
From: 移动系统liao <liaoxujun@qq.com>
Date: 星期四, 15 八月 2024 15:22:53 +0800
Subject: [PATCH] 完成阿里云文件上传APi和服务
---
cylsg/EzUpFile/EzFileUploadService.cs | 282 +++++++++++++++++++++++++++++++++++
cylsg/EzUpFile/EzUpFile.csproj | 15 +
/dev/null | 7
.gitignore | 2
cylsg/cylsg.Web.Core/cylsg.Web.Core.csproj | 1
cylsg/cylsg.Application/LogoInController.cs | 3
cylsg/cylsg.Application/applicationsettings.json | 8
cylsg/cylsg.Web.Entry/appsettings.json | 1
cylsg/EzUpFile/UpFileController.cs | 80 ++++++++++
cylsg/EzUpFile/UpFileConfig.json | 29 +++
cylsg/EzUpFile/IEzFileUploadService.cs | 30 +++
11 files changed, 446 insertions(+), 12 deletions(-)
diff --git a/.gitignore b/.gitignore
index f80bbf4..c6f6863 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,3 +23,5 @@
cylsg/EzTencentCloud/bin/
cylsg/EzTencentCloud/obj/
cylsg/EzUpFile/obj/
+cylsg/cylsg.services/bin/
+cylsg/EzUpFile/bin/
diff --git a/cylsg/EzTencentCloud/Class1.cs b/cylsg/EzTencentCloud/Class1.cs
deleted file mode 100644
index 5238cf9..0000000
--- a/cylsg/EzTencentCloud/Class1.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-锘縩amespace EzTencentCloud
-{
- public class Class1
- {
-
- }
-}
diff --git a/cylsg/EzUpFile/Class1.cs b/cylsg/EzUpFile/Class1.cs
deleted file mode 100644
index 2d366e9..0000000
--- a/cylsg/EzUpFile/Class1.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-锘縩amespace EzUpFile
-{
- public class Class1
- {
-
- }
-}
diff --git a/cylsg/EzUpFile/EzFileUploadService.cs b/cylsg/EzUpFile/EzFileUploadService.cs
new file mode 100644
index 0000000..a10403d
--- /dev/null
+++ b/cylsg/EzUpFile/EzFileUploadService.cs
@@ -0,0 +1,282 @@
+锘�
+using Aliyun.OSS;
+using Aliyun.OSS.Util;
+using cylsg.utility;
+using cylsg.utility.Extend;
+using Furion;
+using Furion.DependencyInjection;
+using Furion.FriendlyException;
+using Microsoft.AspNetCore.Http;
+
+using SqlSugar;
+using System.Globalization;
+using System.Security.Policy;
+namespace EzUpFile
+{
+ /// <summary>
+ /// 闄勪欢鏈嶅姟绋嬪簭
+ /// </summary>
+ public class EzFileUploadService : IEzFileUploadService, IScoped
+ {
+
+ private readonly HttpRequest? _request;
+ private readonly ISqlSugarClient _sqlSugarClient;
+ public EzFileUploadService(IHttpContextAccessor httpContext, ISqlSugarClient sqlSugarClient)
+ {
+
+ _request = httpContext.HttpContext?.Request ?? null;
+ _sqlSugarClient = sqlSugarClient;
+ }
+
+ /// <summary>
+ /// 涓婁紶闄勪欢
+ /// </summary>
+ /// <returns></returns>
+ public async Task<string> UploadFiles()
+ {
+
+
+ var maxSize = 1024 * 1024 * 5; //涓婁紶澶у皬5M
+
+ var file = _request?.Form?.Files["file"];
+ if (file == null)
+ {
+ throw Oops.Oh("浣犳病鏈夐�夋嫨鏂囦欢");
+ }
+
+ var fileName = file.FileName;
+ var fileExt = Path.GetExtension(fileName).ToLowerInvariant();
+
+ //妫�鏌ュぇ灏�
+ if (file.Length > maxSize)
+ {
+ throw Oops.Oh("鏂囦欢澶т簬璁剧疆鏂囦欢");
+ }
+
+ ////妫�鏌ユ枃浠舵墿灞曞悕
+ //if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(op.AttachmentSaveFileExtName?.Split(',') ?? new string[] { "" }, fileExt.Substring(1).ToLower()) == -1)
+ //{
+ // throw Oops.Oh("涓婁紶鏂囦欢鎵╁睍鍚嶆槸涓嶅厑璁哥殑鎵╁睍鍚�,璇蜂笂浼犲悗缂�鍚嶄负锛�" + op.AttachmentSaveFileExtName);
+
+ //}
+
+ string url = string.Empty;
+
+
+ url = await UpLoadFileForAliYunOSS(fileExt, file);
+
+
+ return url;
+
+
+ }
+
+ /// <summary>
+ /// 涓婁紶base64
+ /// </summary>
+ /// <param name="base64"></param>
+ /// <returns></returns>
+ public async Task<string> UploadFilesFByBase64(string base64)
+ {
+
+ if (string.IsNullOrEmpty(base64))
+ {
+ throw Oops.Oh("娌℃湁鍐呭");
+ }
+
+ //妫�鏌ヤ笂浼犲ぇ灏�
+ if (!CommonHelper.CheckBase64Size(base64, 5))
+ {
+ throw Oops.Oh("涓婁紶鏂囦欢澶у皬瓒呰繃闄愬埗锛屾渶澶у厑璁镐笂浼�" + "5" + "M");
+
+ }
+
+ base64 = base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//灏哹ase64澶撮儴淇℃伅鏇挎崲
+ byte[] bytes = Convert.FromBase64String(base64);
+ MemoryStream memStream = new MemoryStream(bytes);
+
+ string url = string.Empty;
+
+ url = await UpLoadBase64ForAliYunOSS(memStream);
+
+
+
+ return url;
+ }
+
+ /// <summary>
+ /// 鍒犻櫎鏂囦欢
+ /// </summary>
+ /// <param name="Path"></param>
+ /// <returns></returns>
+ public async Task<bool> DelFile(string Path)
+ {
+
+
+ var ret = await DelFileForAliYunOSS(Path);
+
+
+
+ return ret;
+ }
+
+
+
+ #region 闃块噷浜戜笂浼犳柟娉曪紙File锛�
+ /// <summary>
+ /// 闃块噷浜戜笂浼犳柟娉曪紙File锛�
+ /// </summary>
+ /// <param name="options"></param>
+ /// <param name="fileExt"></param>
+ /// <param name="file"></param>
+ /// <returns></returns>
+ public async Task<string> UpLoadFileForAliYunOSS(string fileExt, IFormFile file)
+ {
+
+
+ var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
+ var today = DateTime.Now.ToString("yyyyMMdd");
+
+ //涓婁紶鍒伴樋閲屼簯
+ await using var fileStream = file.OpenReadStream();
+ var md5 = OssUtils.ComputeContentMd5(fileStream, file.Length);
+
+ var filePath = App.Configuration["FileUploadOptions:SavePath"] + today + "/" + newFileName; //浜戞枃浠朵繚瀛樿矾寰�
+ //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword
+ var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]);
+ //灏嗘枃浠秏d5鍊艰祴鍊肩粰meat澶翠俊鎭紝鏈嶅姟鍣ㄩ獙璇佹枃浠禡D5
+ var objectMeta = new ObjectMetadata
+ {
+ ContentMd5 = md5
+ };
+
+
+ var task = Task.Run(() =>
+ //鏂囦欢涓婁紶--绌洪棿鍚嶃�佹枃浠朵繚瀛樿矾寰勩�佹枃浠舵祦銆乵eta澶翠俊鎭�(鏂囦欢md5) //杩斿洖meta澶翠俊鎭�(鏂囦欢md5)
+ aliYun.PutObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], filePath, fileStream, objectMeta)
+
+
+
+ );
+ //绛夊緟瀹屾垚
+ try
+ {
+ task.Wait();
+ }
+ catch (AggregateException ex)
+ {
+
+ throw Oops.Oh(ex.Message);
+ }
+
+ //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src
+
+ return App.Configuration["FileUploadOptions:AliOSSSaveBaseUrl"] + filePath;
+ }
+
+ #endregion
+
+
+
+
+
+ #region 闃块噷浜戜笂浼犳柟娉曪紙Base64锛�
+ /// <summary>
+ /// 闃块噷浜戜笂浼犳柟娉曪紙Base64锛�
+ /// </summary>
+ /// <param name="options"></param>
+ /// <param name="memStream"></param>
+ /// <returns></returns>
+ public async Task<string> UpLoadBase64ForAliYunOSS(MemoryStream memStream)
+ {
+
+
+ var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + ".jpg";
+ var today = DateTime.Now.ToString("yyyyMMdd");
+
+ // 璁剧疆褰撳墠娴佺殑浣嶇疆涓烘祦鐨勫紑濮�
+ memStream.Seek(0, SeekOrigin.Begin);
+
+ await using var fileStream = memStream;
+ var md5 = OssUtils.ComputeContentMd5(fileStream, memStream.Length);
+
+ var filePath = App.Configuration["FileUploadOptions:SavePath"] + today + "/" + newFileName; //浜戞枃浠朵繚瀛樿矾寰�
+ //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword
+ var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]);
+ //灏嗘枃浠秏d5鍊艰祴鍊肩粰meat澶翠俊鎭紝鏈嶅姟鍣ㄩ獙璇佹枃浠禡D5
+ var objectMeta = new ObjectMetadata
+ {
+ ContentMd5 = md5
+ };
+ try
+ {
+ //鏂囦欢涓婁紶--绌洪棿鍚嶃�佹枃浠朵繚瀛樿矾寰勩�佹枃浠舵祦銆乵eta澶翠俊鎭�(鏂囦欢md5) //杩斿洖meta澶翠俊鎭�(鏂囦欢md5)
+ aliYun.PutObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], filePath, fileStream, objectMeta);
+ }
+ catch (AggregateException ex)
+ {
+
+ throw Oops.Oh(ex.Message);
+ }
+
+
+ //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src
+
+ return App.Configuration["FileUploadOptions:AliOSSSaveBaseUrl"] + filePath;
+
+ }
+
+ #endregion
+
+
+
+
+
+ #region 鍒犻櫎鏂囦欢
+
+ /// <summary>
+ /// 闃块噷浜戝垹闄�
+ /// </summary>
+ /// <param name="options"></param>
+ /// <param name="fileUrl">甯xx.xx鐨勯摼鎺ュ湴鍧�</param>
+ /// <returns></returns>
+ public async Task<bool> DelFileForAliYunOSS(string fileUrl)
+ {
+
+
+
+
+
+
+
+
+
+
+ //鍒濆鍖栭樋閲屼簯閰嶇疆--澶栫綉Endpoint銆佽闂甀D銆佽闂畃assword
+ var aliYun = new OssClient(App.Configuration["FileUploadOptions:AliOSSEndpoint"], App.Configuration["FileUploadOptions:AliOSSAccessKeyID"], App.Configuration["FileUploadOptions:AliOSSAccessKeySecret"]);
+
+ try
+ {
+ var task = Task.Run(() => aliYun.DeleteObject(App.Configuration["FileUploadOptions:AliOSSBucketName"], (App.Configuration["FileUploadOptions:SavePath"]?.RemoveStartWithStr("/") ?? "") + fileUrl.GetFileName()));
+
+
+ task.Wait();
+
+ }
+ catch (Exception ex)
+ {
+
+ throw Oops.Oh(ex.Message);
+ }
+
+
+ //杩斿洖缁橴Editor鐨勬彃鍏ョ紪杈戝櫒鐨勫浘鐗囩殑src
+
+ return true;
+
+ }
+ #endregion
+
+
+ }
+}
diff --git a/cylsg/EzUpFile/EzUpFile.csproj b/cylsg/EzUpFile/EzUpFile.csproj
index fa71b7a..7e6ce6a 100644
--- a/cylsg/EzUpFile/EzUpFile.csproj
+++ b/cylsg/EzUpFile/EzUpFile.csproj
@@ -6,4 +6,19 @@
<Nullable>enable</Nullable>
</PropertyGroup>
+ <ItemGroup>
+ <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\cylsg.Core\cylsg.Core.csproj" />
+ <ProjectReference Include="..\cylsg.utility\cylsg.utility.csproj" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <None Update="UpFileConfig.json">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+
</Project>
diff --git a/cylsg/EzUpFile/IEzFileUploadService.cs b/cylsg/EzUpFile/IEzFileUploadService.cs
new file mode 100644
index 0000000..3af7ecc
--- /dev/null
+++ b/cylsg/EzUpFile/IEzFileUploadService.cs
@@ -0,0 +1,30 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EzUpFile
+{
+ public interface IEzFileUploadService
+ {
+ /// <summary>
+ /// 涓婁紶base64
+ /// </summary>
+ /// <param name="base64"></param>
+ /// <returns></returns>
+ Task<string> UploadFilesFByBase64(string base64);
+ /// <summary>
+ /// 涓婁紶鏂囦欢
+ /// </summary>
+ /// <returns></returns>
+ Task<string> UploadFiles();
+ /// <summary>
+ /// 鍒犻櫎鏂囦欢
+ /// </summary>
+ /// <param name="Path"></param>
+ /// <returns></returns>
+ Task<bool> DelFile(string Path);
+
+ }
+}
diff --git a/cylsg/EzUpFile/UpFileConfig.json b/cylsg/EzUpFile/UpFileConfig.json
new file mode 100644
index 0000000..31c4179
--- /dev/null
+++ b/cylsg/EzUpFile/UpFileConfig.json
@@ -0,0 +1,29 @@
+{
+ "FileUploadOptions": {
+
+ "AliOSSBucketName": "appimchat",
+ "SavePath": "cylsg/",
+ "AliOSSEndpoint": "oss-accelerate.aliyuncs.com",
+ "AliOSSAccessKeyID": "LTAI5tNYGwTd3swLhC8H2XYV",
+ "AliOSSAccessKeySecret": "TyfkpYbXRUCh1K8LLtUyxY3ZcFCy1A",
+ "AliOSSSaveBaseUrl": "https://appimchat.oss-cn-chengdu.aliyuncs.com/"
+
+ },
+
+ "[openapi:FileUpdata]": {
+ "Title": "鏂囦欢涓婁紶",
+ "Order": 0,
+ "Description": "鏂囦欢涓婁紶API",
+ "Version": "1.0.0",
+ "TermsOfService": "",
+ "Contact": {
+ "Name": "鏂囦欢涓婁紶宸濆嵃涓存椂宸�",
+ "Url": "",
+ "Email": "liaoXuJun@QQ.Com"
+ },
+ "License": {
+ "Name": "MIT",
+ "Url": ""
+ }
+ }
+}
diff --git a/cylsg/EzUpFile/UpFileController.cs b/cylsg/EzUpFile/UpFileController.cs
new file mode 100644
index 0000000..a9b428c
--- /dev/null
+++ b/cylsg/EzUpFile/UpFileController.cs
@@ -0,0 +1,80 @@
+锘縰sing Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace EzUpFile
+{
+ /// <summary>
+ /// 鏂囦欢涓婁紶涓嬭浇
+ /// </summary>
+ [DynamicApiController]
+ [ApiDescriptionSettings("FileUpdata@0")]
+ public class UpFileController
+ {
+
+ /// <summary>
+ /// 涓婁紶闄勪欢
+ /// </summary>
+ /// <param name="fileUploadService"></param>
+ /// <returns></returns>
+ [HttpPost]
+ public async Task<string> UpdateFile([FromServices] IEzFileUploadService fileUploadService, IFormFile file)
+ {
+ return await fileUploadService.UploadFiles();
+ }
+ /// <summary>
+ /// 鑾峰彇鍒板鎴风鐨処Pv4
+ /// </summary>
+ /// <param name="httpContextAccessor"></param>
+ /// <returns></returns>
+ [HttpGet]
+ public string GetIp4([FromServices] IHttpContextAccessor httpContextAccessor)
+ {
+ var httpc = httpContextAccessor.HttpContext;
+ var ipv4 = httpc.GetRemoteIpAddressToIPv4();
+ return ipv4;
+ }
+ /// <summary>
+ /// 涓婁紶闄勪欢
+ /// </summary>
+ /// <param name="fileUploadService"></param>
+ /// <param name="Param"></param>
+ /// <returns></returns>
+ [HttpPost]
+ public async Task<string> UpdateFileBase64([FromServices] IEzFileUploadService fileUploadService, UpDataFileData Param)
+ {
+ return await fileUploadService.UploadFilesFByBase64(Param.FileBase64);
+ }
+ [HttpPost]
+ public async Task<bool> DelFile([FromServices] IEzFileUploadService fileUploadService, DelFileData Param)
+ {
+ return await fileUploadService.DelFile(Param.FilePath);
+ }
+ }
+ /// <summary>
+ /// 涓婁紶鏁版嵁
+ /// </summary>
+ public class UpDataFileData
+
+ {
+ /// <summary>
+ /// 鏁版嵁base64
+ /// </summary>
+ public string FileBase64 { get; set; }
+ }
+ /// <summary>
+ /// 涓婁紶鏁版嵁
+ /// </summary>
+ public class DelFileData
+
+ {
+ /// <summary>
+ /// 鏁版嵁base64
+ /// </summary>
+ public string FilePath { get; set; }
+ }
+}
diff --git a/cylsg/cylsg.Application/LogoInController.cs b/cylsg/cylsg.Application/LogoInController.cs
index 8521f42..a97ec29 100644
--- a/cylsg/cylsg.Application/LogoInController.cs
+++ b/cylsg/cylsg.Application/LogoInController.cs
@@ -15,6 +15,7 @@
/// 鐧诲綍
/// </summary>
[DynamicApiController]
+ [ApiDescriptionSettings("Default@1")]
public class LogoIn
{
private readonly IWechatService _wechatService;
@@ -47,7 +48,7 @@
}
var jwt = new EzJwtModel()
{
- ITCode = user.ItCode,
+ //ITCode = user.ItCode, 涓嶅湪鏄庢枃涓娇鐢↖tcode
NickName = user.Nickname,
UserID = user.Id,
diff --git a/cylsg/cylsg.Application/applicationsettings.json b/cylsg/cylsg.Application/applicationsettings.json
index 4b91598..47b488e 100644
--- a/cylsg/cylsg.Application/applicationsettings.json
+++ b/cylsg/cylsg.Application/applicationsettings.json
@@ -1,14 +1,14 @@
锘縶
"$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
"SpecificationDocumentSettings": {
- "DocumentTitle": "Furion | 宸濆嵃闆舵椂宸�",
+ "DocumentTitle": " 宸濆嵃涓存椂宸�",
"GroupOpenApiInfos": [
{
"Group": "Default",
- "Title": "宸濆嵃闆舵椂宸�",
- "Description": "宸濆嵃闆舵椂宸�",
+ "Title": "宸濆嵃涓存椂宸�",
+ "Description": "宸濆嵃涓存椂宸�",
"Version": "1.0.0",
-
+ "Order": 10,
"Contact": {
"Name": "",
diff --git a/cylsg/cylsg.Web.Core/cylsg.Web.Core.csproj b/cylsg/cylsg.Web.Core/cylsg.Web.Core.csproj
index b558568..76cc66d 100644
--- a/cylsg/cylsg.Web.Core/cylsg.Web.Core.csproj
+++ b/cylsg/cylsg.Web.Core/cylsg.Web.Core.csproj
@@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\cylsg.Application\cylsg.Application.csproj" />
<ProjectReference Include="..\cylsg.Authorization\cylsg.Authorization.csproj" />
+ <ProjectReference Include="..\EzUpFile\EzUpFile.csproj" />
</ItemGroup>
</Project>
diff --git a/cylsg/cylsg.Web.Entry/appsettings.json b/cylsg/cylsg.Web.Entry/appsettings.json
index c9ec59a..bb50097 100644
--- a/cylsg/cylsg.Web.Entry/appsettings.json
+++ b/cylsg/cylsg.Web.Entry/appsettings.json
@@ -24,6 +24,7 @@
],
+
"DynamicApiControllerSettings": {
"KeepName": true,
"KeepVerb": true,
--
Gitblit v1.9.1