DigitalManufacturingAppService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace DigitalManufacturing.DigitalManufacturingManagement
  20. {
  21. public class DigitalManufacturingAppService : ApplicationService, IDigitalManufacturingAppService
  22. {
  23. private readonly IRepository<FileInfo, Guid> _repository;
  24. private readonly FileManager _fileManager;
  25. string[] pictureFormatArray = { ".png", ".jpg", ".jpeg", ".gif", ".PNG", ".JPG", ".JPEG", ".GIF" };
  26. public DigitalManufacturingAppService(IRepository<FileInfo, Guid> repository, FileManager fileManager)
  27. {
  28. _repository = repository;
  29. _fileManager = fileManager;
  30. }
  31. public async Task<PagedResultDto<FileInfoDto>> GetAll(GetFileInputDto input)
  32. {
  33. var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), _ => _.Name.Contains(input.Filter));
  34. var totalCount = await query.CountAsync();
  35. var items = await query.OrderBy(input.Sorting ?? "Name")
  36. .Skip(input.SkipCount)
  37. .Take(input.MaxResultCount)
  38. .ToListAsync();
  39. var dtos = ObjectMapper.Map<List<FileInfo>, List<FileInfoDto>>(items);
  40. return new PagedResultDto<FileInfoDto>(totalCount, dtos);
  41. }
  42. public async Task<FileInfoDto> Upload([Required] string name, [Required] IFormFile file)
  43. {
  44. if (file == null || file.Length == 0) throw new BusinessException("无法上传空文件");
  45. //限制100M
  46. if (file.Length > 104857600)
  47. {
  48. throw new BusinessException("上传文件过大");
  49. }
  50. //文件格式
  51. var fileExtension = Path.GetExtension(file.FileName);
  52. if (!pictureFormatArray.Contains(fileExtension))
  53. {
  54. throw new BusinessException("上传文件格式错误");
  55. }
  56. var size = "";
  57. if (file.Length < 1024)
  58. size = file.Length.ToString() + "B";
  59. else if (file.Length >= 1024 && file.Length < 1048576)
  60. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  61. else if (file.Length >= 1048576 && file.Length < 104857600)
  62. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  63. else size = file.Length.ToString() + "B";
  64. string uploadsFolder = Path.Combine(Environment.CurrentDirectory, "wwwroot", "files");
  65. if (!Directory.Exists(uploadsFolder))
  66. {
  67. Directory.CreateDirectory(uploadsFolder);
  68. }
  69. var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  70. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  71. using (var fileStream = new FileStream(filePath, FileMode.Create))
  72. {
  73. file.CopyTo(fileStream);
  74. fileStream.Flush();
  75. }
  76. //TODO:文件md5哈希校验
  77. var result = await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/files/" + uniqueFileName, FileType.Image);
  78. return ObjectMapper.Map<FileInfo, FileInfoDto>(result);
  79. }
  80. public async Task UploadPrivate([Required] string name, [Required] IFormFile file)
  81. {
  82. if (file == null || file.Length == 0) throw new BusinessException("无法上传空文件");
  83. if (file.Length > 104857600) throw new BusinessException("上传文件过大");
  84. var fileExtension = Path.GetExtension(file.FileName);
  85. if (!pictureFormatArray.Contains(fileExtension)) throw new BusinessException("上传文件格式错误");
  86. var size = "";
  87. if (file.Length < 1024)
  88. size = file.Length.ToString() + "B";
  89. else if (file.Length >= 1024 && file.Length < 1048576)
  90. size = ((float)file.Length / 1024).ToString("F2") + "KB";
  91. else if (file.Length >= 1048576 && file.Length < 104857600)
  92. size = ((float)file.Length / 1024 / 1024).ToString("F2") + "MB";
  93. else size = file.Length.ToString() + "B";
  94. string uploadsFolder = Path.Combine(Environment.CurrentDirectory, "files");
  95. if (!Directory.Exists(uploadsFolder)) Directory.CreateDirectory(uploadsFolder);
  96. var uniqueFileName = Guid.NewGuid().ToString() + fileExtension;
  97. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  98. using (var fileStream = new FileStream(filePath, FileMode.Create))
  99. {
  100. file.CopyTo(fileStream);
  101. fileStream.Flush();
  102. }
  103. await _fileManager.Create(name, uniqueFileName, fileExtension, "", size, filePath, "/files/" + uniqueFileName, FileType.Image);
  104. }
  105. public dynamic Download([Required] string name)
  106. {
  107. var filePath = Path.Combine(Environment.CurrentDirectory, "files", name);
  108. if (!File.Exists(filePath)) throw new BusinessException("找不到文件");
  109. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = name };
  110. }
  111. }
  112. }