using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SecureSharing.Business.Dtos; using SecureSharing.Business.Interfaces; using SecureSharing.Data.Data; using SecureSharing.Infrastructure; using SecureSharing.Models; namespace SecureSharing.Controllers; [Authorize] public sealed class HomeController : Controller { private readonly ILogger _logger; private readonly IMessageService _messageService; private readonly IModelFactory _modelFactory; private readonly IWebHostEnvironment _webHostEnvironment; private const string DefaultPath = "files"; public HomeController(ILogger logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment) { _logger = logger; _messageService = messageService; _modelFactory = modelFactory; _webHostEnvironment = webHostEnvironment; } public IActionResult Index() { return View(); } [HttpPost] public async Task CreateMessage(MessageModel model) { var message = new MessageDto { Text = model.Text }; var currentPath = _webHostEnvironment.WebRootPath.Split('/')[0]; // var filePath = $@"{currentPath}\{DefaultPath}\{message.Code}"; var filePath = Path.Combine(currentPath, DefaultPath, message.Code.ToString()); Directory.CreateDirectory(filePath); foreach (var formFile in model.Files) { if (formFile.Length > 0) { using var stream = new FileStream(filePath, FileMode.Create); await formFile.CopyToAsync(stream); message.FileNames.Add(new FileModel { Name = formFile.Name }); } } var code = await _messageService.Create(message, model.ChosenPeriod); return RedirectToAction("Link", "Home", new { code = code, share = true }); } [HttpGet] public async Task Link(Guid code, bool? share) { var model = await _modelFactory.PrepareLinkVM(code, share); return View(model); } public IActionResult Privacy() { return View(); } }