FileService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using HiTeachCC.Model.Core.Models;
  2. using HiTeachCC.Service.Core.Interface;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using PdfSharpCore.Pdf;
  6. using PdfSharpCore.Pdf.IO;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.Context.Configuration;
  12. using TEAMModelOS.SDK.Context.Constant.Common;
  13. using TEAMModelOS.SDK.Context.Exception;
  14. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  15. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  16. using TEAMModelOS.SDK.Helper.Network.HttpHelper;
  17. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  18. using TEAMModelOS.SDK.Module.AzureBlob.Container;
  19. using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
  20. using TEAMModelOS.SDK.Module.PowerPointX;
  21. using TEAMModelOS.SDK.Module.PowerPointX.Model;
  22. namespace HiTeachCC.Service.Core.Implement
  23. {
  24. public class FileService : BaseService, IFileService
  25. {
  26. private readonly IAzureBlobDBRepository azureBlobDBRepository;
  27. public FileService(IAzureBlobDBRepository _azureBlobDBRepository) {
  28. azureBlobDBRepository = _azureBlobDBRepository;
  29. }
  30. public async Task<Dictionary<string, object>> UploadDocument(IFormFile file)
  31. {
  32. Dictionary<string, object> resdict = new Dictionary<string, object>();
  33. if (FileType.GetExtention(file.FileName).ToLower().Equals("pptx"))
  34. {
  35. await ProcessPPT(file, resdict);
  36. return resdict;
  37. }
  38. else if (FileType.GetExtention(file.FileName).ToLower().Equals("pdf"))
  39. {
  40. PdfDocument inputDocument = PdfSharpCore.Pdf.IO.PdfReader.Open(file.OpenReadStream(), PdfDocumentOpenMode.Import);
  41. int count = inputDocument.PageCount;
  42. int limit = BaseConfigModel.Configuration.GetSection("HABOOKConfig:PDFLimit").Get<int>();
  43. if (count > limit)
  44. {
  45. throw new BizException(500, "PDF file cannot exceed " + limit + " pages!");
  46. }
  47. await ProcessPDF(file, resdict);
  48. resdict.Add("page", count);
  49. return resdict;
  50. }
  51. else {
  52. throw new BizException(500, "file type does not support!");
  53. }
  54. }
  55. private async Task ProcessPDF(IFormFile file, Dictionary<string, object> resdict)
  56. {
  57. string shaCode = ShaHashHelper.GetSHA1(file.OpenReadStream());
  58. Dictionary<string, object> dict = new Dictionary<string, object> { { "Sha1Code", shaCode } };
  59. List<AzureBlobModel> models = await FindListByDict<AzureBlobModel>(dict);
  60. if (models.IsNotEmpty())
  61. {
  62. resdict.Add("model", models[0]);
  63. resdict.Add("type", "pdf");
  64. }
  65. else
  66. {
  67. AzureBlobModel azureBlobModel = await azureBlobDBRepository.UploadFile(file, "pdffiles", true);
  68. azureBlobModel.Sha1Code = shaCode;
  69. await SaveOrUpdate<AzureBlobModel>(azureBlobModel);
  70. azureBlobModel.Sha1Code = shaCode;
  71. resdict.Add("model", azureBlobModel);
  72. resdict.Add("type", "pdf");
  73. }
  74. }
  75. private async Task ProcessPPT(IFormFile file, Dictionary<string, object> resdict)
  76. {
  77. string shaCode = ShaHashHelper.GetSHA1(file.OpenReadStream());
  78. Dictionary<string, object> dict = new Dictionary<string, object> { { "Sha1Code", shaCode } };
  79. List<AzureBlobModel> models = await FindListByDict<AzureBlobModel>(dict);
  80. PPTXInfo info;
  81. if (models.IsNotEmpty())
  82. {
  83. List<PPTData> pptData = await FindListByDict<PPTData>(dict);
  84. var pptxjson = HttpHelper.HttpGet(pptData[0].DataUrl);
  85. info = JsonSerialization.FromJsonAbs<PPTXInfo>(pptxjson);
  86. resdict.Add("pptx", info);
  87. resdict.Add("model", models[0]);
  88. resdict.Add("type", "pptx");
  89. }
  90. else
  91. {
  92. AzureBlobModel azureBlobModel = await azureBlobDBRepository.UploadFile(file, "pptfiles", true);
  93. azureBlobModel.Sha1Code = shaCode;
  94. await SaveOrUpdate<AzureBlobModel>(azureBlobModel);
  95. PresentationConvert convert = new PresentationConvert();
  96. info = convert.LoadPresentation(file.OpenReadStream());
  97. AzureBlobModel pptJsonFileModel = await azureBlobDBRepository.UploadObject(shaCode + ".json", info, "pptfiles", true);
  98. PPTData pptData = new PPTData { DataUrl = pptJsonFileModel.BlobUrl, Sha1Code = shaCode, RowKey = shaCode, PartitionKey = "pptfiles" };
  99. await SaveOrUpdate<PPTData>(pptData);
  100. resdict.Add("pptx", info);
  101. resdict.Add("model", azureBlobModel);
  102. resdict.Add("type", "pptx");
  103. }
  104. }
  105. }
  106. }