Skip to content

Commit

Permalink
只允许下载 Resource 和 wwwroot 目录下的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
liukuo362573 committed Feb 6, 2022
1 parent 05f6152 commit c3b66e6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion YiSha.Util/YiSha.Util/FileHelper.cs
Expand Up @@ -178,16 +178,23 @@ public static TData<string> DeleteFile(int fileModule, string filePath)
/// <returns></returns>
public static TData<FileContentResult> DownloadFile(string filePath, int delete)
{
filePath = filePath.Replace("../", string.Empty);
filePath = filePath.TrimStart('/');
if (!filePath.StartsWith("wwwroot") && !filePath.StartsWith("Resource"))
{
throw new Exception("非法访问");
}
TData<FileContentResult> obj = new TData<FileContentResult>();
string absoluteFilePath = GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
byte[] fileBytes = File.ReadAllBytes(absoluteFilePath);
if (delete == 1)
{
File.Delete(absoluteFilePath);
}
// md5 值
string fileNamePrefix = DateTime.Now.ToString("yyyyMMddHHmmss");
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string title = string.Empty;
string title = fileNameWithoutExtension;
if (fileNameWithoutExtension.Contains("_"))
{
title = fileNameWithoutExtension.Split('_')[1].Trim();
Expand Down

0 comments on commit c3b66e6

Please sign in to comment.