UploadController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using AngleSharp;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using Masuit.MyBlogs.Core.Common;
  4. using Masuit.MyBlogs.Core.Extensions.Firewall;
  5. using Masuit.MyBlogs.Core.Extensions.UEditor;
  6. using Masuit.MyBlogs.Core.Models.DTO;
  7. using Masuit.MyBlogs.Core.Models.ViewModel;
  8. using Masuit.Tools.AspNetCore.Mime;
  9. using Masuit.Tools.AspNetCore.ResumeFileResults.Extensions;
  10. using Masuit.Tools.Core.Net;
  11. using Masuit.Tools.Html;
  12. using Masuit.Tools.Logging;
  13. using Masuit.Tools.Systems;
  14. using Microsoft.AspNetCore.Hosting;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Newtonsoft.Json;
  18. using OpenXmlPowerTools;
  19. using System;
  20. using System.ComponentModel.DataAnnotations;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Text.RegularExpressions;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using System.Xml.Linq;
  28. namespace Masuit.MyBlogs.Core.Controllers
  29. {
  30. /// <summary>
  31. /// 文件上传
  32. /// </summary>
  33. [ApiExplorerSettings(IgnoreApi = true)]
  34. [ServiceFilter(typeof(FirewallAttribute))]
  35. public class UploadController : Controller
  36. {
  37. public IWebHostEnvironment HostEnvironment { get; set; }
  38. public ActionResult ResultData(object data, bool isTrue = true, string message = "")
  39. {
  40. return Content(JsonConvert.SerializeObject(new
  41. {
  42. Success = isTrue,
  43. Message = message,
  44. Data = data
  45. }, new JsonSerializerSettings
  46. {
  47. MissingMemberHandling = MissingMemberHandling.Ignore
  48. }), "application/json", Encoding.UTF8);
  49. }
  50. #region Word上传转码
  51. /// <summary>
  52. /// 上传Word转码
  53. /// </summary>
  54. /// <returns></returns>
  55. [HttpPost]
  56. public async Task<ActionResult> UploadWord(CancellationToken cancellationToken)
  57. {
  58. var form = await Request.ReadFormAsync(cancellationToken);
  59. var files = form.Files;
  60. if (files.Count <= 0)
  61. {
  62. return ResultData(null, false, "请先选择您需要上传的文件!");
  63. }
  64. var file = files[0];
  65. string fileName = file.FileName;
  66. if (!Regex.IsMatch(Path.GetExtension(fileName), "doc|docx"))
  67. {
  68. return ResultData(null, false, "文件格式不支持,只能上传doc或者docx的文档!");
  69. }
  70. var html = await SaveAsHtml(file);
  71. if (html.Length < 10)
  72. {
  73. return ResultData(null, false, "读取文件内容失败,请检查文件的完整性,建议另存后重新上传!");
  74. }
  75. if (html.Length > 1000000)
  76. {
  77. return ResultData(null, false, "文档内容超长,服务器拒绝接收,请优化文档内容后再尝试重新上传!");
  78. }
  79. return ResultData(new
  80. {
  81. Title = Path.GetFileNameWithoutExtension(fileName),
  82. Content = html
  83. });
  84. }
  85. private async Task<string> ConvertToHtml(IFormFile file)
  86. {
  87. var docfile = Path.Combine(Environment.GetEnvironmentVariable("temp") ?? "upload", file.FileName);
  88. try
  89. {
  90. await using var ms = file.OpenReadStream();
  91. await using var fs = System.IO.File.Create(docfile, 1024, FileOptions.DeleteOnClose);
  92. await ms.CopyToAsync(fs);
  93. using var doc = WordprocessingDocument.Open(fs, true);
  94. var pageTitle = file.FileName;
  95. var part = doc.CoreFilePropertiesPart;
  96. if (part != null)
  97. {
  98. pageTitle ??= (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault();
  99. }
  100. var settings = new HtmlConverterSettings()
  101. {
  102. PageTitle = pageTitle,
  103. FabricateCssClasses = false,
  104. RestrictToSupportedLanguages = false,
  105. RestrictToSupportedNumberingFormats = false,
  106. ImageHandler = imageInfo =>
  107. {
  108. var stream = new MemoryStream();
  109. imageInfo.Bitmap.Save(stream, imageInfo.Bitmap.RawFormat);
  110. var base64String = Convert.ToBase64String(stream.ToArray());
  111. return new XElement(Xhtml.img, new XAttribute(NoNamespace.src, $"data:{imageInfo.ContentType};base64," + base64String), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
  112. }
  113. };
  114. var htmlElement = HtmlConverter.ConvertToHtml(doc, settings);
  115. var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
  116. var htmlString = html.ToString(SaveOptions.DisableFormatting);
  117. return htmlString;
  118. }
  119. finally
  120. {
  121. if (System.IO.File.Exists(docfile))
  122. {
  123. System.IO.File.Delete(docfile);
  124. }
  125. }
  126. }
  127. private async Task<string> SaveAsHtml(IFormFile file)
  128. {
  129. var html = await ConvertToHtml(file);
  130. var context = BrowsingContext.New(Configuration.Default);
  131. var doc = context.OpenAsync(req => req.Content(html)).Result;
  132. var body = doc.Body;
  133. var nodes = body.GetElementsByTagName("img");
  134. foreach (var img in nodes)
  135. {
  136. var attr = img.Attributes["src"].Value;
  137. var strs = attr.Split(",");
  138. var base64 = strs[1];
  139. var bytes = Convert.FromBase64String(base64);
  140. var ext = strs[0].Split(";")[0].Split("/")[1];
  141. await using var image = new MemoryStream(bytes);
  142. var imgFile = $"{SnowFlake.NewId}.{ext}";
  143. var path = Path.Combine(HostEnvironment.WebRootPath, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\'), "images", imgFile);
  144. var dir = Path.GetDirectoryName(path);
  145. Directory.CreateDirectory(dir);
  146. await using var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  147. await image.CopyToAsync(fs);
  148. img.Attributes["src"].Value = path.Substring(HostEnvironment.WebRootPath.Length).Replace("\\", "/");
  149. }
  150. return body.InnerHtml.HtmlSantinizerStandard().HtmlSantinizerCustom(attributes: new[] { "dir", "lang" });
  151. }
  152. private static async Task SaveFile(IFormFile file, string path)
  153. {
  154. var dir = Path.GetDirectoryName(path);
  155. Directory.CreateDirectory(dir);
  156. await using var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  157. await file.CopyToAsync(fs);
  158. }
  159. #endregion
  160. /// <summary>
  161. /// 文件下载
  162. /// </summary>
  163. /// <param name="path"></param>
  164. /// <returns></returns>
  165. [HttpGet("download/{**path}")]
  166. public ActionResult Download([FromServices] IMimeMapper mimeMapper, [Required] string path)
  167. {
  168. if (string.IsNullOrEmpty(path)) return Content("null");
  169. var file = Path.Combine(HostEnvironment.WebRootPath, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\'), path.Trim('.', '/', '\\'));
  170. if (System.IO.File.Exists(file))
  171. {
  172. return this.ResumePhysicalFile(file, mimeMapper.GetMimeFromPath(file), Path.GetFileName(file));
  173. }
  174. return Content("null");
  175. }
  176. /// <summary>
  177. /// UEditor文件上传处理
  178. /// </summary>
  179. /// <returns></returns>
  180. [Route("fileuploader")]
  181. public async Task<ActionResult> UeditorFileUploader()
  182. {
  183. UserInfoDto user = HttpContext.Session.Get<UserInfoDto>(SessionKey.UserInfo) ?? new UserInfoDto();
  184. var action = Request.Query["action"].ToString() switch //通用
  185. {
  186. "config" => (Handler)new ConfigHandler(HttpContext),
  187. "uploadimage" => new UploadHandler(HttpContext, new UploadConfig()
  188. {
  189. AllowExtensions = UeditorConfig.GetStringList("imageAllowFiles"),
  190. PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("imagePathFormat"),
  191. SizeLimit = UeditorConfig.GetInt("imageMaxSize"),
  192. UploadFieldName = UeditorConfig.GetString("imageFieldName")
  193. }),
  194. "uploadscrawl" => new UploadHandler(HttpContext, new UploadConfig()
  195. {
  196. AllowExtensions = new[]
  197. {
  198. ".png"
  199. },
  200. PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("scrawlPathFormat"),
  201. SizeLimit = UeditorConfig.GetInt("scrawlMaxSize"),
  202. UploadFieldName = UeditorConfig.GetString("scrawlFieldName"),
  203. Base64 = true,
  204. Base64Filename = "scrawl.png"
  205. }),
  206. "catchimage" => new CrawlerHandler(HttpContext),
  207. _ => new NotSupportedHandler(HttpContext)
  208. };
  209. if (user.IsAdmin)
  210. {
  211. switch (Request.Query["action"])//管理员用
  212. {
  213. //case "uploadvideo":
  214. // action = new UploadHandler(context, new UploadConfig()
  215. // {
  216. // AllowExtensions = UeditorConfig.GetStringList("videoAllowFiles"),
  217. // PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload") + UeditorConfig.GetString("videoPathFormat"),
  218. // SizeLimit = UeditorConfig.GetInt("videoMaxSize"),
  219. // UploadFieldName = UeditorConfig.GetString("videoFieldName")
  220. // });
  221. // break;
  222. case "uploadfile":
  223. action = new UploadHandler(HttpContext, new UploadConfig()
  224. {
  225. AllowExtensions = UeditorConfig.GetStringList("fileAllowFiles"),
  226. PathFormat = "/" + CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("filePathFormat"),
  227. SizeLimit = UeditorConfig.GetInt("fileMaxSize"),
  228. UploadFieldName = UeditorConfig.GetString("fileFieldName")
  229. });
  230. break;
  231. //case "listimage":
  232. // action = new ListFileManager(context, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "/upload") + UeditorConfig.GetString("imageManagerListPath"), UeditorConfig.GetStringList("imageManagerAllowFiles"));
  233. // break;
  234. //case "listfile":
  235. // action = new ListFileManager(context, CommonHelper.SystemSettings.GetOrAdd("UploadPath", "/upload") + UeditorConfig.GetString("fileManagerListPath"), UeditorConfig.GetStringList("fileManagerAllowFiles"));
  236. // break;
  237. }
  238. }
  239. string result = await action.Process();
  240. return Content(result, ContentType.Json);
  241. }
  242. /// <summary>
  243. /// 上传文件
  244. /// </summary>
  245. /// <param name="imagebedClient"></param>
  246. /// <param name="file"></param>
  247. /// <returns></returns>
  248. [HttpPost("upload"), ApiExplorerSettings(IgnoreApi = false)]
  249. public async Task<ActionResult> UploadFile([FromServices] ImagebedClient imagebedClient, IFormFile file, CancellationToken cancellationToken)
  250. {
  251. string path;
  252. string filename = SnowFlake.GetInstance().GetUniqueId() + Path.GetExtension(file.FileName);
  253. var pathBase = CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\');
  254. switch (file.ContentType)
  255. {
  256. case var _ when file.ContentType.StartsWith("image"):
  257. {
  258. await using var stream = file.OpenReadStream();
  259. var (url, success) = await imagebedClient.UploadImage(stream, file.FileName, cancellationToken);
  260. if (success)
  261. {
  262. return ResultData(url);
  263. }
  264. path = Path.Combine(HostEnvironment.WebRootPath, pathBase, "images", filename);
  265. var dir = Path.GetDirectoryName(path);
  266. Directory.CreateDirectory(dir);
  267. await using var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  268. await file.CopyToAsync(fs);
  269. break;
  270. }
  271. case var _ when file.ContentType.StartsWith("audio") || file.ContentType.StartsWith("video"):
  272. path = Path.Combine(HostEnvironment.WebRootPath, pathBase, "media", filename);
  273. break;
  274. case var _ when file.ContentType.StartsWith("text") || (ContentType.Doc + "," + ContentType.Xls + "," + ContentType.Ppt + "," + ContentType.Pdf).Contains(file.ContentType):
  275. path = Path.Combine(HostEnvironment.WebRootPath, pathBase, "docs", filename);
  276. break;
  277. default:
  278. path = Path.Combine(HostEnvironment.WebRootPath, pathBase, "files", filename);
  279. break;
  280. }
  281. try
  282. {
  283. await SaveFile(file, path);
  284. return ResultData(path.Substring(HostEnvironment.WebRootPath.Length).Replace("\\", "/"));
  285. }
  286. catch (Exception e)
  287. {
  288. LogManager.Error(e);
  289. return ResultData(null, false, "文件上传失败!");
  290. }
  291. }
  292. }
  293. }