using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Untity;
namespace IStation.Application
{
///
/// 处理文件辅助类
///
public partial class FileHelper
{
#region Upload
///
/// 上传文件 文件流形式
///
///
///
///
public static string UploadFile(Stream stream, string suffix)
{
var root = ConfigHelper.DataPath;
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
var guid = GuidCreater.CreateN();
var fileName = guid + suffix;
var filePath = Path.Combine(root, fileName);
StreamTransfer.ToFile(stream, filePath);
return fileName;
}
///
/// 上传到子文件夹文件 文件流形式
///
/// 子文件夹
/// 文件拓展名
/// 文件流
///
public static string UploadSubFile(string sub, Stream stream, string suffix)
{
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
var guid = GuidCreater.CreateN();
var fileName = guid + suffix;
var filePath = Path.Combine(root, fileName);
StreamTransfer.ToFile(stream, filePath);
return fileName;
}
///
/// 上传文件 字节形式
///
/// 字节流
/// 文件拓展名
///
public static string UploadFile(byte[] bytes, string suffix)
{
string root = ConfigHelper.DataPath;
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
var guid = GuidCreater.CreateN();
var fileName = guid + suffix;
var filePath = Path.Combine(root, fileName);
System.IO.File.WriteAllBytes(filePath, bytes);
return fileName;
}
///
/// 上传到子文件夹文件 字节形式
///
/// 子文件夹名
/// 文件拓展名
/// 字节数组
///
public static string UploadSubFile(string sub, byte[] bytes, string suffix)
{
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
var guid = GuidCreater.CreateN();
var fileName = guid + suffix;
var filePath = Path.Combine(root, fileName);
System.IO.File.WriteAllBytes(filePath, bytes);
return fileName;
}
#endregion
#region Download
///
/// 下载文件 文件流
///
/// 文件名称
///
public static Stream DownloadFile(string fileName)
{
var root = ConfigHelper.DataPath;
var filePath = Path.Combine(root, fileName);
if (System.IO.File.Exists(filePath))
{
return StreamTransfer.FromFile(filePath);
}
return null;
}
///
/// 下载文件 文件流方式
///
/// 子文件夹名
/// 文件名称
///
public static Stream DownloadSubFile(string sub, string fileName)
{
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
var filePath = Path.Combine(root, fileName);
if (System.IO.File.Exists(filePath))
{
return StreamTransfer.FromFile(filePath);
}
return default;
}
///
/// 下载文件 字节形式
///
/// 文件名称
///
public static byte[] DownloadFile2(string fileName)
{
var root = ConfigHelper.DataPath;
var filePath = Path.Combine(root, fileName);
if (System.IO.File.Exists(filePath))
{
return System.IO.File.ReadAllBytes(filePath);
}
return default;
}
///
/// 下载文件 字节形式
///
/// 子文件夹名
/// 文件名称
///
public static byte[] DownloadSubFile2(string sub, string fileName)
{
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
var filePath = Path.Combine(root, fileName);
if (System.IO.File.Exists(filePath))
{
return System.IO.File.ReadAllBytes(filePath);
}
return default;
}
#endregion
#region Update
///
/// 更新文件
///
/// 文件名称
/// 流
/// 文件后缀
///
public static string UpdateFile(string fileName, Stream stream, string suffix)
{
try
{
var code = UploadFile(stream, suffix);
if (!string.IsNullOrEmpty(code))
{
if (!string.IsNullOrEmpty(fileName))
Delete(fileName);
}
return code;
}
catch (Exception)
{
return default;
}
}
///
/// 更新文件
///
/// 老文件名
/// 文件后缀
/// 字节流
///
public static string UpdateFile2(string fileName, byte[] bytes, string suffix)
{
try
{
var code = UploadFile(bytes, suffix);
if (!string.IsNullOrEmpty(code))
{
if (!string.IsNullOrEmpty(fileName))
Delete(fileName);
}
return code;
}
catch (Exception)
{
return default;
}
}
///
/// 更新文件
///
/// 子文件夹
/// 老文件名
/// 文件后缀
/// 字节流
///
public static string UpdateSubFile(string sub, string fileName, Stream stream, string suffix)
{
try
{
var code = UploadSubFile(sub, stream, suffix);
if (!string.IsNullOrEmpty(code))
{
if (!string.IsNullOrEmpty(fileName))
Delete(sub, fileName);
}
return code;
}
catch (Exception)
{
return null;
}
}
///
/// 更新文件
///
/// 子文件夹
/// 老文件名
/// 文件后缀
/// 字节流
///
public static string UpdateSubFile2(string sub, string fileName, byte[] bytes, string suffix)
{
try
{
var code = UploadSubFile(sub, bytes, suffix);
if (!string.IsNullOrEmpty(code))
{
if (!string.IsNullOrEmpty(fileName))
Delete(sub, fileName);
}
return code;
}
catch (Exception)
{
return null;
}
}
#endregion
#region Delete
///
/// 删除文件
///
/// 文件名称
///
public static bool Delete(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
try
{
var root = ConfigHelper.DataPath;
var filePath = System.IO.Path.Combine(root, fileName);
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
}
catch (Exception)
{
return false;
}
return true;
}
///
/// 删除文件
///
/// 子文件名
/// 文件名称
///
public static bool Delete(string sub, string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
var filePath = Path.Combine(root, fileName);
try
{
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
}
catch (Exception)
{
return false;
}
return true;
}
#endregion
#region Path
///
/// 获取文件路径
///
public static string GetFilePath(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
var root = ConfigHelper.DataPath;
var filePath = System.IO.Path.Combine(root, fileName);
return filePath;
}
///
/// 获取文件相对路径
///
public static string GetRelatedFilePath(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
return $"{ConfigHelper.DataFolder}/{fileName}";
}
///
/// 获取文件路径
///
public static string GetFilePath(string sub, string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
var filePath = Path.Combine(root, fileName);
return filePath;
}
///
/// 获取文件路径2
///
public static string GetFilePath2(string sub, string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
string root;
if (string.IsNullOrEmpty(sub))
{
root = ConfigHelper.DataPath;
}
else
{
root = Path.Combine(ConfigHelper.DataPath, sub);
}
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
var filePath = Path.Combine(root, fileName);
return filePath;
}
///
/// 获取文件相对路径
///
public static string GetRelatedFilePath(string sub, string fileName)
{
if (string.IsNullOrEmpty(fileName))
return default;
if (string.IsNullOrEmpty(sub))
return GetRelatedFilePath(fileName);
return $"{ConfigHelper.DataFolder}/{sub}/{fileName}";
}
#endregion
}
}