From dd855dc9241274d1d7e80efd208f243810163d23 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 16 Mar 2022 13:13:11 +0100 Subject: Promenjeno ime FileUploadController u FileController. --- backend/api/api/Controllers/FileController.cs | 83 ++++++++++++++++++++++ .../api/api/Controllers/FileUploadController.cs | 83 ---------------------- 2 files changed, 83 insertions(+), 83 deletions(-) create mode 100644 backend/api/api/Controllers/FileController.cs delete mode 100644 backend/api/api/Controllers/FileUploadController.cs (limited to 'backend/api') diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs new file mode 100644 index 00000000..395c8cea --- /dev/null +++ b/backend/api/api/Controllers/FileController.cs @@ -0,0 +1,83 @@ +using System.Net.Http.Headers; +using api.Models; +using api.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Net.Http.Headers; +namespace api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class FileController : ControllerBase + { + private string[] permittedExtensions = { ".csv" }; + private readonly IConfiguration _configuration; + private JwtToken _token; + private IFileService _fileservice; + public FileController(IConfiguration configuration,IFileService fileService) + { + _configuration = configuration; + _token = new JwtToken(configuration); + _fileservice = fileService; + + } + + + [HttpPost("Csv")] + [Authorize(Roles = "User")] + public async Task> CsvUpload([FromForm]IFormFile file) + { + + //get username from jwtToken + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = _token.TokenToUsername(parameter); + if (username == null) + return null; + }else + return BadRequest(); + + + //Check filetype + var filename=file.FileName; + var ext=Path.GetExtension(filename).ToLowerInvariant(); + var name = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant(); + if (string.IsNullOrEmpty(ext) || ! permittedExtensions.Contains(ext)) { + return BadRequest("Wrong file type"); + } + var folderPath=Path.Combine(Directory.GetCurrentDirectory(),"UploadedFiles",username); + //Check Directory + if (!Directory.Exists(folderPath)) + { + Directory.CreateDirectory(folderPath); + } + //Index file if same filename + var fullPath = Path.Combine(folderPath, filename); + int i=0; + + while (System.IO.File.Exists(fullPath)) { + i++; + fullPath = Path.Combine(folderPath,name+i.ToString()+ext); + } + + + //Write file + using (var stream=new FileStream(fullPath, FileMode.Create)) + { + await file.CopyToAsync(stream); + } + FileModel fileModel= new FileModel(); + fileModel.path=fullPath; + fileModel.username=username; + fileModel=_fileservice.Create(fileModel); + + return Ok(fileModel); + } + } +} + diff --git a/backend/api/api/Controllers/FileUploadController.cs b/backend/api/api/Controllers/FileUploadController.cs deleted file mode 100644 index 3869a6fe..00000000 --- a/backend/api/api/Controllers/FileUploadController.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Net.Http.Headers; -using api.Models; -using api.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Net.Http.Headers; -namespace api.Controllers -{ - [Route("api/[controller]")] - [ApiController] - public class FileUploadController : ControllerBase - { - private string[] permittedExtensions = { ".csv" }; - private readonly IConfiguration _configuration; - private JwtToken _token; - private IFileService _fileservice; - public FileUploadController(IConfiguration configuration,IFileService fileService) - { - _configuration = configuration; - _token = new JwtToken(configuration); - _fileservice = fileService; - - } - - - [HttpPost("Csv")] - [Authorize(Roles = "User")] - public async Task> CsvUpload([FromForm]IFormFile file) - { - - //get username from jwtToken - string username; - var header = Request.Headers[HeaderNames.Authorization]; - if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) - { - - var scheme = headerValue.Scheme; - var parameter = headerValue.Parameter; - username = _token.TokenToUsername(parameter); - if (username == null) - return null; - }else - return BadRequest(); - - - //Check filetype - var filename=file.FileName; - var ext=Path.GetExtension(filename).ToLowerInvariant(); - var name = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant(); - if (string.IsNullOrEmpty(ext) || ! permittedExtensions.Contains(ext)) { - return BadRequest("Wrong file type"); - } - var folderPath=Path.Combine(Directory.GetCurrentDirectory(),"UploadedFiles",username); - //Check Directory - if (!Directory.Exists(folderPath)) - { - Directory.CreateDirectory(folderPath); - } - //Index file if same filename - var fullPath = Path.Combine(folderPath, filename); - int i=0; - - while (System.IO.File.Exists(fullPath)) { - i++; - fullPath = Path.Combine(folderPath,name+i.ToString()+ext); - } - - - //Write file - using (var stream=new FileStream(fullPath, FileMode.Create)) - { - await file.CopyToAsync(stream); - } - FileModel fileModel= new FileModel(); - fileModel.path=fullPath; - fileModel.username=username; - fileModel=_fileservice.Create(fileModel); - - return Ok(fileModel); - } - } -} - -- cgit v1.2.3