ContentService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. namespace TEAMModelOS.Services
  13. {
  14. public class ContentService
  15. {
  16. private static readonly Dictionary<string, List<string>> fileTypes = new()
  17. {
  18. { "audio", new List<string> { "mp3", "ogg", "wav", "ape", "cda", "au", "midi", "mac", "aac" }},
  19. { "doc", new List<string> { "ppt", "pptx", "doc", "docx", "pdf", "xls", "xlsx", "csv" }},
  20. { "image", new List<string> { "jpg", "jpeg", "bmp", "tif", "png", "gif", "svg" }},
  21. { "res", new List<string> { "hte", "htex" }},
  22. { "video", new List<string> { "rmvb", "wmv", "asf", "avi", "3gp", "mpg", "mkv", "mp4", "dvd", "ogm", "mov", "mpeg2", "mpeg4" }},
  23. };
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly AzureCosmosFactory _azureCosmos;
  26. private readonly FileExtractService _fileExtractService;
  27. public ContentService(AzureStorageFactory azureStorage, AzureCosmosFactory azureCosmos)
  28. {
  29. _azureStorage = azureStorage;
  30. _azureCosmos = azureCosmos;
  31. _fileExtractService = new FileExtractService(azureStorage);
  32. }
  33. public async Task UploadToSchool(IFormFile file, string id, string[] periodId, string[] subjectId, string[] gradeId)
  34. {
  35. var fileName = file.FileName;
  36. var extension = Path.GetExtension(fileName).ToLower().TrimStart('.');
  37. var type = _getFileType(extension);
  38. var blobPath = $"{type}/{fileName}";
  39. await _uploadFile(file, extension, id, blobPath);
  40. if (type == "image")
  41. {
  42. var thumbnailPath = $"thum/{fileName}";
  43. await _uploadThumbnail(file, id, thumbnailPath);
  44. }
  45. var bloblog = new Bloblog
  46. {
  47. id = Guid.NewGuid().ToString(),
  48. pk = "Bloblog",
  49. code = $"Bloblog-{id}",
  50. time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  51. size = file.Length,
  52. url = blobPath,
  53. type = type,
  54. ext = "." + extension,
  55. periodId = periodId.ToList(),
  56. subjectId = subjectId.ToList(),
  57. gradeId = gradeId.ToList(),
  58. };
  59. await _writeFileRecord(Constant.School, id, bloblog);
  60. }
  61. public async Task UploadToPrivate(IFormFile file, string id)
  62. {
  63. var fileName = file.FileName;
  64. var extension = Path.GetExtension(fileName).ToLower().TrimStart('.');
  65. var type = _getFileType(extension);
  66. var blobPath = $"{type}/{fileName}";
  67. await _uploadFile(file, extension, id, blobPath);
  68. if (type == "image")
  69. {
  70. var thumbnailPath = $"thum/{fileName}";
  71. await _uploadThumbnail(file, id, thumbnailPath);
  72. }
  73. var bloblog = new Bloblog
  74. {
  75. id = Guid.NewGuid().ToString(),
  76. pk = "Bloblog",
  77. code = $"Bloblog-{id}",
  78. time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  79. size = file.Length,
  80. url = blobPath,
  81. type = type,
  82. ext = "." + extension,
  83. periodId = new List<string> {},
  84. subjectId = new List<string> {},
  85. gradeId = new List<string> {},
  86. };
  87. await _writeFileRecord(Constant.Teacher, id, bloblog);
  88. }
  89. private string _getFileType(string extension)
  90. {
  91. foreach (var fileType in fileTypes)
  92. {
  93. if (fileType.Value.Contains(extension))
  94. {
  95. return fileType.Key;
  96. }
  97. }
  98. return "other";
  99. }
  100. private async Task _uploadFile(IFormFile file, string extension, string id, string path)
  101. {
  102. var fileName = Path.GetFileNameWithoutExtension(file.FileName);
  103. var blobClient = _azureStorage.GetBlobContainerClient(id).GetBlobClient(path);
  104. using (var stream = file.OpenReadStream())
  105. {
  106. if (extension == "htex")
  107. {
  108. await _fileExtractService.ExtractHTEX(id, fileName, stream);
  109. }
  110. else
  111. {
  112. await blobClient.UploadAsync(stream);
  113. }
  114. }
  115. }
  116. private async Task _uploadThumbnail(IFormFile file, string id, string path)
  117. {
  118. int width = 300;
  119. using (var stream = new MemoryStream())
  120. {
  121. await file.CopyToAsync(stream);
  122. stream.Seek(0, SeekOrigin.Begin);
  123. using (var image = Image.FromStream(stream))
  124. {
  125. int height = (int)(image.Height * (width / (double)image.Width));
  126. using (var resizedImage = new Bitmap(image, width, height))
  127. using (var thumbnailStream = new MemoryStream())
  128. {
  129. resizedImage.Save(thumbnailStream, ImageFormat.Jpeg);
  130. thumbnailStream.Position = 0;
  131. var blobClient = _azureStorage.GetBlobContainerClient(id).GetBlobClient(path);
  132. await blobClient.UploadAsync(thumbnailStream);
  133. }
  134. }
  135. }
  136. }
  137. private async Task _writeFileRecord(string containerName, string id, Bloblog bloblog)
  138. {
  139. var cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, containerName);
  140. await cosmosContainer.CreateItemAsync(bloblog, new PartitionKey($"Bloblog-{id}"));
  141. }
  142. }
  143. }