using ce.autofac.extension; using learun.iapplication; using learun.util; using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace learun.application { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2019.09.25 /// 描 述:附件管理 /// public class AnnexesFileBLL : BLLBase, AnnexesFileIBLL, BLL { private readonly AnnexesFileService annexesFileService = new AnnexesFileService(); #region 获取数据 /// /// 获取实体列表 /// /// 附件夹主键 /// public Task> GetList(string folderId) { return annexesFileService.GetList(folderId); } /// /// 获取附件名称集合 /// /// 附件夹主键 /// public Task GetFileNames(string folderId) { return annexesFileService.GetFileNames(folderId); } /// /// 获取附件实体 /// /// 主键 /// public Task GetEntity(string keyValue) { return annexesFileService.GetEntity(keyValue); } #endregion 获取数据 #region 提交数据 /// /// 保存数据实体 /// /// 附件夹主键 /// 附件实体数据 public async Task SaveEntity(string folderId, AnnexesFileEntity annexesFileEntity) { await annexesFileService.SaveEntity(folderId, annexesFileEntity); } /// /// 删除附件 /// /// 文件主键 public async Task DeleteEntity(string fileId) { await annexesFileService.DeleteEntity(fileId); } #endregion 提交数据 #region 扩展方法 /// /// 保存附件(支持大文件分片传输) /// /// 附件夹主键 /// 文件主键 /// 文件名称 /// 文件总共分多少片 /// public async Task SaveAnnexes(string folderId, string fileGuid, string fileName, int chunks) { //获取文件完整文件名(包含绝对路径) //文件存放路径格式:/{account}/yyyymmdd/{guid}.{后缀名} string filePath = ConfigHelper.GetConfig().AnnexesFile; ; string FileEextension = Path.GetExtension(fileName); string uploadDate = DateTime.Now.ToString("yyyyMMdd"); string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, this.GetUserAccount(), uploadDate, fileGuid, FileEextension); //创建文件夹 string path = Path.GetDirectoryName(virtualPath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity(); if (!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 = this.GetUserId(); fileAnnexesEntity.F_CreateUserName = this.GetUserName(); await SaveEntity(folderId, fileAnnexesEntity); } return true; } /// /// 保存附件(支持大文件分片传输) /// /// 文件主键 /// 文件名称 /// 文件总共分多少片 /// public string SaveAnnexes(string fileGuid, string fileName, int chunks) { //获取文件完整文件名(包含绝对路径) //文件存放路径格式:/Resource/Temp/{date}/{guid}.{后缀名} string filePath = ConfigHelper.GetValue("baseDir") + "/Resource/Temp/"; string FileEextension = Path.GetExtension(fileName); string virtualPath = string.Format("{0}/{1}{2}", filePath, fileGuid, FileEextension); //创建文件夹 string path = Path.GetDirectoryName(virtualPath); Directory.CreateDirectory(path); if (!File.Exists(virtualPath)) { long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks); if (filesize == -1)// 表示保存失败 { RemoveChunkAnnexes(fileGuid, chunks); return ""; } } return virtualPath; } /// /// 保存附件到文件中 /// /// 文件主键 /// 文件路径 /// 总共分片数 /// -1:表示保存失败 public long SaveAnnexesToFile(string fileGuid, string filePath, int chunks) { long filesize = 0; //创建一个FileInfo对象 FileInfo file = new FileInfo(filePath); //创建文件 FileStream fs = file.Create(); for (int i = 0; i < chunks; i++) { byte[] bufferByRedis = FileHelper.ReadCache(i + "_" + fileGuid); if (bufferByRedis == null) { return -1; } //写入二进制流 fs.Write(bufferByRedis, 0, bufferByRedis.Length); filesize += bufferByRedis.Length; FileHelper.RemoveCache(i + "_" + fileGuid); } //关闭文件流 fs.Close(); return filesize; } /// /// 保存分片附件 /// /// 文件主键 /// 分片文件序号 /// 文件流 public void SaveChunkAnnexes(string fileGuid, int chunk, byte[] bytes) { FileHelper.WriteCache(chunk + "_" + fileGuid, bytes); } /// /// 移除文件分片数据 /// /// 文件主键 /// 文件分片数 public void RemoveChunkAnnexes(string fileGuid, int chunks) { for (int i = 0; i < chunks; i++) { FileHelper.RemoveCache(i + "_" + fileGuid); } } #endregion 扩展方法 } }