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 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.StaticFiles;
  4. using SecureSharing.Business.Dtos;
  5. using SecureSharing.Business.Interfaces;
  6. using SecureSharing.Data.Data;
  7. using SecureSharing.Infrastructure;
  8. using SecureSharing.Models;
  9. namespace SecureSharing.Controllers;
  10. [Authorize]
  11. public sealed class HomeController : Controller
  12. {
  13. private readonly ILogger<HomeController> _logger;
  14. private readonly IMessageService _messageService;
  15. private readonly IModelFactory _modelFactory;
  16. private readonly IWebHostEnvironment _webHostEnvironment;
  17. private const string DefaultPath = "files";
  18. public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment)
  19. {
  20. _logger = logger;
  21. _messageService = messageService;
  22. _modelFactory = modelFactory;
  23. _webHostEnvironment = webHostEnvironment;
  24. }
  25. public IActionResult Index()
  26. {
  27. return View();
  28. }
  29. [HttpPost]
  30. public async Task<IActionResult> CreateMessage(MessageModel model)
  31. {
  32. if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0)
  33. {
  34. return Redirect("/");
  35. }
  36. var message = new MessageDto { Text = model.Text };
  37. var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
  38. Directory.CreateDirectory(basePath);
  39. foreach (var formFile in model.Files)
  40. {
  41. if (formFile.Length <= 0)
  42. continue;
  43. var filePath = Path.Combine(basePath, formFile.FileName);
  44. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  45. await formFile.CopyToAsync(stream);
  46. message.FileNames.Add(new FileModel { Name = formFile.FileName });
  47. }
  48. var code = await _messageService.Create(message, model.ChosenPeriod);
  49. return RedirectToAction("Link", "Home", new { code = code, share = true });
  50. }
  51. public async Task<FileStreamResult> Download(string filename, Guid code)
  52. {
  53. var path = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, code.ToString(), filename);
  54. var memory = new MemoryStream();
  55. await using var stream = new FileStream(path, FileMode.Open);
  56. await stream.CopyToAsync(memory);
  57. memory.Position = 0;
  58. new FileExtensionContentTypeProvider().TryGetContentType(filename, out var contentType);
  59. return contentType is null ? null : File(memory, contentType,Path.GetFileName(path));
  60. }
  61. [HttpGet]
  62. public async Task<IActionResult> Link(Guid code, bool? share)
  63. {
  64. var model = await _modelFactory.PrepareLinkVM(code, share);
  65. return View(model);
  66. }
  67. public IActionResult Privacy()
  68. {
  69. return View();
  70. }
  71. }