using System.IO;
namespace learun.util
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.18
/// 描 述:文件帮助类
///
public static class FileHelper
{
///
/// 读取根目录下面的文件
///
/// 文件路径
///
public static byte[] ReadRoot(string filePath)
{
string rootPath = ConfigHelper.GetValue("baseDir") + "/wwwroot";
string path = rootPath + filePath;
return File.ReadAllBytes(path);
}
///
/// 读取缓存文件
///
/// 文件路径
///
public static byte[] ReadCache(string fileName)
{
string rootPath = ConfigHelper.GetValue("baseDir") + "/cache/";
string path = rootPath + fileName;
return File.ReadAllBytes(path);
}
///
/// 读取缓存文件
///
/// 文件路径
/// 文件数据
///
public static void WriteCache(string fileName, byte[] buffer)
{
string rootPath = ConfigHelper.GetValue("baseDir") + "/cache/";
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
string path = rootPath + fileName;
FileInfo file = new FileInfo(path);
FileStream fs = file.Create();
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
///
/// 读取缓存文件
///
/// 文件路径
///
public static void RemoveCache(string fileName)
{
string rootPath = ConfigHelper.GetValue("baseDir") + "/cache/";
string path = rootPath + fileName;
File.Delete(path);
}
///
/// 读取文件
///
/// 文件绝对路径
///
public static byte[] Read(string filePath)
{
return File.ReadAllBytes(filePath);
}
///
/// 获取文件的传输类型
///
/// 文件扩展名
///
public static string getContentType(string fileExt)
{
string contentType = "";
switch (fileExt?.ToLower())
{
case "jpg":
case "jpeg":
case "gif":
case "png":
case "webp":
contentType = "image/" + fileExt.ToLower();
break;
case "bmp":
contentType = "application/x-bmp";
break;
case "pdf":
contentType = "application/" + fileExt.ToLower();
break;
case "txt":
contentType = "text/plain";
break;
case "csv":
contentType = "";
break;
case "html":
contentType = "text/html";
break;
default:
contentType = "application/octet-stream";
break;
}
return contentType;
}
}
}