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

HomeController.cs 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 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. var basePath = $"C:\\Users\\radivoje.milutinovic\\Downloads\\fajlovitmp\\{message.Code}";
  39. Directory.CreateDirectory(basePath);
  40. foreach (var formFile in model.Files)
  41. {
  42. if (formFile.Length <= 0) continue;
  43. // Console.WriteLine(JsonSerializer.Serialize(formFile));
  44. var filePath = Path.Combine(basePath, formFile.FileName);
  45. await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  46. await formFile.CopyToAsync(stream);
  47. message.FileNames.Add(new FileModel { Name = formFile.FileName });
  48. }
  49. var code = await _messageService.Create(message, model.ChosenPeriod);
  50. return RedirectToAction("Link", "Home", new { code = code, share = true });
  51. }
  52. [HttpGet]
  53. public async Task<IActionResult> Link(Guid code, bool? share)
  54. {
  55. var model = await _modelFactory.PrepareLinkVM(code, share);
  56. return View(model);
  57. }
  58. public IActionResult Privacy()
  59. {
  60. return View();
  61. }
  62. }