Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. await UploadFiles(model, message);
  56. var code = await _messageService.Create(message, model.ChosenPeriod);
  57. return RedirectToAction("Link", "Home", new { code = code, share = true });
  58. }
  59. private async Task UploadFiles(MessageModel model, MessageDto message)
  60. {
  61. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
  62. var basePathTemporary = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp);
  63. Directory.CreateDirectory(basePath);
  64. var directoryNames = model.FilesAsText
  65. .Split(';')
  66. .Select(x => x.Split(':')[0])
  67. .Where(x => !string.IsNullOrEmpty(x))
  68. .Distinct()
  69. .ToList();
  70. var fileNamesTmp = model.FilesAsText
  71. .Split(';')
  72. .Where(x => !string.IsNullOrEmpty(x))
  73. .ToList();
  74. var fileNames = fileNamesTmp.Select(x => x.Split(':')[1]).ToList();
  75. foreach (var file in fileNames)
  76. {
  77. message.FileNames.Add(new FileModel { Name = file });
  78. }
  79. foreach (var directoryName in directoryNames)
  80. {
  81. var directoryPath = Path.Combine(basePathTemporary, directoryName);
  82. var files = Directory.GetFiles(directoryPath);
  83. for (var i = 0; i < files.Length; i++)
  84. {
  85. // var file = files[i];
  86. var file = fileNames[i];
  87. var filePath = Path.Combine(directoryPath, file);
  88. var newFilePath = Path.Combine(basePath, file);
  89. System.IO.File.Move(filePath, newFilePath);
  90. }
  91. }
  92. foreach (var directory in directoryNames)
  93. {
  94. Directory.Delete(Path.Combine(basePathTemporary, directory), true);
  95. }
  96. foreach (var formFile in model.Files)
  97. {
  98. if (formFile.Length <= 0)
  99. continue;
  100. var filePath = Path.Combine(basePath, formFile.FileName);
  101. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  102. await formFile.CopyToAsync(stream);
  103. message.FileNames.Add(new FileModel { Name = formFile.FileName });
  104. }
  105. }
  106. public async Task<FileStreamResult> Download(string filename, Guid code)
  107. {
  108. var path = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, code.ToString(), filename);
  109. var memory = new MemoryStream();
  110. await using var stream = new FileStream(path, FileMode.Open);
  111. await stream.CopyToAsync(memory);
  112. memory.Position = 0;
  113. new FileExtensionContentTypeProvider().TryGetContentType(filename, out var contentType);
  114. return contentType is null ? null : File(memory, contentType,Path.GetFileName(path));
  115. }
  116. [HttpGet]
  117. public async Task<IActionResult> Link(Guid code, bool? share)
  118. {
  119. var model = await _modelFactory.PrepareLinkVM(code, share);
  120. return View(model);
  121. }
  122. public IActionResult Privacy()
  123. {
  124. return View();
  125. }
  126. }