|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- 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
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:陈彬彬
- /// 日 期:2017.03.08
- /// 描 述:附件管理
- /// </summary>
- public class AnnexesFileBLL : AnnexesFileIBLL
- {
- AnnexesFileService annexesFileService = new AnnexesFileService();
- /*缓存文件分片信息*/
- private ICache cache = CacheFactory.CaChe();
- private string cacheKey = "Learun_adms_annexes_";
- #region 获取数据
- /// <summary>
- /// 获取实体列表
- /// </summary>
- /// <param name="keyValue">附件夹主键</param>
- /// <returns></returns>
- public IEnumerable<AnnexesFileEntity> GetList(string keyValue)
- {
- try
- {
- return annexesFileService.GetList(keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 获取附件名称集合
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <returns></returns>
- public string GetFileNames(string keyValue)
- {
- try
- {
- return annexesFileService.GetFileNames(keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 获取附件实体
- /// </summary>
- /// <param name="keyValue">主键</param>
- /// <returns></returns>
- 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 提交数据
- /// <summary>
- /// 保存数据实体
- /// </summary>
- /// <param name="folderId">附件夹主键</param>
- /// <param name="annexesFileEntity">附件实体数据</param>
- 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);
- }
- }
- }
- /// <summary>
- /// 删除附件
- /// </summary>
- /// <param name="fileId">文件主键</param>
- public void DeleteEntity(string fileId)
- {
- try
- {
- annexesFileService.DeleteEntity(fileId);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- #endregion
-
- #region 扩展方法
- /// <summary>
- /// 保存附件(支持大文件分片传输)
- /// </summary>
- /// <param name="folderId">附件夹主键</param>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="fileName">文件名称</param>
- /// <param name="chunks">文件总共分多少片</param>
- /// <param name="fileStream">文件二进制流</param>
- /// <param name="userInfo"></param>
- /// <param name="filePath"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
-
- /// <summary>
- /// 保存附件(支持大文件分片传输)
- /// </summary>
- /// <param name="folderId">附件夹主键</param>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="fileName">文件名称</param>
- /// <param name="chunks">文件总共分多少片</param>
- /// <param name="fileStream">文件二进制流</param>
- /// <returns></returns>
- 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);
- }
- }
- }
-
-
- /// <summary>
- /// 保存附件(支持大文件分片传输)
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="fileName">文件名称</param>
- /// <param name="chunks">文件总共分多少片</param>
- /// <param name="fileStream">文件二进制流</param>
- /// <returns></returns>
- 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);
- }
- }
- }
- /// <summary>
- /// 保存附件到文件中
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="filePath">文件路径</param>
- /// <param name="chunks">总共分片数</param>
- /// <param name="buffer">文件二进制流</param>
- /// <returns>-1:表示保存失败</returns>
- 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<byte[]>(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);
- }
- }
- }
- /// <summary>
- /// 保存分片附件
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="chunk">分片文件序号</param>
- /// <param name="fileStream">文件流</param>
- public void SaveChunkAnnexes(string fileGuid, int chunk, Stream fileStream)
- {
- try
- {
- byte[] bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- cache.Write<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes, CacheId.annexes);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowBusinessException(ex);
- }
- }
- }
- /// <summary>
- /// 移除文件分片数据
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="chunks">文件分片数</param>
- 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
- }
- }
|