|
|
@@ -6,9 +6,7 @@
|
|
|
|
|
|
using Aliyun.OSS.Util;
|
|
|
using Furion.VirtualFileServer;
|
|
|
-using Microsoft.AspNetCore.Components.Forms;
|
|
|
using OnceMi.AspNetCore.OSS;
|
|
|
-using System.IO;
|
|
|
|
|
|
namespace Admin.NET.Core.Service;
|
|
|
|
|
|
@@ -58,11 +56,10 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <summary>
|
|
|
/// 上传文件 🔖
|
|
|
/// </summary>
|
|
|
- /// <param name="input"></param>
|
|
|
+ /// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[DisplayName("上传文件")]
|
|
|
- [HttpPost]
|
|
|
- public async Task<SysFile> UploadFile([FromForm]FileUploadInput input)
|
|
|
+ public async Task<SysFile> UploadFile([FromForm] FileUploadInput input)
|
|
|
{
|
|
|
return await HandleUploadFile(input.File, input.Path, filetype: input.FileType);
|
|
|
}
|
|
|
@@ -76,7 +73,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <param name="path"></param>
|
|
|
/// <param name="fileType"></param>
|
|
|
/// <returns></returns>
|
|
|
- private async Task<SysFile> UploadFileFromBase64(string strBase64, string fileName, string contentType, string? path,string? fileType)
|
|
|
+ private async Task<SysFile> UploadFileFromBase64(string strBase64, string fileName, string contentType, string? path, string? fileType)
|
|
|
{
|
|
|
byte[] fileData = Convert.FromBase64String(strBase64);
|
|
|
var ms = new MemoryStream();
|
|
|
@@ -91,7 +88,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
Headers = new HeaderDictionary(),
|
|
|
ContentType = contentType
|
|
|
};
|
|
|
- return await UploadFile(new FileUploadInput {File= formFile, Path= path,FileType= fileType });
|
|
|
+ return await UploadFile(new FileUploadInput { File = formFile, Path = path, FileType = fileType });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -100,10 +97,9 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
[DisplayName("上传文件Base64")]
|
|
|
- [HttpPost]
|
|
|
public async Task<SysFile> UploadFileFromBase64(UploadFileFromBase64Input input)
|
|
|
{
|
|
|
- return await UploadFileFromBase64(input.FileDataBase64, input.FileName, input.ContentType, input.Path,input.FileType);
|
|
|
+ return await UploadFileFromBase64(input.FileDataBase64, input.FileName, input.ContentType, input.Path, input.FileType);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -117,7 +113,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
var filelist = new List<SysFile>();
|
|
|
foreach (var file in files)
|
|
|
{
|
|
|
- filelist.Add(await UploadFile(new FileUploadInput {File=file}));
|
|
|
+ filelist.Add(await UploadFile(new FileUploadInput { File = file }));
|
|
|
}
|
|
|
return filelist;
|
|
|
}
|
|
|
@@ -133,7 +129,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
var file = input.Id > 0 ? await GetFile(input) : await _sysFileRep.GetFirstAsync(u => u.Url == input.Url);
|
|
|
var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
|
- if (_OSSProviderOptions.IsEnable)
|
|
|
+ if (_OSSProviderOptions.IsEnable)
|
|
|
{
|
|
|
var filePath = string.Concat(file.FilePath, "/", file.Id.ToString() + file.Suffix);
|
|
|
var stream = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync();
|
|
|
@@ -141,32 +137,37 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
}
|
|
|
else if (App.Configuration["SSHProvider:IsEnable"].ToBoolean())
|
|
|
{
|
|
|
- var fullPath = string.Concat(file.FilePath, "/", file.Id+ file.Suffix);
|
|
|
+ var fullPath = string.Concat(file.FilePath, "/", file.Id + file.Suffix);
|
|
|
using (SSHHelper helper = new SSHHelper(App.Configuration["SSHProvider:Host"],
|
|
|
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]))
|
|
|
- {
|
|
|
+ {
|
|
|
return new FileStreamResult(helper.OpenRead(fullPath), "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
|
|
|
var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath);
|
|
|
return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 文件预览
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="Id"></param>
|
|
|
+ /// <returns></returns>
|
|
|
[AllowAnonymous]
|
|
|
- [HttpGet("/api/sysFile/Preview/{id}")]
|
|
|
- public async Task<IActionResult> Preview([FromRoute] long Id)
|
|
|
+ public async Task<IActionResult> GetPreview([FromRoute] long Id)
|
|
|
{
|
|
|
var file = await GetFile(new FileInput { Id = Id });
|
|
|
- var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
|
|
|
+ //var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
|
|
|
|
|
|
if (_OSSProviderOptions.IsEnable)
|
|
|
{
|
|
|
var filePath = string.Concat(file.FilePath, "/", file.Id.ToString() + file.Suffix);
|
|
|
var stream = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync();
|
|
|
- return new FileStreamResult(stream.Stream, "application/octet-stream") ;
|
|
|
+ return new FileStreamResult(stream.Stream, "application/octet-stream");
|
|
|
}
|
|
|
else if (App.Configuration["SSHProvider:IsEnable"].ToBoolean())
|
|
|
{
|
|
|
@@ -174,14 +175,14 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
using (SSHHelper helper = new SSHHelper(App.Configuration["SSHProvider:Host"],
|
|
|
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]))
|
|
|
{
|
|
|
- return new FileStreamResult(helper.OpenRead(fullPath), "application/octet-stream") ;
|
|
|
+ return new FileStreamResult(helper.OpenRead(fullPath), "application/octet-stream");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
|
|
|
var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath);
|
|
|
- return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") ;
|
|
|
+ return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -193,7 +194,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
[AllowAnonymous]
|
|
|
[DisplayName("下载指定文件Base64格式")]
|
|
|
public async Task<string> DownloadFileBase64([FromBody] string url)
|
|
|
- {
|
|
|
+ {
|
|
|
if (_OSSProviderOptions.IsEnable)
|
|
|
{
|
|
|
using var httpClient = new HttpClient();
|
|
|
@@ -214,11 +215,11 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
var sysFile = await _sysFileRep.GetFirstAsync(u => u.Url == url) ?? throw Oops.Oh($"文件不存在");
|
|
|
using (SSHHelper helper = new SSHHelper(App.Configuration["SSHProvider:Host"],
|
|
|
App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]))
|
|
|
- {
|
|
|
+ {
|
|
|
return Convert.ToBase64String(helper.ReadAllBytes(sysFile.FilePath));
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
var sysFile = await _sysFileRep.GetFirstAsync(u => u.Url == url) ?? throw Oops.Oh($"文件不存在");
|
|
|
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, sysFile.FilePath);
|
|
|
@@ -281,7 +282,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
var isExist = await _sysFileRep.IsAnyAsync(u => u.Id == input.Id);
|
|
|
if (!isExist) throw Oops.Oh(ErrorCodeEnum.D8000);
|
|
|
|
|
|
- await _sysFileRep.UpdateAsync(u => new SysFile() { FileName = input.FileName,FileType=input.FileType }, u => u.Id == input.Id);
|
|
|
+ await _sysFileRep.UpdateAsync(u => new SysFile() { FileName = input.FileName, FileType = input.FileType }, u => u.Id == input.Id);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -302,7 +303,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <param name="savePath">路径</param>
|
|
|
/// <param name="allowSuffix">允许格式:.jpg.png.gif.tif.bmp</param>
|
|
|
/// <returns></returns>
|
|
|
- private async Task<SysFile> HandleUploadFile(IFormFile file, string savePath, string allowSuffix = "",string filetype = "")
|
|
|
+ private async Task<SysFile> HandleUploadFile(IFormFile file, string savePath, string allowSuffix = "", string filetype = "")
|
|
|
{
|
|
|
if (file == null) throw Oops.Oh(ErrorCodeEnum.D8000);
|
|
|
|
|
|
@@ -365,11 +366,11 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
SizeKb = sizeKb.ToString(),
|
|
|
FilePath = path,
|
|
|
FileMd5 = fileMd5,
|
|
|
- FileType= filetype
|
|
|
+ FileType = filetype
|
|
|
};
|
|
|
|
|
|
var finalName = newFile.Id + suffix; // 文件最终名称
|
|
|
- if (_OSSProviderOptions.IsEnable)
|
|
|
+ if (_OSSProviderOptions.IsEnable)
|
|
|
{
|
|
|
newFile.Provider = Enum.GetName(_OSSProviderOptions.Provider);
|
|
|
var filePath = string.Concat(path, "/", finalName);
|
|
|
@@ -392,14 +393,14 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
}
|
|
|
else if (App.Configuration["SSHProvider:IsEnable"].ToBoolean())
|
|
|
{
|
|
|
- var fullPath = string.Concat( path.StartsWith("/")?path:"/"+path, "/", finalName);
|
|
|
+ var fullPath = string.Concat(path.StartsWith("/") ? path : "/" + path, "/", finalName);
|
|
|
using (SSHHelper helper = new SSHHelper(App.Configuration["SSHProvider:Host"],
|
|
|
- App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"])) {
|
|
|
-
|
|
|
+ App.Configuration["SSHProvider:Port"].ToInt(), App.Configuration["SSHProvider:Username"], App.Configuration["SSHProvider:Password"]))
|
|
|
+ {
|
|
|
helper.UploadFile(file.OpenReadStream(), fullPath);
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
newFile.Provider = ""; // 本地存储 Provider 显示为空
|
|
|
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, path);
|
|
|
@@ -407,25 +408,14 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
Directory.CreateDirectory(filePath);
|
|
|
|
|
|
var realFile = Path.Combine(filePath, finalName);
|
|
|
- //IDetector detector;
|
|
|
using (var stream = File.Create(realFile))
|
|
|
{
|
|
|
await file.CopyToAsync(stream);
|
|
|
- //detector = stream.DetectFiletype();
|
|
|
}
|
|
|
- //var realExt = detector.Extension; // 真实扩展名
|
|
|
- //// 二次校验扩展名
|
|
|
- //if (!string.Equals(realExt, suffix.Replace(".", ""), StringComparison.OrdinalIgnoreCase))
|
|
|
- //{
|
|
|
- // var delFilePath = Path.Combine(App.WebHostEnvironment.WebRootPath, realFile);
|
|
|
- // if (File.Exists(delFilePath))
|
|
|
- // File.Delete(delFilePath);
|
|
|
- // throw Oops.Oh(ErrorCodeEnum.D8001);
|
|
|
- //}
|
|
|
|
|
|
// 生成外链
|
|
|
var host = CommonUtil.GetLocalhost();
|
|
|
- if (!host.EndsWith("/"))
|
|
|
+ if (!host.EndsWith('/'))
|
|
|
host += "/";
|
|
|
newFile.Url = $"{host}{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
|
|
|
}
|
|
|
@@ -495,12 +485,12 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <param name="relationName"></param>
|
|
|
/// <param name="relationId"></param>
|
|
|
/// <param name="belongId"></param>
|
|
|
- /// <returns></returns>
|
|
|
+ /// <returns></returns>
|
|
|
[NonAction]
|
|
|
public async Task<int> UpdateRelation(List<long> ids, string relationName, long relationId, long belongId = 0)
|
|
|
{
|
|
|
if (ids == null || ids.Count == 0)
|
|
|
- return 0;
|
|
|
+ return 0;
|
|
|
return await _sysFileRep.AsUpdateable()
|
|
|
.SetColumns(m => m.RelationName == relationName)
|
|
|
.SetColumns(m => m.RelationId == relationId)
|
|
|
@@ -512,16 +502,16 @@ public class SysFileService : IDynamicApiController, ITransient
|
|
|
/// <summary>
|
|
|
/// 根据关联查询附件
|
|
|
/// </summary>
|
|
|
- /// <param name="input"></param>
|
|
|
+ /// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
- /// <exception cref="ArgumentNullException"></exception>
|
|
|
+ /// <exception cref="ArgumentNullException"></exception>
|
|
|
public async Task<List<FileOutput>> GetRelationFiles(RelationQueryInput input)
|
|
|
{
|
|
|
return await _sysFileRep.AsQueryable()
|
|
|
.Where(m => !m.IsDelete)
|
|
|
.WhereIF(input.RelationId.HasValue && input.RelationId > 0, m => m.RelationId == input.RelationId)
|
|
|
.WhereIF(input.BelongId.HasValue && input.BelongId > 0, m => m.BelongId == input.BelongId.Value)
|
|
|
- .WhereIF(!string.IsNullOrWhiteSpace(input.RelationName), m => m.RelationName == input.RelationName)
|
|
|
+ .WhereIF(!string.IsNullOrWhiteSpace(input.RelationName), m => m.RelationName == input.RelationName)
|
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.FileTypes), m => input.GetFileTypeBS().Contains(m.FileType))
|
|
|
.Select(m => new FileOutput
|
|
|
{
|