|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.AspNetCore.StaticFiles; |
|
|
|
using Org.BouncyCastle.Ocsp; |
|
|
|
using SecureSharing.Business.Dtos; |
|
|
|
using SecureSharing.Business.Interfaces; |
|
|
|
using SecureSharing.Data.Data; |
|
|
|
@@ -17,6 +18,7 @@ public sealed class HomeController : Controller |
|
|
|
private readonly IModelFactory _modelFactory; |
|
|
|
private readonly IWebHostEnvironment _webHostEnvironment; |
|
|
|
private const string DefaultPath = "files"; |
|
|
|
private const string DefaultPathTmp = "filestmp"; |
|
|
|
|
|
|
|
public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment) |
|
|
|
{ |
|
|
|
@@ -31,11 +33,30 @@ public sealed class HomeController : Controller |
|
|
|
return View(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<string> UploadTemporaryFile() |
|
|
|
{ |
|
|
|
var code = Guid.NewGuid().ToString(); |
|
|
|
var files = Request.Form.Files.ToList(); |
|
|
|
var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp, code); |
|
|
|
Directory.CreateDirectory(basePath); |
|
|
|
|
|
|
|
foreach (var formFile in files) |
|
|
|
{ |
|
|
|
if (formFile.Length <= 0) |
|
|
|
continue; |
|
|
|
|
|
|
|
var filePath = Path.Combine(basePath, formFile.FileName); |
|
|
|
await using var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite); |
|
|
|
await formFile.CopyToAsync(stream); |
|
|
|
} |
|
|
|
|
|
|
|
return code; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
public async Task<IActionResult> CreateMessage(MessageModel model) |
|
|
|
{ |
|
|
|
if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0) |
|
|
|
if (string.IsNullOrWhiteSpace(model.Text) && model.Files.Count == 0 && string.IsNullOrEmpty(model.FilesAsText)) |
|
|
|
{ |
|
|
|
return Redirect("/"); |
|
|
|
} |
|
|
|
@@ -43,8 +64,46 @@ public sealed class HomeController : Controller |
|
|
|
var message = new MessageDto { Text = model.Text }; |
|
|
|
|
|
|
|
var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString()); |
|
|
|
var basePathTemporary = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPathTmp); |
|
|
|
Directory.CreateDirectory(basePath); |
|
|
|
|
|
|
|
var directoryNames = model.FilesAsText |
|
|
|
.Split(';') |
|
|
|
.Select(x => x.Split(':')[0]) |
|
|
|
.Where(x => !string.IsNullOrEmpty(x)) |
|
|
|
.Distinct() |
|
|
|
.ToList(); |
|
|
|
|
|
|
|
var fileNamesTmp = model.FilesAsText |
|
|
|
.Split(';') |
|
|
|
.Where(x => !string.IsNullOrEmpty(x)) |
|
|
|
.ToList(); |
|
|
|
var fileNames = fileNamesTmp.Select(x => x.Split(':')[1]).ToList(); |
|
|
|
|
|
|
|
foreach (var file in fileNames) |
|
|
|
{ |
|
|
|
message.FileNames.Add(new FileModel {Name = file}); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var directoryName in directoryNames) |
|
|
|
{ |
|
|
|
var directoryPath = Path.Combine(basePathTemporary, directoryName); |
|
|
|
var files = Directory.GetFiles(directoryPath); |
|
|
|
for (var i = 0; i < files.Length; i++) |
|
|
|
{ |
|
|
|
// var file = files[i]; |
|
|
|
var file = fileNames[i]; |
|
|
|
var filePath = Path.Combine(directoryPath, file); |
|
|
|
var newFilePath = Path.Combine(basePath, file); |
|
|
|
System.IO.File.Move(filePath, newFilePath); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var directory in directoryNames) |
|
|
|
{ |
|
|
|
Directory.Delete(Path.Combine(basePathTemporary, directory), true); |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var formFile in model.Files) |
|
|
|
{ |
|
|
|
if (formFile.Length <= 0) |
|
|
|
@@ -55,7 +114,7 @@ public sealed class HomeController : Controller |
|
|
|
await formFile.CopyToAsync(stream); |
|
|
|
message.FileNames.Add(new FileModel { Name = formFile.FileName }); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var code = await _messageService.Create(message, model.ChosenPeriod); |
|
|
|
return RedirectToAction("Link", "Home", new { code = code, share = true }); |
|
|
|
} |