Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

HomeController.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Text.Json;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  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. public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment)
  18. {
  19. _logger = logger;
  20. _messageService = messageService;
  21. _modelFactory = modelFactory;
  22. _webHostEnvironment = webHostEnvironment;
  23. }
  24. public IActionResult Index()
  25. {
  26. return View();
  27. }
  28. [HttpPost]
  29. public async Task<IActionResult> CreateMessage(MessageModel model)
  30. {
  31. if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0)
  32. {
  33. return Redirect("/");
  34. }
  35. var message = new MessageDto { Text = model.Text };
  36. // var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
  37. var basePath = $"C:\\Users\\radivoje.milutinovic\\Downloads\\fajlovitmp\\{message.Code}";
  38. Directory.CreateDirectory(basePath);
  39. foreach (var formFile in model.Files)
  40. {
  41. if (formFile.Length <= 0) continue;
  42. // Console.WriteLine(JsonSerializer.Serialize(formFile));
  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. [HttpGet]
  52. public async Task<IActionResult> Link(Guid code, bool? share)
  53. {
  54. var model = await _modelFactory.PrepareLinkVM(code, share);
  55. return View(model);
  56. }
  57. public IActionResult Privacy()
  58. {
  59. return View();
  60. }
  61. }