😎恢复文件预览模式(暂定)
This commit is contained in:
parent
312c1e4101
commit
5603e52553
@ -113,25 +113,23 @@ public class SysFileService : IDynamicApiController, ITransient
|
||||
{
|
||||
var file = input.Id > 0 ? await GetFile(input) : await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == input.Url);
|
||||
var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
|
||||
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + 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") { FileDownloadName = fileName + file.Suffix };
|
||||
}
|
||||
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") { FileDownloadName = fileName + file.Suffix };
|
||||
return new FileStreamResult(helper.OpenRead(filePath), "application/octet-stream") { FileDownloadName = fileName + file.Suffix };
|
||||
}
|
||||
}
|
||||
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 };
|
||||
}
|
||||
@ -147,20 +145,26 @@ public class SysFileService : IDynamicApiController, ITransient
|
||||
public async Task<IActionResult> GetPreview([FromRoute] long Id)
|
||||
{
|
||||
var file = await GetFile(new FileInput { Id = Id });
|
||||
var contextType = GetContextType(file.Suffix);
|
||||
//var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
|
||||
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
|
||||
|
||||
if (_OSSProviderOptions.IsEnable)
|
||||
{
|
||||
var filePath = string.Concat(file.FilePath, "/", file.Id.ToString() + file.Suffix);
|
||||
var (stream, encoding, response) = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync();
|
||||
return new FileDownHelper().File(stream, contextType);
|
||||
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())
|
||||
{
|
||||
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(filePath), "application/octet-stream");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
|
||||
var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath);
|
||||
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true);
|
||||
return new FileDownHelper().File(stream, contextType);
|
||||
return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream");
|
||||
}
|
||||
}
|
||||
|
||||
@ -405,7 +409,7 @@ public class SysFileService : IDynamicApiController, ITransient
|
||||
var host = CommonUtil.GetLocalhost();
|
||||
if (!host.EndsWith('/'))
|
||||
host += "/";
|
||||
newFile.Url = $"/api/sysFile/preview/{newFile.Id}";
|
||||
newFile.Url = $"{host}{newFile.FilePath}/{newFile.Id + newFile.Suffix}";
|
||||
}
|
||||
await _sysFileRep.AsInsertable(newFile).ExecuteCommandAsync();
|
||||
return newFile;
|
||||
@ -508,42 +512,4 @@ 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
|
||||
{
|
||||
}
|
||||
@ -69,10 +69,6 @@ const viteConfig = defineConfig((mode: ConfigEnv) => {
|
||||
target: env.VITE_API_URL,
|
||||
changeOrigin: true,
|
||||
},
|
||||
'^/api': {
|
||||
target: env.VITE_API_URL,
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
build: {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user