您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

HomeController.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using AutoMapper;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.AspNetCore.StaticFiles;
  5. using SecureSharing.Business.Dtos;
  6. using SecureSharing.Business.Interfaces;
  7. using SecureSharing.Data.Data;
  8. using SecureSharing.Infrastructure;
  9. using SecureSharing.Models;
  10. namespace SecureSharing.Controllers;
  11. [Authorize]
  12. public sealed class HomeController : Controller
  13. {
  14. private const string DefaultPath = "files";
  15. private const string DefaultPathTmp = "filestmp";
  16. private readonly ILogger<HomeController> _logger;
  17. private readonly IMessageService _messageService;
  18. private readonly IModelFactory _modelFactory;
  19. private readonly IWebHostEnvironment _webHostEnvironment;
  20. private readonly IMapper _mapper;
  21. public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory,
  22. IWebHostEnvironment webHostEnvironment, IMapper mapper)
  23. {
  24. _logger = logger;
  25. _messageService = messageService;
  26. _modelFactory = modelFactory;
  27. _webHostEnvironment = webHostEnvironment;
  28. _mapper = mapper;
  29. }
  30. public IActionResult Index()
  31. {
  32. return View();
  33. }
  34. [AllowAnonymous]
  35. public async Task<string> UploadTemporaryFile()
  36. {
  37. var code = Guid.NewGuid().ToString();
  38. var files = Request.Form.Files.ToList();
  39. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp, code);
  40. Directory.CreateDirectory(basePath);
  41. foreach (var formFile in files)
  42. {
  43. if (formFile.Length <= 0)
  44. continue;
  45. var filePath = Path.Combine(basePath, formFile.FileName);
  46. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  47. await formFile.CopyToAsync(stream);
  48. }
  49. return code;
  50. }
  51. [HttpPost]
  52. public async Task<IActionResult> CreateMessage(MessageModel model)
  53. {
  54. if (!model.AllowEditing
  55. && string.IsNullOrWhiteSpace(model.Text)
  56. && model.Files.Count == 0
  57. && model.FilesAsText == "978682e8-3ce7-4258-b731-d027b5b213aa")
  58. {
  59. return Redirect("/");
  60. }
  61. model.FilesAsText = model.FilesAsText != "978682e8-3ce7-4258-b731-d027b5b213aa"
  62. ? model.FilesAsText.Split("978682e8-3ce7-4258-b731-d027b5b213aa")[1]
  63. : "";
  64. var message = new MessageDto { Text = model.Text, Anonymous = model.AllowEditing || model.Anonymous, AllowEditing = model.AllowEditing};
  65. await UploadFiles(model, message);
  66. var code = await _messageService.Create(message, model.ChosenPeriod);
  67. return RedirectToAction("Link", "Home", new { code, share = true });
  68. }
  69. [HttpPost]
  70. [AllowAnonymous]
  71. public async Task<IActionResult> UpdateMessage(MessageModel model)
  72. {
  73. if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0 &&
  74. model.FilesAsText == "978682e8-3ce7-4258-b731-d027b5b213aa")
  75. {
  76. return Redirect("/");
  77. }
  78. model.FilesAsText = model.FilesAsText != "978682e8-3ce7-4258-b731-d027b5b213aa"
  79. ? model.FilesAsText.Split("978682e8-3ce7-4258-b731-d027b5b213aa")[1]
  80. : "";
  81. var message = new MessageDto { Text = model.Text, Anonymous = model.Anonymous, Id = model.Id, Code = model.Code};
  82. // var messageDto = _mapper.Map<MessageDto>(model);
  83. await UploadFiles(model, message);
  84. await _messageService.Update(message);
  85. return RedirectToAction("LinkAnonymous", "Home", new { model.Code, share = true, edit = true });
  86. }
  87. [AllowAnonymous]
  88. private async Task UploadFiles(MessageModel model, MessageDto message)
  89. {
  90. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath,
  91. message.Code.ToString());
  92. var basePathTemporary = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp);
  93. Directory.CreateDirectory(basePath);
  94. var directoryNames = model.FilesAsText
  95. .Split(';')
  96. .Select(x => x.Split(':')[0])
  97. .Where(x => !string.IsNullOrEmpty(x))
  98. .Distinct()
  99. .ToList();
  100. var fileNamesTmp = model.FilesAsText
  101. .Split(';')
  102. .Where(x => !string.IsNullOrEmpty(x))
  103. .ToList();
  104. var fileNames = fileNamesTmp.Select(x => x.Split(':')[1]).ToList();
  105. foreach (var file in fileNames) message.FileNames.Add(new FileModel { Name = file });
  106. for (var ind = 0; ind < directoryNames.Count; ind++)
  107. {
  108. var directoryName = directoryNames[ind];
  109. var directoryPath = Path.Combine(basePathTemporary, directoryName);
  110. var files = Directory.GetFiles(directoryPath);
  111. for (var i = 0; i < files.Length; i++)
  112. {
  113. // var file = files[i];
  114. var file = fileNames[ind + i];
  115. var filePath = Path.Combine(directoryPath, file);
  116. var newFilePath = Path.Combine(basePath, file);
  117. System.IO.File.Move(filePath, newFilePath);
  118. }
  119. }
  120. foreach (var directory in directoryNames) Directory.Delete(Path.Combine(basePathTemporary, directory), true);
  121. foreach (var formFile in model.Files)
  122. {
  123. if (formFile.Length <= 0)
  124. continue;
  125. var filePath = Path.Combine(basePath, formFile.FileName);
  126. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  127. await formFile.CopyToAsync(stream);
  128. message.FileNames.Add(new FileModel { Name = formFile.FileName });
  129. }
  130. }
  131. [AllowAnonymous]
  132. public async Task<FileStreamResult> Download(string filename, Guid code)
  133. {
  134. var path = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, code.ToString(), filename);
  135. var memory = new MemoryStream();
  136. await using var stream = new FileStream(path, FileMode.Open);
  137. await stream.CopyToAsync(memory);
  138. memory.Position = 0;
  139. new FileExtensionContentTypeProvider().TryGetContentType(filename, out var contentType);
  140. if(contentType is null && filename.EndsWith(".7z"))
  141. contentType = "application/octet-stream";
  142. return contentType is null ? null : File(memory, contentType, Path.GetFileName(path));
  143. }
  144. [HttpGet]
  145. public async Task<IActionResult> Link(Guid code, bool? share, bool? edit)
  146. {
  147. var model = await _modelFactory.PrepareLinkVM(code, share, edit);
  148. return View(model);
  149. }
  150. [AllowAnonymous]
  151. [HttpGet]
  152. public async Task<IActionResult> LinkAnonymous(Guid code, bool? edit)
  153. {
  154. var model = await _modelFactory.PrepareLinkAnonymous(code, edit);
  155. return View(model);
  156. }
  157. [AllowAnonymous]
  158. [HttpGet]
  159. public async Task<IActionResult> LinkEdit(Guid code, bool? edit)
  160. {
  161. var model = await _modelFactory.PrepareLinkEdit(code, edit);
  162. return View(model.MessageModel);
  163. }
  164. public IActionResult Privacy()
  165. {
  166. return View();
  167. }
  168. }