修改:文件预览

This commit is contained in:
coolcalf 2024-08-12 12:08:38 +08:00
parent 10e0f65fb4
commit c686d8a376

View File

@ -6,6 +6,7 @@
using Aliyun.OSS.Util;
using OnceMi.AspNetCore.OSS;
using System.IO;
namespace Admin.NET.Core.Service;
@ -147,28 +148,20 @@ public class SysFileService : IDynamicApiController, ITransient
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 contextType = GetContextType(file.Suffix);
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");
}
else if (App.Configuration["SSHProvider:IsEnable"].ToBoolean())
{
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");
}
var (stream, encoding, response) = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync();
return new FileDownHelper().File(stream, contextType);
}
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");
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
return new FileDownHelper().File(stream, contextType);
}
}
@ -413,23 +406,12 @@ public class SysFileService : IDynamicApiController, ITransient
var host = CommonUtil.GetLocalhost();
if (!host.EndsWith('/'))
host += "/";
newFile.Url = $"{host}{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
newFile.Url = $"/sysFile/preview/{newFile.Id}";
}
await _sysFileRep.AsInsertable(newFile).ExecuteCommandAsync();
return newFile;
}
///// <summary>
///// 获取Minio文件的下载或者预览地址
///// </summary>
///// <param name="bucketName">桶名</param>
///// <param name="fileName">文件名</param>
///// <returns></returns>
//private async Task<string> GetMinioPreviewFileUrl(string bucketName, string fileName)
//{
// return await _OSSService.PresignedGetObjectAsync(bucketName, fileName, 7);
//}
/// <summary>
/// 上传头像 🔖
/// </summary>
@ -527,4 +509,42 @@ public class SysFileService : IDynamicApiController, ITransient
})
.ToListAsync();
}
}
/// <summary>
/// 根据扩展名返回 ContextType
/// </summary>
/// <param name="ext"></param>
/// <returns></returns>
private string GetContextType(string ext)
{
return ext switch
{
".js" => "application/x-javascript",
".css" => "text/css",
".asp" => "text/asp",
".htm" or ".html" or ".php" => "text/html",
".cgi" => "text/cgi",
".aspx" => "text/aspx",
".jfif" => "image/jpeg",
".jpg" => "image/jpeg",
".webp" => "image/jpeg",
".jpeg" => "image/jpeg",
".gif" => "image/gif",
".ico" => "image/x-icon",
".png" => "image/png",
".xls" or ".xlsx" or ".apk" or ".exe" or ".zip" or ".rar" or ".doc" or ".docx" => "application/x-download",
".pdf" => "application/pdf",
".json" => "application/json",
".swf" => "application/x-shockwave-flash",
".rss" => "application/rss+xml; charset=ISO-8859-1",
".xml" => "text/xml",
//除了以上类型,都允许下载
_ => "application/octet-stream",
};
}
}
//为了使用 ControllBase 中的方法,需要继承自 ControllBase 的对象
internal class FileDownHelper : ControllerBase
{
}