215 lines
7.5 KiB
C#
215 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using EveryThing.Data;
|
|
using EveryThing.Models;
|
|
using EveryThing.Models.Project;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.IO;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Hosting;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using Microsoft.VisualBasic.FileIO;
|
|
using EveryThing.Models.CodeTable;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EveryThing.Pages.Files
|
|
{
|
|
[Authorize(Roles = "Administrator,ProjecThingUser")]
|
|
public class UploadModel : PageModel
|
|
{
|
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public UploadModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager, IWebHostEnvironment environment)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
_hostingEnvironment = environment;
|
|
}
|
|
|
|
|
|
[BindProperty]
|
|
public IFormFile File { get; set; }
|
|
[BindProperty]
|
|
public int IdReferenceFk { get; set; }
|
|
[BindProperty]
|
|
public int FileTypeInt { get; set; }
|
|
public string Referer { get; set; }
|
|
|
|
public IActionResult OnGet(int idReferenceFk, FileType fileType)
|
|
{
|
|
if (idReferenceFk <= 0 || (int)fileType < 0)
|
|
return NotFound();
|
|
|
|
IdReferenceFk = idReferenceFk;
|
|
FileTypeInt = (int)fileType;
|
|
|
|
Referer = Request.Headers["Referer"].ToString();
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostUpload(int idReferenceFk, int fileTypeInt, List<IFormFile> postedFiles, string referer)
|
|
{
|
|
if (postedFiles is not { Count: > 0 })
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
var fileType = (FileType)fileTypeInt;
|
|
|
|
var path = GetFolder(user.IdCompanyFk, fileType);// Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", "Files", user.IdCompanyFk.ToString(), fileTypeInt.ToString());
|
|
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
|
|
foreach (var formFile in postedFiles)
|
|
{
|
|
var guid = Guid.NewGuid().ToString().Replace("-", "_");
|
|
var extension = Path.GetExtension(formFile.FileName);
|
|
var fileName = guid + extension;
|
|
|
|
await using var stream = new FileStream(Path.Combine(path, fileName), FileMode.Create);
|
|
await formFile.CopyToAsync(stream);
|
|
|
|
var newFile = new Models.File
|
|
{
|
|
IdCompanyFk = user.IdCompanyFk,
|
|
IdReferenceFk = idReferenceFk,
|
|
Title = formFile.FileName,
|
|
Extension = extension,
|
|
Guid = guid,
|
|
Salt = "",
|
|
Iv = "",
|
|
DateOfUpload = DateTime.Now,
|
|
FileType = fileType
|
|
};
|
|
|
|
_context.Files.Add(newFile);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(referer))
|
|
{
|
|
return Redirect(referer);
|
|
}
|
|
|
|
switch (fileType)
|
|
{
|
|
case FileType.ProjectPart:
|
|
return RedirectToPage("/Projects/Edit", new { id = _context.ProjectParts.FirstOrDefault(x => x.IdProjectPart == idReferenceFk)!.IdProjectFk});
|
|
case FileType.Project:
|
|
return RedirectToPage("/Projects/Edit", new { id = idReferenceFk });
|
|
case FileType.CodeTableItem:
|
|
return RedirectToPage("/CodeTableItems/Index");
|
|
default:
|
|
return RedirectToPage("/Index");
|
|
}
|
|
}
|
|
|
|
private string GetFolder(int idCompany, FileType fileType)
|
|
{
|
|
//return Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", "Files", idCompany.ToString(), ((int)fileType).ToString());
|
|
return Path.Combine(_hostingEnvironment.WebRootPath, "Uploads", "Files", ((int)fileType).ToString());
|
|
}
|
|
|
|
public FileResult OnGetDownloadFile(int idFile, int idReferenceFk, int fileTypeInt)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
var fileType = (FileType)fileTypeInt;
|
|
|
|
var file = _context.Files
|
|
.FirstOrDefault(x => x.IdCompanyFk == user.IdCompanyFk
|
|
&& x.IdReferenceFk == idReferenceFk
|
|
&& x.IdFile == idFile);
|
|
|
|
if (file == null)
|
|
return null;
|
|
|
|
var path = Path.Combine(GetFolder(user.IdCompanyFk, fileType), file.Guid + file.Extension);
|
|
|
|
var bytes = System.IO.File.ReadAllBytes(path);
|
|
|
|
return File(bytes, "application/octet-stream", file.Title);
|
|
}
|
|
|
|
public IActionResult OnDeleteFile(int idFile)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
var file = _context.Files
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdFile == idFile);
|
|
|
|
if (file == null)
|
|
{
|
|
return new JsonResult(new { idFile, error = $"File with id {idFile} not exists!", successful = false });
|
|
}
|
|
|
|
var path = Path.Combine(GetFolder(user.IdCompanyFk, file.FileType), file.Guid + file.Extension);
|
|
|
|
if (System.IO.File.Exists(path))
|
|
{
|
|
System.IO.File.Delete(path);
|
|
}
|
|
//else
|
|
//{
|
|
// return new JsonResult(new { idFile, error = "File path not exists!", successful = false });
|
|
//}
|
|
|
|
if (System.IO.File.Exists(path))
|
|
{
|
|
return new JsonResult(new { idFile, error = "File was not deleted from disk!", successful = false });
|
|
}
|
|
|
|
_context.Files.Remove(file);
|
|
_context.SaveChanges();
|
|
|
|
return new JsonResult(new { idFile, error = "", successful = true});
|
|
}
|
|
|
|
public IActionResult OnGetFile(int idFile)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
var successful = true;
|
|
var error = "";
|
|
var inUse = false;
|
|
|
|
var file = _context.Files
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk)
|
|
.FirstOrDefault(x => x.IdFile == idFile);
|
|
|
|
if (file == null)
|
|
{
|
|
successful = false;
|
|
error = $"File with ID: {idFile} not found";
|
|
}
|
|
else
|
|
{
|
|
//TODO prever in use
|
|
inUse = false;
|
|
}
|
|
|
|
return new JsonResult(new { file, error, successful, inUse });
|
|
}
|
|
}
|
|
}
|