using Learun.Cache.Base;
using Learun.Cache.Factory;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
namespace Learun.Application.Base.SystemModule
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:附件管理
///
public class AnnexesFileBLL : AnnexesFileIBLL
{
AnnexesFileService annexesFileService = new AnnexesFileService();
/*缓存文件分片信息*/
private ICache cache = CacheFactory.CaChe();
private string cacheKey = "Learun_adms_annexes_";
#region 获取数据
///
/// 获取实体列表
///
/// 附件夹主键
///
public IEnumerable GetList(string keyValue)
{
try
{
return annexesFileService.GetList(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取附件名称集合
///
/// 主键值
///
public string GetFileNames(string keyValue)
{
try
{
return annexesFileService.GetFileNames(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取附件实体
///
/// 主键
///
public AnnexesFileEntity GetEntity(string keyValue)
{
try
{
return annexesFileService.GetEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public AnnexesFileEntity GetEntityByFolderId(string folderId)
{
try
{
return annexesFileService.GetEntityByFolderId(folderId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 保存数据实体
///
/// 附件夹主键
/// 附件实体数据
public void SaveEntity(string folderId, AnnexesFileEntity annexesFileEntity)
{
try
{
annexesFileService.SaveEntity(folderId, annexesFileEntity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 删除附件
///
/// 文件主键
public void DeleteEntity(string fileId)
{
try
{
annexesFileService.DeleteEntity(fileId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 扩展方法
///
/// 保存附件(支持大文件分片传输)
///
/// 附件夹主键
/// 文件主键
/// 文件名称
/// 文件总共分多少片
/// 文件二进制流
///
///
///
public bool SaveAnnexes(string folderId, string fileGuid, string fileName, int chunks, UserInfo userInfo, string filePath = "")
{
try
{
//获取文件完整文件名(包含绝对路径)
//文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
string virtualPath = "";
string uploadDate = DateTime.Now.ToString("yyyyMMdd");
string FileEextension = Path.GetExtension(fileName);
if (string.IsNullOrEmpty(filePath))
{
filePath = Config.GetValue("AnnexesFile");
virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
}
else
{
virtualPath = string.Format("{0}/{1}", filePath, fileName);
}
//创建文件夹
string path = Path.GetDirectoryName(virtualPath);
Directory.CreateDirectory(path);
AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
if (!System.IO.File.Exists(virtualPath))
{
long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
if (filesize == -1)// 表示保存失败
{
RemoveChunkAnnexes(fileGuid, chunks);
return false;
}
//文件信息写入数据库
fileAnnexesEntity.F_Id = fileGuid;
fileAnnexesEntity.F_FileName = fileName;
fileAnnexesEntity.F_FilePath = virtualPath;
fileAnnexesEntity.F_FileSize = filesize.ToString();
fileAnnexesEntity.F_FileExtensions = FileEextension;
fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
fileAnnexesEntity.F_CreateUserId = userInfo.userId;
fileAnnexesEntity.F_CreateUserName = userInfo.realName;
SaveEntity(folderId, fileAnnexesEntity);
}
return true;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存附件(支持大文件分片传输)
///
/// 附件夹主键
/// 文件主键
/// 文件名称
/// 文件总共分多少片
/// 文件二进制流
///
public string SaveAnnexesInfo(HttpPostedFileBase Filedata)
{
try
{
//获取文件完整文件名(包含绝对路径)
//文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
string fileGuid = Guid.NewGuid().ToString();
string folderId = Guid.NewGuid().ToString();
Stream fileStream = Filedata.InputStream;
string filePath = Config.GetValue("AnnexesFile");
string uploadDate = DateTime.Now.ToString("yyyyMMdd");
string FileEextension = Path.GetExtension(Filedata.FileName);
string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, "headImg", uploadDate, fileGuid, FileEextension);
//创建文件夹
string path = Path.GetDirectoryName(virtualPath);
Directory.CreateDirectory(path);
AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
if (!System.IO.File.Exists(virtualPath))
{
long filesize = 0;
FileInfo file = new FileInfo(virtualPath);
//创建文件
FileStream fs = file.Create();
byte[] bufferByRedis = new byte[fileStream.Length];
fileStream.Read(bufferByRedis, 0, bufferByRedis.Length);
if (bufferByRedis == null)
{
filesize = -1;
}
//写入二进制流
fs.Write(bufferByRedis, 0, bufferByRedis.Length);
filesize += bufferByRedis.Length;
//关闭文件流
fs.Close();
if (filesize == -1)// 表示保存失败
{
return "";
}
//文件信息写入数据库
fileAnnexesEntity.F_Id = fileGuid;
fileAnnexesEntity.F_FileName = Filedata.FileName;
fileAnnexesEntity.F_FilePath = virtualPath;
fileAnnexesEntity.F_FileSize = filesize.ToString();
fileAnnexesEntity.F_FileExtensions = FileEextension;
fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
fileAnnexesEntity.F_CreateUserId = "";
fileAnnexesEntity.F_CreateUserName = "";
SaveEntity(folderId, fileAnnexesEntity);
}
return folderId;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存附件(支持大文件分片传输)
///
/// 文件主键
/// 文件名称
/// 文件总共分多少片
/// 文件二进制流
///
public string SaveAnnexes(string fileGuid, string fileName, int chunks, UserInfo userInfo)
{
try
{
//获取文件完整文件名(包含绝对路径)
//文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
string filePath = Config.GetValue("AnnexesFile");
string uploadDate = DateTime.Now.ToString("yyyyMMdd");
string FileEextension = Path.GetExtension(fileName);
string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.account, uploadDate, fileGuid, FileEextension);
//创建文件夹
string path = Path.GetDirectoryName(virtualPath);
Directory.CreateDirectory(path);
AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
if (!System.IO.File.Exists(virtualPath))
{
long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
if (filesize == -1)// 表示保存失败
{
RemoveChunkAnnexes(fileGuid, chunks);
return "";
}
}
return virtualPath;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存附件到文件中
///
/// 文件主键
/// 文件路径
/// 总共分片数
/// 文件二进制流
/// -1:表示保存失败
public long SaveAnnexesToFile(string fileGuid, string filePath, int chunks)
{
try
{
long filesize = 0;
//创建一个FileInfo对象
FileInfo file = new FileInfo(filePath);
//创建文件
FileStream fs = file.Create();
for (int i = 0; i < chunks; i++)
{
byte[] bufferByRedis = cache.Read(cacheKey + i + "_" + fileGuid, CacheId.annexes);
if (bufferByRedis == null)
{
return -1;
}
//写入二进制流
fs.Write(bufferByRedis, 0, bufferByRedis.Length);
filesize += bufferByRedis.Length;
cache.Remove(cacheKey + i + "_" + fileGuid, CacheId.annexes);
}
//关闭文件流
fs.Close();
return filesize;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存分片附件
///
/// 文件主键
/// 分片文件序号
/// 文件流
public void SaveChunkAnnexes(string fileGuid, int chunk, Stream fileStream)
{
try
{
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
cache.Write(cacheKey + chunk + "_" + fileGuid, bytes, CacheId.annexes);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 移除文件分片数据
///
/// 文件主键
/// 文件分片数
public void RemoveChunkAnnexes(string fileGuid, int chunks)
{
try
{
for (int i = 0; i < chunks; i++)
{
cache.Remove(cacheKey + i + "_" + fileGuid, CacheId.annexes);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public void SaveEntityByKey(string fId, AnnexesFileEntity annexesFileEntity)
{
try
{
annexesFileService.SaveEntityByKey(fId, annexesFileEntity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
}
}