| public async Task<MessageDto> GetById(int messageId) | public async Task<MessageDto> GetById(int messageId) | ||||
| { | { | ||||
| var result = await _dbContext.Messages.AsNoTracking().FirstOrDefaultAsync(x => x.Id == messageId); | |||||
| var result = await _dbContext.Messages.Include(x=>x.FileNames).AsNoTracking().FirstOrDefaultAsync(x => x.Id == messageId); | |||||
| var mappedResult = _mapper.Map<MessageDto>(result); | var mappedResult = _mapper.Map<MessageDto>(result); | ||||
| return mappedResult; | return mappedResult; | ||||
| } | } | ||||
| public async Task<MessageDto> GetByCode(Guid code) | public async Task<MessageDto> GetByCode(Guid code) | ||||
| { | { | ||||
| var result = await _dbContext.Messages.AsNoTracking().FirstOrDefaultAsync(x => x.Code == code.ToString()); | |||||
| var result = await _dbContext.Messages.Include(x=>x.FileNames).AsNoTracking().FirstOrDefaultAsync(x => x.Code == code.ToString()); | |||||
| var mappedResult = _mapper.Map<MessageDto>(result); | var mappedResult = _mapper.Map<MessageDto>(result); | ||||
| return mappedResult; | return mappedResult; | ||||
| } | } |
| using Microsoft.AspNetCore.Authorization; | |||||
| using System.Text.Json; | |||||
| using Microsoft.AspNetCore.Authorization; | |||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||
| using SecureSharing.Business.Dtos; | using SecureSharing.Business.Dtos; | ||||
| using SecureSharing.Business.Interfaces; | using SecureSharing.Business.Interfaces; | ||||
| [HttpPost] | [HttpPost] | ||||
| public async Task<IActionResult> CreateMessage(MessageModel model) | public async Task<IActionResult> CreateMessage(MessageModel model) | ||||
| { | { | ||||
| if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0) | |||||
| { | |||||
| return Redirect("/"); | |||||
| } | |||||
| var message = new MessageDto { Text = model.Text }; | 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); | |||||
| // var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString()); | |||||
| var basePath = $"C:\\Users\\radivoje.milutinovic\\Downloads\\fajlovitmp\\{message.Code}"; | |||||
| Directory.CreateDirectory(basePath); | |||||
| foreach (var formFile in model.Files) | 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 }); | |||||
| } | |||||
| if (formFile.Length <= 0) continue; | |||||
| // Console.WriteLine(JsonSerializer.Serialize(formFile)); | |||||
| var filePath = Path.Combine(basePath, formFile.FileName); | |||||
| await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite); | |||||
| await formFile.CopyToAsync(stream); | |||||
| message.FileNames.Add(new FileModel { Name = formFile.FileName }); | |||||
| } | } | ||||
| var code = await _messageService.Create(message, model.ChosenPeriod); | var code = await _messageService.Create(message, model.ChosenPeriod); | ||||
| return RedirectToAction("Link", "Home", new { code = code, share = true }); | return RedirectToAction("Link", "Home", new { code = code, share = true }); | ||||
| } | } |
| using SecureSharing.Business.Interfaces; | using SecureSharing.Business.Interfaces; | ||||
| using SecureSharing.Data.Data; | |||||
| using SecureSharing.Models; | using SecureSharing.Models; | ||||
| namespace SecureSharing.Infrastructure; | namespace SecureSharing.Infrastructure; | ||||
| model = new LinkModel | model = new LinkModel | ||||
| { | { | ||||
| MessageModel = new MessageModel { Code = code, Text = message.Text }, Share = share, | |||||
| MessageModel = new MessageModel | |||||
| { | |||||
| Code = code, | |||||
| Text = message.Text, | |||||
| FileNames = message.FileNames.Select(x => x.Name).ToList() | |||||
| }, | |||||
| Share = share, | |||||
| IsValid = message.IsValid | IsValid = message.IsValid | ||||
| }; | }; | ||||
| public Guid Code { get; set; } | public Guid Code { get; set; } | ||||
| public PeriodOfValidity ChosenPeriod { get; set; } | public PeriodOfValidity ChosenPeriod { get; set; } | ||||
| public List<IFormFile> Files { get; init; } = new(); | public List<IFormFile> Files { get; init; } = new(); | ||||
| public List<string> FileNames { get; init; } = new(); | |||||
| // public Dictionary<int, string> AvailablePeriods { get; set; } | // public Dictionary<int, string> AvailablePeriods { get; set; } | ||||
| } | } |
| <label class="label-text">Sharing link avaliability</label> | <label class="label-text">Sharing link avaliability</label> | ||||
| <div class="button-box col-lg-12"> | <div class="button-box col-lg-12"> | ||||
| <div class="single-button-input"> | <div class="single-button-input"> | ||||
| <input id="OneTime" class="radio-input" type="radio" asp-for="ChosenPeriod" value="@PeriodOfValidity.ONE_TIME"> | |||||
| <input id="OneTime" checked="checked" class="radio-input" type="radio" asp-for="ChosenPeriod" value="@PeriodOfValidity.ONE_TIME"> | |||||
| <label for="OneTime" class="label-available"><span style="color:#FFA463">One</span> Time</label> | <label for="OneTime" class="label-available"><span style="color:#FFA463">One</span> Time</label> | ||||
| </div> | </div> | ||||
| <div class="single-button-input"> | <div class="single-button-input"> |
| @{ | @{ | ||||
| Layout = "~/Views/Shared/_Layout.cshtml"; | Layout = "~/Views/Shared/_Layout.cshtml"; | ||||
| } | } | ||||
| @using Newtonsoft.Json | |||||
| @model LinkModel | @model LinkModel | ||||
| <div class="label-text link-show"> | <div class="label-text link-show"> | ||||
| @Model.MessageModel.Text | @Model.MessageModel.Text | ||||
| </div> | </div> | ||||
| @if (Model.MessageModel.Files.Count > 0) | |||||
| { | |||||
| <div class="label-text"> | |||||
| Files: | |||||
| </div> | |||||
| <div class="label-text link-show"> | |||||
| @foreach (var file in Model.MessageModel.FileNames) | |||||
| { | |||||
| <p>@file</p> | |||||
| } | |||||
| </div> | |||||
| } | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| </div> | </div> | ||||
| } | } | ||||
| </div> | </div> | ||||
| <div class="label-text"> | <div class="label-text"> | ||||
| Message: | Message: | ||||
| </div> | </div> | ||||
| <div class="label-text link-show"> | <div class="label-text link-show"> | ||||
| @Model.MessageModel.Text | @Model.MessageModel.Text | ||||
| </div> | </div> | ||||
| <a class="btn btn-light share-button" asp-controller="Home" asp-action="Index">Share new message securely!</a> | <a class="btn btn-light share-button" asp-controller="Home" asp-action="Index">Share new message securely!</a> | ||||
| } | } | ||||
| } | } |
| .label-text { | .label-text { | ||||
| font-size: 23px; | font-size: 23px; | ||||
| padding-left: 4px; | |||||
| } | } | ||||
| .label-text-lower{ | .label-text-lower{ | ||||
| background-color: #90278F; | background-color: #90278F; | ||||
| color: #FFFFFF; | color: #FFFFFF; | ||||
| } | } | ||||
| .share-button { | .share-button { | ||||
| /* Auto layout */ | /* Auto layout */ | ||||