You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HomeController.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.StaticFiles;
  4. using Org.BouncyCastle.Ocsp;
  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 readonly ILogger<HomeController> _logger;
  15. private readonly IMessageService _messageService;
  16. private readonly IModelFactory _modelFactory;
  17. private readonly IWebHostEnvironment _webHostEnvironment;
  18. private const string DefaultPath = "files";
  19. private const string DefaultPathTmp = "filestmp";
  20. public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment)
  21. {
  22. _logger = logger;
  23. _messageService = messageService;
  24. _modelFactory = modelFactory;
  25. _webHostEnvironment = webHostEnvironment;
  26. }
  27. public IActionResult Index()
  28. {
  29. return View();
  30. }
  31. public async Task<string> UploadTemporaryFile()
  32. {
  33. var code = Guid.NewGuid().ToString();
  34. var files = Request.Form.Files.ToList();
  35. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp, code);
  36. Directory.CreateDirectory(basePath);
  37. foreach (var formFile in files)
  38. {
  39. if (formFile.Length <= 0)
  40. continue;
  41. var filePath = Path.Combine(basePath, formFile.FileName);
  42. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  43. await formFile.CopyToAsync(stream);
  44. }
  45. return code;
  46. }
  47. [HttpPost]
  48. public async Task<IActionResult> CreateMessage(MessageModel model)
  49. {
  50. if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0 && string.IsNullOrEmpty(model.FilesAsText))
  51. {
  52. return Redirect("/");
  53. }
  54. var message = new MessageDto { Text = model.Text };
  55. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
  56. var basePathTemporary = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp);
  57. Directory.CreateDirectory(basePath);
  58. var directoryNames = model.FilesAsText
  59. .Split(';')
  60. .Select(x => x.Split(':')[0])
  61. .Where(x => !string.IsNullOrEmpty(x))
  62. .Distinct()
  63. .ToList();
  64. var fileNamesTmp = model.FilesAsText
  65. .Split(';')
  66. .Where(x => !string.IsNullOrEmpty(x))
  67. .ToList();
  68. var fileNames = fileNamesTmp.Select(x => x.Split(':')[1]).ToList();
  69. foreach (var file in fileNames)
  70. {
  71. message.FileNames.Add(new FileModel {Name = file});
  72. }
  73. foreach (var directoryName in directoryNames)
  74. {
  75. var directoryPath = Path.Combine(basePathTemporary, directoryName);
  76. var files = Directory.GetFiles(directoryPath);
  77. for (var i = 0; i < files.Length; i++)
  78. {
  79. // var file = files[i];
  80. var file = fileNames[i];
  81. var filePath = Path.Combine(directoryPath, file);
  82. var newFilePath = Path.Combine(basePath, file);
  83. System.IO.File.Move(filePath, newFilePath);
  84. }
  85. }
  86. foreach (var directory in directoryNames)
  87. {
  88. Directory.Delete(Path.Combine(basePathTemporary, directory), true);
  89. }
  90. foreach (var formFile in model.Files)
  91. {
  92. if (formFile.Length <= 0)
  93. continue;
  94. var filePath = Path.Combine(basePath, formFile.FileName);
  95. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  96. await formFile.CopyToAsync(stream);
  97. message.FileNames.Add(new FileModel { Name = formFile.FileName });
  98. }
  99. var code = await _messageService.Create(message, model.ChosenPeriod);
  100. return RedirectToAction("Link", "Home", new { code = code, share = true });
  101. }
  102. public async Task<FileStreamResult> Download(string filename, Guid code)
  103. {
  104. var path = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, code.ToString(), filename);
  105. var memory = new MemoryStream();
  106. await using var stream = new FileStream(path, FileMode.Open);
  107. await stream.CopyToAsync(memory);
  108. memory.Position = 0;
  109. new FileExtensionContentTypeProvider().TryGetContentType(filename, out var contentType);
  110. return contentType is null ? null : File(memory, contentType,Path.GetFileName(path));
  111. }
  112. [HttpGet]
  113. public async Task<IActionResult> Link(Guid code, bool? share)
  114. {
  115. var model = await _modelFactory.PrepareLinkVM(code, share);
  116. return View(model);
  117. }
  118. public IActionResult Privacy()
  119. {
  120. return View();
  121. }
  122. }