AttachmentController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using CZFW.CMS.Admin;
  2. using CZFW.Core;
  3. using CZFW.Framework.Model.ViewModel;
  4. using CZFW.MDB.Util;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. namespace CZFW.CMS.Builder.Controllers
  13. {
  14. public class AttachmentController:AdminControllerBase
  15. {
  16. ILogger<AttachmentController> _logger;
  17. public AttachmentController(ILogger<AttachmentController> logger)
  18. {
  19. _logger = logger;
  20. }
  21. [HttpPost]
  22. public ResultModel UploadImages()
  23. {
  24. if (!Request.ContentType.Contains("form-data"))
  25. {
  26. var result = new ResultModel()
  27. {
  28. Success = false,
  29. Code = "",
  30. Message = "表单中未找到文件"
  31. };
  32. return result;
  33. } //如果上传文件不是form-data类型抛出异常
  34. var urls = new List<String>();
  35. var model = new ResultModel();
  36. var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
  37. try
  38. {
  39. foreach (var f in Request.Form.Files)
  40. {
  41. var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
  42. if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.image))
  43. return Result(false, "文件类型错误,请上传jpg,png,gif类型的图片文件");
  44. var mimeType = f.Headers["Content-Type"];
  45. if (!mimeType.ToString().Contains("image"))
  46. {
  47. return new ResultModel("文件类型错误!请上传jpg,png,gif类型的图片文件。");
  48. }
  49. string date = DateTime.Now.ToString();
  50. var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);// filePathRoot + "\\TempFiles\\" + fileName;
  51. var ff = new FileInfo(destPath);
  52. using (var tt = ff.OpenWrite())
  53. {
  54. f.CopyTo(tt);
  55. tt.Flush();
  56. tt.Close();
  57. }
  58. var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "images", fileName);// $"{filePathRoot}\\{Site.SiteName}\\images\\{fileName}";
  59. FileInfo flocal = new FileInfo(destPath);
  60. var tp = flocal.CopyTo(destPath2, true);
  61. urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/images/{fileName}");
  62. }
  63. model.Data = urls;
  64. model.Success = true;
  65. }
  66. catch (Exception err)
  67. {
  68. model.Success = false;
  69. model.Message = err.Message;
  70. }
  71. return model;
  72. }
  73. [HttpPost]
  74. [RequestSizeLimit(500_000_000)] //最大100m左右
  75. public ResultModel UploadDocs()
  76. {
  77. if (!Request.ContentType.Contains("form-data"))
  78. {
  79. var result = new ResultModel()
  80. {
  81. Success = false,
  82. Code = "",
  83. Message = "表单中未找到文件"
  84. };
  85. return result;
  86. } //如果上传文件不是form-data类型抛出异常
  87. var urls = new List<String>();
  88. var model = new ResultModel();
  89. var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
  90. try
  91. {
  92. foreach (var f in Request.Form.Files)
  93. {
  94. var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
  95. if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.document))
  96. return Result(false, "文件类型错误,请上传word,excel,ppt,pdf,zip或者rar类型的文档");
  97. var mimeType = f.Headers["Content-Type"];
  98. //if (!mimeType.ToString().Contains("image"))
  99. //{
  100. // return new ResultModel("文件类型错误!请上传jpg,png,gif类型的图片文件。");
  101. //}
  102. string date = DateTime.Now.ToString();
  103. var destPath = Path.Combine(filePathRoot, "TempFiles" , fileName);
  104. var ff = new FileInfo(destPath);
  105. using (var tt = ff.OpenWrite())
  106. {
  107. f.CopyTo(tt);
  108. tt.Flush();
  109. tt.Close();
  110. }
  111. var destPath2 = Path.Combine(filePathRoot,Site.SiteName,"docs",fileName);
  112. FileInfo flocal = new FileInfo(destPath);
  113. var tp = flocal.CopyTo(destPath2, true);
  114. urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/docs/{fileName}");
  115. }
  116. model.Data = urls;
  117. model.Success = true;
  118. }
  119. catch (Exception err)
  120. {
  121. model.Success = false;
  122. model.Message = err.Message;
  123. }
  124. return model;
  125. }
  126. [HttpPost]
  127. [RequestSizeLimit(500_000_000)] //最大100m左右
  128. public ResultModel UploadAudio()
  129. {
  130. if (!Request.ContentType.Contains("form-data"))
  131. {
  132. var result = new ResultModel()
  133. {
  134. Success = false,
  135. Code = "",
  136. Message = "表单中未找到文件"
  137. };
  138. return result;
  139. } //如果上传文件不是form-data类型抛出异常
  140. var urls = new List<String>();
  141. var model = new ResultModel();
  142. var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
  143. try
  144. {
  145. foreach (var f in Request.Form.Files)
  146. {
  147. var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
  148. if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.audio))
  149. return Result(false, "文件类型错误,请上传mp3,wav,mid类型的音频文件");
  150. var mimeType = f.Headers["Content-Type"];
  151. if (!mimeType.ToString().Contains("audio"))
  152. {
  153. return new ResultModel("文件类型错误!请上传mp3,wav,mid类型的音频文件。");
  154. }
  155. string date = DateTime.Now.ToString();
  156. var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
  157. var ff = new FileInfo(destPath);
  158. using (var tt = ff.OpenWrite())
  159. {
  160. f.CopyTo(tt);
  161. tt.Flush();
  162. tt.Close();
  163. }
  164. var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "audios", fileName);
  165. FileInfo flocal = new FileInfo(destPath);
  166. var tp = flocal.CopyTo(destPath2, true);
  167. urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/audios/{fileName}");
  168. }
  169. model.Data = urls;
  170. model.Success = true;
  171. }
  172. catch (Exception err)
  173. {
  174. model.Success = false;
  175. model.Message = err.Message;
  176. }
  177. return model;
  178. }
  179. [HttpPost]
  180. [RequestSizeLimit(500_000_000)] //最大100m左右
  181. public ResultModel UploadVideo()
  182. {
  183. if (!Request.ContentType.Contains("form-data"))
  184. {
  185. var result = new ResultModel()
  186. {
  187. Success = false,
  188. Code = "",
  189. Message = "表单中未找到文件"
  190. };
  191. return result;
  192. } //如果上传文件不是form-data类型抛出异常
  193. var urls = new List<String>();
  194. var model = new ResultModel();
  195. var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
  196. try
  197. {
  198. foreach (var f in Request.Form.Files)
  199. {
  200. var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
  201. if (!CheckFileExtension(f.FileName, CheckFileTypeEnum.video))
  202. return Result(false, "文件类型错误,请上传mp4, avi类型的视频文件");
  203. var mimeType = f.Headers["Content-Type"];
  204. if (!mimeType.ToString().Contains("video"))
  205. {
  206. return new ResultModel("文件类型错误!请上传mp4, avi类型的视频文件。");
  207. }
  208. string date = DateTime.Now.ToString();
  209. var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
  210. var ff = new FileInfo(destPath);
  211. using (var tt = ff.OpenWrite())
  212. {
  213. f.CopyTo(tt);
  214. tt.Flush();
  215. tt.Close();
  216. }
  217. var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "videos", fileName);
  218. FileInfo flocal = new FileInfo(destPath);
  219. var tp = flocal.CopyTo(destPath2, true);
  220. urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/videos/{fileName}");
  221. }
  222. model.Data = urls;
  223. model.Success = true;
  224. }
  225. catch (Exception err)
  226. {
  227. model.Success = false;
  228. model.Message = err.Message;
  229. }
  230. return model;
  231. }
  232. public bool CheckFileExtension(string fileName, CheckFileTypeEnum type)
  233. {
  234. var parts = fileName.ToLower().Split('.');
  235. if(parts.Length<1)
  236. {
  237. _logger.LogWarning($"尝试检查文件后缀名异常,文件名:{fileName}");
  238. return false;
  239. }
  240. var extension = parts[parts.Length - 1];
  241. switch (type)
  242. {
  243. case CheckFileTypeEnum.image:
  244. {
  245. var imageExtensions =new string[]{"jpg","png","gif","bmp","tif"};
  246. if (imageExtensions.Contains(extension))
  247. return true;
  248. else
  249. return false;
  250. }
  251. case CheckFileTypeEnum.audio:
  252. {
  253. var audioExtensions = new string[] { "mp3","aud","wav","mid"};
  254. if (audioExtensions.Contains(extension))
  255. return true;
  256. else
  257. return false;
  258. }
  259. case CheckFileTypeEnum.video:
  260. {
  261. var videoExtensions = new string[] { "mp4","avi","mpeg","rmvb","flv"};
  262. if (videoExtensions.Contains(extension))
  263. return true;
  264. else
  265. return false;
  266. }
  267. case CheckFileTypeEnum.document:
  268. {
  269. var docExtensions = new string[] { "doc","docx","pdf","ppt","pptx","xls","xlsx","zip","rar"};
  270. if (docExtensions.Contains(extension))
  271. return true;
  272. else
  273. return false;
  274. }
  275. default:
  276. return false;
  277. }
  278. }
  279. [HttpPost]
  280. public IActionResult Upload(string theme = "default")
  281. {
  282. if (!Request.ContentType.Contains("form-data"))
  283. {
  284. var result = new ResultModel()
  285. {
  286. Success = false,
  287. Code = "",
  288. Message = "表单中未找到文件"
  289. };
  290. var res = new { error = 1, message = result.Message };
  291. return Json(res);
  292. } //如果上传文件不是form-data类型抛出异常
  293. var urls = new List<String>();
  294. var model = new ResultModel();
  295. var filePathRoot = ConfigHelper.GetValue<string>("ResourcePathRoot");
  296. try
  297. {
  298. foreach (var f in Request.Form.Files)
  299. {
  300. var fileName = $"{CZFW.Core.Utility.GetUnixTimeTick()}" + f.FileName;
  301. if (
  302. !(CheckFileExtension(f.FileName, CheckFileTypeEnum.image) ||
  303. CheckFileExtension(f.FileName, CheckFileTypeEnum.audio) ||
  304. CheckFileExtension(f.FileName, CheckFileTypeEnum.document) ||
  305. CheckFileExtension(f.FileName, CheckFileTypeEnum.video))
  306. )
  307. {
  308. var res = new { error = 1, message = "文件类型错误" };
  309. return Json(res);
  310. }
  311. string date = DateTime.Now.ToString();
  312. var destPath = Path.Combine(filePathRoot, "TempFiles", fileName);
  313. var ff = new FileInfo(destPath);
  314. using (var tt = ff.OpenWrite())
  315. {
  316. f.CopyTo(tt);
  317. tt.Flush();
  318. tt.Close();
  319. }
  320. var destPathRoot = ConfigHelper.GetValue<string>("SiteViewPathRoot");
  321. var destPath2 = Path.Combine(filePathRoot, Site.SiteName, "editor_upload", fileName);
  322. FileInfo flocal = new FileInfo(destPath);
  323. var tp = flocal.CopyTo(destPath2, true);
  324. urls.Add($"/{Constants.RESOURCE_FILE_URL_PREFIX}/{Site.SiteName}/editor_upload/{fileName}");
  325. }
  326. model.Data = urls;
  327. model.Success = true;
  328. }
  329. catch (Exception err)
  330. {
  331. model.Success = false;
  332. model.Message = err.Message;
  333. }
  334. if (model.Success)
  335. {
  336. var url = (model.Data as List<string>)[0];
  337. var res = new { error = 0, url };
  338. return Json(res);
  339. }
  340. else
  341. {
  342. var res = new { error = 1, message = model.Message };
  343. return Json(res);
  344. }
  345. }
  346. }
  347. public enum CheckFileTypeEnum
  348. {
  349. image,
  350. audio,
  351. video,
  352. document
  353. }
  354. }