ProcurementAppService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Procurement.Enums;
  2. using Procurement.FileManagement.Dto;
  3. using Procurement.Models;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel.DataAnnotations;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Linq.Dynamic.Core;
  13. using System.Threading.Tasks;
  14. using Volo.Abp;
  15. using Volo.Abp.Application.Dtos;
  16. using Volo.Abp.Application.Services;
  17. using Volo.Abp.Domain.Repositories;
  18. using FileInfo = Procurement.Models.FileInfo;
  19. using Volo.Abp.MultiTenancy;
  20. using Procurement.Core;
  21. namespace Procurement.FileManagement
  22. {
  23. public class ProcurementAppService : ApplicationService, IProcurementAppService
  24. {
  25. private readonly ICurrentTenant _currentTenant;
  26. private readonly IRepository<FileInfo, Guid> _repository;
  27. private readonly FileManager _fileManager;
  28. string[] pictureFormatArray = { ".png", ".jpg", ".jpeg", ".gif", ".PNG", ".JPG", ".JPEG", ".GIF" };
  29. public ProcurementAppService(IRepository<FileInfo, Guid> repository, FileManager fileManager, ICurrentTenant currentTenant)
  30. {
  31. _repository = repository;
  32. _fileManager = fileManager;
  33. _currentTenant = currentTenant;
  34. }
  35. public async Task<PagedResultDto<FileInfoDto>> GetAll(GetFileInputDto input)
  36. {
  37. var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), _ => _.Name.Contains(input.Filter));
  38. var totalCount = await query.CountAsync();
  39. var items = await query.OrderBy(input.Sorting ?? "Name")
  40. .Skip(input.SkipCount)
  41. .Take(input.MaxResultCount)
  42. .ToListAsync();
  43. var dtos = ObjectMapper.Map<List<FileInfo>, List<FileInfoDto>>(items);
  44. return new PagedResultDto<FileInfoDto>(totalCount, dtos);
  45. }
  46. public async Task<FileInfoDto> Upload([Required] string name, [Required] IFormFile file)
  47. {
  48. if (file == null || file.Length == 0) throw new BusinessException("无法上传空文件");
  49. //限制100M
  50. if (file.Length > 104857600)
  51. {
  52. //new NLogHelper("ProcurementAppService").WriteLog("Upload", "上传文件过大",NLog.LogLevel.Error,_currentTenant.Id.ToString());
  53. //NLogHelper.Default.WriteLog("Upload", "上传文件过大", _currentTenant.Id.ToString());
  54. throw new BusinessException("上传文件过大");
  55. }
  56. //文件格式
  57. var fileExtension = Path.GetExtension(file.FileName);
  58. if (!pictureFormatArray.Contains(fileExtension))
  59. {
  60. throw new BusinessException("上传文件格式错误");
  61. }
  62. var size = "";
  63. if (file.Length < 1024)
  64. size = file.Length.ToString() + "B";
  65. else if (file.Length >= 1024 && file.Length < 1048576)
  66. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  67. else if (file.Length >= 1048576 && file.Length < 104857600)
  68. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  69. else size = file.Length.ToString() + "B";
  70. string uploadsFolder = Path.Combine(Environment.CurrentDirectory, "wwwroot", "files");
  71. if (!Directory.Exists(uploadsFolder))
  72. {
  73. Directory.CreateDirectory(uploadsFolder);
  74. }
  75. var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  76. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  77. using (var fileStream = new FileStream(filePath, FileMode.Create))
  78. {
  79. file.CopyTo(fileStream);
  80. fileStream.Flush();
  81. }
  82. //TODO:文件md5哈希校验
  83. var result = await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/files/" + uniqueFileName, FileType.Image);
  84. return ObjectMapper.Map<FileInfo, FileInfoDto>(result);
  85. }
  86. public async Task UploadPrivate([Required] string name, [Required] IFormFile file)
  87. {
  88. if (file == null || file.Length == 0) throw new BusinessException("无法上传空文件");
  89. if (file.Length > 104857600) throw new BusinessException("上传文件过大");
  90. var fileExtension = Path.GetExtension(file.FileName);
  91. if (!pictureFormatArray.Contains(fileExtension)) throw new BusinessException("上传文件格式错误");
  92. var size = "";
  93. if (file.Length < 1024)
  94. size = file.Length.ToString() + "B";
  95. else if (file.Length >= 1024 && file.Length < 1048576)
  96. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  97. else if (file.Length >= 1048576 && file.Length < 104857600)
  98. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  99. else size = file.Length.ToString() + "B";
  100. string uploadsFolder = Path.Combine(Environment.CurrentDirectory, "files");
  101. if (!Directory.Exists(uploadsFolder)) Directory.CreateDirectory(uploadsFolder);
  102. var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  103. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  104. using (var fileStream = new FileStream(filePath, FileMode.Create))
  105. {
  106. file.CopyTo(fileStream);
  107. fileStream.Flush();
  108. }
  109. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/files/" + uniqueFileName, FileType.Image);
  110. }
  111. public dynamic Download([Required] string name)
  112. {
  113. var filePath = Path.Combine(Environment.CurrentDirectory, "files", name);
  114. if (!File.Exists(filePath)) throw new BusinessException("找不到文件");
  115. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = name };
  116. }
  117. }
  118. }