You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

480 lines
17 KiB

  1. using Learun.Cache.Base;
  2. using Learun.Cache.Factory;
  3. using Learun.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Web;
  8. namespace Learun.Application.Base.SystemModule
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:陈彬彬
  14. /// 日 期:2017.03.08
  15. /// 描 述:附件管理
  16. /// </summary>
  17. public class AnnexesFileBLL : AnnexesFileIBLL
  18. {
  19. AnnexesFileService annexesFileService = new AnnexesFileService();
  20. /*缓存文件分片信息*/
  21. private ICache cache = CacheFactory.CaChe();
  22. private string cacheKey = "Learun_adms_annexes_";
  23. #region 获取数据
  24. /// <summary>
  25. /// 获取实体列表
  26. /// </summary>
  27. /// <param name="keyValue">附件夹主键</param>
  28. /// <returns></returns>
  29. public IEnumerable<AnnexesFileEntity> GetList(string keyValue)
  30. {
  31. try
  32. {
  33. return annexesFileService.GetList(keyValue);
  34. }
  35. catch (Exception ex)
  36. {
  37. if (ex is ExceptionEx)
  38. {
  39. throw;
  40. }
  41. else
  42. {
  43. throw ExceptionEx.ThrowBusinessException(ex);
  44. }
  45. }
  46. }
  47. /// <summary>
  48. /// 获取附件名称集合
  49. /// </summary>
  50. /// <param name="keyValue">主键值</param>
  51. /// <returns></returns>
  52. public string GetFileNames(string keyValue)
  53. {
  54. try
  55. {
  56. return annexesFileService.GetFileNames(keyValue);
  57. }
  58. catch (Exception ex)
  59. {
  60. if (ex is ExceptionEx)
  61. {
  62. throw;
  63. }
  64. else
  65. {
  66. throw ExceptionEx.ThrowBusinessException(ex);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 获取附件实体
  72. /// </summary>
  73. /// <param name="keyValue">主键</param>
  74. /// <returns></returns>
  75. public AnnexesFileEntity GetEntity(string keyValue)
  76. {
  77. try
  78. {
  79. return annexesFileService.GetEntity(keyValue);
  80. }
  81. catch (Exception ex)
  82. {
  83. if (ex is ExceptionEx)
  84. {
  85. throw;
  86. }
  87. else
  88. {
  89. throw ExceptionEx.ThrowBusinessException(ex);
  90. }
  91. }
  92. }
  93. public AnnexesFileEntity GetEntityByFolderId(string folderId)
  94. {
  95. try
  96. {
  97. return annexesFileService.GetEntityByFolderId(folderId);
  98. }
  99. catch (Exception ex)
  100. {
  101. if (ex is ExceptionEx)
  102. {
  103. throw;
  104. }
  105. else
  106. {
  107. throw ExceptionEx.ThrowBusinessException(ex);
  108. }
  109. }
  110. }
  111. #endregion
  112. #region 提交数据
  113. /// <summary>
  114. /// 保存数据实体
  115. /// </summary>
  116. /// <param name="folderId">附件夹主键</param>
  117. /// <param name="annexesFileEntity">附件实体数据</param>
  118. public void SaveEntity(string folderId, AnnexesFileEntity annexesFileEntity)
  119. {
  120. try
  121. {
  122. annexesFileService.SaveEntity(folderId, annexesFileEntity);
  123. }
  124. catch (Exception ex)
  125. {
  126. if (ex is ExceptionEx)
  127. {
  128. throw;
  129. }
  130. else
  131. {
  132. throw ExceptionEx.ThrowBusinessException(ex);
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// 删除附件
  138. /// </summary>
  139. /// <param name="fileId">文件主键</param>
  140. public void DeleteEntity(string fileId)
  141. {
  142. try
  143. {
  144. annexesFileService.DeleteEntity(fileId);
  145. }
  146. catch (Exception ex)
  147. {
  148. if (ex is ExceptionEx)
  149. {
  150. throw;
  151. }
  152. else
  153. {
  154. throw ExceptionEx.ThrowBusinessException(ex);
  155. }
  156. }
  157. }
  158. #endregion
  159. #region 扩展方法
  160. /// <summary>
  161. /// 保存附件(支持大文件分片传输)
  162. /// </summary>
  163. /// <param name="folderId">附件夹主键</param>
  164. /// <param name="fileGuid">文件主键</param>
  165. /// <param name="fileName">文件名称</param>
  166. /// <param name="chunks">文件总共分多少片</param>
  167. /// <param name="fileStream">文件二进制流</param>
  168. /// <param name="userInfo"></param>
  169. /// <param name="filePath"></param>
  170. /// <returns></returns>
  171. public bool SaveAnnexes(string folderId, string fileGuid, string fileName, int chunks, UserInfo userInfo, string filePath = "")
  172. {
  173. try
  174. {
  175. //获取文件完整文件名(包含绝对路径)
  176. //文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
  177. string virtualPath = "";
  178. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  179. string FileEextension = Path.GetExtension(fileName);
  180. if (string.IsNullOrEmpty(filePath))
  181. {
  182. filePath = Config.GetValue("AnnexesFile");
  183. virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
  184. }
  185. else
  186. {
  187. virtualPath = string.Format("{0}/{1}", filePath, fileName);
  188. }
  189. //创建文件夹
  190. string path = Path.GetDirectoryName(virtualPath);
  191. Directory.CreateDirectory(path);
  192. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  193. if (!System.IO.File.Exists(virtualPath))
  194. {
  195. long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
  196. if (filesize == -1)// 表示保存失败
  197. {
  198. RemoveChunkAnnexes(fileGuid, chunks);
  199. return false;
  200. }
  201. //文件信息写入数据库
  202. fileAnnexesEntity.F_Id = fileGuid;
  203. fileAnnexesEntity.F_FileName = fileName;
  204. fileAnnexesEntity.F_FilePath = virtualPath;
  205. fileAnnexesEntity.F_FileSize = filesize.ToString();
  206. fileAnnexesEntity.F_FileExtensions = FileEextension;
  207. fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
  208. fileAnnexesEntity.F_CreateUserId = userInfo.userId;
  209. fileAnnexesEntity.F_CreateUserName = userInfo.realName;
  210. SaveEntity(folderId, fileAnnexesEntity);
  211. }
  212. return true;
  213. }
  214. catch (Exception ex)
  215. {
  216. if (ex is ExceptionEx)
  217. {
  218. throw;
  219. }
  220. else
  221. {
  222. throw ExceptionEx.ThrowBusinessException(ex);
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// 保存附件(支持大文件分片传输)
  228. /// </summary>
  229. /// <param name="folderId">附件夹主键</param>
  230. /// <param name="fileGuid">文件主键</param>
  231. /// <param name="fileName">文件名称</param>
  232. /// <param name="chunks">文件总共分多少片</param>
  233. /// <param name="fileStream">文件二进制流</param>
  234. /// <returns></returns>
  235. public string SaveAnnexesInfo(HttpPostedFileBase Filedata)
  236. {
  237. try
  238. {
  239. //获取文件完整文件名(包含绝对路径)
  240. //文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
  241. string fileGuid = Guid.NewGuid().ToString();
  242. string folderId = Guid.NewGuid().ToString();
  243. Stream fileStream = Filedata.InputStream;
  244. string filePath = Config.GetValue("AnnexesFile");
  245. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  246. string FileEextension = Path.GetExtension(Filedata.FileName);
  247. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, "headImg", uploadDate, fileGuid, FileEextension);
  248. //创建文件夹
  249. string path = Path.GetDirectoryName(virtualPath);
  250. Directory.CreateDirectory(path);
  251. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  252. if (!System.IO.File.Exists(virtualPath))
  253. {
  254. long filesize = 0;
  255. FileInfo file = new FileInfo(virtualPath);
  256. //创建文件
  257. FileStream fs = file.Create();
  258. byte[] bufferByRedis = new byte[fileStream.Length];
  259. fileStream.Read(bufferByRedis, 0, bufferByRedis.Length);
  260. if (bufferByRedis == null)
  261. {
  262. filesize = -1;
  263. }
  264. //写入二进制流
  265. fs.Write(bufferByRedis, 0, bufferByRedis.Length);
  266. filesize += bufferByRedis.Length;
  267. //关闭文件流
  268. fs.Close();
  269. if (filesize == -1)// 表示保存失败
  270. {
  271. return "";
  272. }
  273. //文件信息写入数据库
  274. fileAnnexesEntity.F_Id = fileGuid;
  275. fileAnnexesEntity.F_FileName = Filedata.FileName;
  276. fileAnnexesEntity.F_FilePath = virtualPath;
  277. fileAnnexesEntity.F_FileSize = filesize.ToString();
  278. fileAnnexesEntity.F_FileExtensions = FileEextension;
  279. fileAnnexesEntity.F_FileType = FileEextension.Replace(".", "");
  280. fileAnnexesEntity.F_CreateUserId = "";
  281. fileAnnexesEntity.F_CreateUserName = "";
  282. SaveEntity(folderId, fileAnnexesEntity);
  283. }
  284. return folderId;
  285. }
  286. catch (Exception ex)
  287. {
  288. if (ex is ExceptionEx)
  289. {
  290. throw;
  291. }
  292. else
  293. {
  294. throw ExceptionEx.ThrowBusinessException(ex);
  295. }
  296. }
  297. }
  298. /// <summary>
  299. /// 保存附件(支持大文件分片传输)
  300. /// </summary>
  301. /// <param name="fileGuid">文件主键</param>
  302. /// <param name="fileName">文件名称</param>
  303. /// <param name="chunks">文件总共分多少片</param>
  304. /// <param name="fileStream">文件二进制流</param>
  305. /// <returns></returns>
  306. public string SaveAnnexes(string fileGuid, string fileName, int chunks, UserInfo userInfo)
  307. {
  308. try
  309. {
  310. //获取文件完整文件名(包含绝对路径)
  311. //文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
  312. string filePath = Config.GetValue("AnnexesFile");
  313. string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  314. string FileEextension = Path.GetExtension(fileName);
  315. string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.account, uploadDate, fileGuid, FileEextension);
  316. //创建文件夹
  317. string path = Path.GetDirectoryName(virtualPath);
  318. Directory.CreateDirectory(path);
  319. AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  320. if (!System.IO.File.Exists(virtualPath))
  321. {
  322. long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
  323. if (filesize == -1)// 表示保存失败
  324. {
  325. RemoveChunkAnnexes(fileGuid, chunks);
  326. return "";
  327. }
  328. }
  329. return virtualPath;
  330. }
  331. catch (Exception ex)
  332. {
  333. if (ex is ExceptionEx)
  334. {
  335. throw;
  336. }
  337. else
  338. {
  339. throw ExceptionEx.ThrowBusinessException(ex);
  340. }
  341. }
  342. }
  343. /// <summary>
  344. /// 保存附件到文件中
  345. /// </summary>
  346. /// <param name="fileGuid">文件主键</param>
  347. /// <param name="filePath">文件路径</param>
  348. /// <param name="chunks">总共分片数</param>
  349. /// <param name="buffer">文件二进制流</param>
  350. /// <returns>-1:表示保存失败</returns>
  351. public long SaveAnnexesToFile(string fileGuid, string filePath, int chunks)
  352. {
  353. try
  354. {
  355. long filesize = 0;
  356. //创建一个FileInfo对象
  357. FileInfo file = new FileInfo(filePath);
  358. //创建文件
  359. FileStream fs = file.Create();
  360. for (int i = 0; i < chunks; i++)
  361. {
  362. byte[] bufferByRedis = cache.Read<byte[]>(cacheKey + i + "_" + fileGuid, CacheId.annexes);
  363. if (bufferByRedis == null)
  364. {
  365. return -1;
  366. }
  367. //写入二进制流
  368. fs.Write(bufferByRedis, 0, bufferByRedis.Length);
  369. filesize += bufferByRedis.Length;
  370. cache.Remove(cacheKey + i + "_" + fileGuid, CacheId.annexes);
  371. }
  372. //关闭文件流
  373. fs.Close();
  374. return filesize;
  375. }
  376. catch (Exception ex)
  377. {
  378. if (ex is ExceptionEx)
  379. {
  380. throw;
  381. }
  382. else
  383. {
  384. throw ExceptionEx.ThrowBusinessException(ex);
  385. }
  386. }
  387. }
  388. /// <summary>
  389. /// 保存分片附件
  390. /// </summary>
  391. /// <param name="fileGuid">文件主键</param>
  392. /// <param name="chunk">分片文件序号</param>
  393. /// <param name="fileStream">文件流</param>
  394. public void SaveChunkAnnexes(string fileGuid, int chunk, Stream fileStream)
  395. {
  396. try
  397. {
  398. byte[] bytes = new byte[fileStream.Length];
  399. fileStream.Read(bytes, 0, bytes.Length);
  400. cache.Write<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes, CacheId.annexes);
  401. }
  402. catch (Exception ex)
  403. {
  404. if (ex is ExceptionEx)
  405. {
  406. throw;
  407. }
  408. else
  409. {
  410. throw ExceptionEx.ThrowBusinessException(ex);
  411. }
  412. }
  413. }
  414. /// <summary>
  415. /// 移除文件分片数据
  416. /// </summary>
  417. /// <param name="fileGuid">文件主键</param>
  418. /// <param name="chunks">文件分片数</param>
  419. public void RemoveChunkAnnexes(string fileGuid, int chunks)
  420. {
  421. try
  422. {
  423. for (int i = 0; i < chunks; i++)
  424. {
  425. cache.Remove(cacheKey + i + "_" + fileGuid, CacheId.annexes);
  426. }
  427. }
  428. catch (Exception ex)
  429. {
  430. if (ex is ExceptionEx)
  431. {
  432. throw;
  433. }
  434. else
  435. {
  436. throw ExceptionEx.ThrowBusinessException(ex);
  437. }
  438. }
  439. }
  440. public void SaveEntityByKey(string fId, AnnexesFileEntity annexesFileEntity)
  441. {
  442. try
  443. {
  444. annexesFileService.SaveEntityByKey(fId, annexesFileEntity);
  445. }
  446. catch (Exception ex)
  447. {
  448. if (ex is ExceptionEx)
  449. {
  450. throw;
  451. }
  452. else
  453. {
  454. throw ExceptionEx.ThrowBusinessException(ex);
  455. }
  456. }
  457. }
  458. #endregion
  459. }
  460. }