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

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