diff options
Diffstat (limited to 'backend/api')
-rw-r--r-- | backend/api/api/Controllers/FileController.cs | 103 | ||||
-rw-r--r-- | backend/api/api/Controllers/PredictorController.cs | 10 | ||||
-rw-r--r-- | backend/api/api/Models/FileModel.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Models/PredictorColumns.cs | 8 | ||||
-rw-r--r-- | backend/api/api/Services/PredictorService.cs | 2 |
5 files changed, 118 insertions, 6 deletions
diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs index 0fe8415b..d29c5676 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -4,6 +4,7 @@ using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; + namespace api.Controllers { [Route("api/[controller]")] @@ -11,6 +12,7 @@ namespace api.Controllers public class FileController : ControllerBase { private string[] permittedExtensions = { ".csv" }; + private string[] permittedExtensionsH5 = { ".h5" };//niz da bi dodali h4 itd private readonly IConfiguration _configuration; private IJwtToken _token; private IFileService _fileservice; @@ -22,6 +24,77 @@ namespace api.Controllers } + [HttpPost("h5")] + [Authorize(Roles = "User,Guest")] + public async Task<ActionResult<string>> H5Upload([FromForm] IFormFile file) + { + + //get username from jwtToken + string uploaderId; + string folderName; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + uploaderId = _token.TokenToId(parameter); + if (uploaderId == null) + return null; + } + else + return BadRequest(); + if (uploaderId == "") + { + folderName = "TempFiles"; + } + else + { + folderName = "UploadedFiles"; + } + + + //Check filetype + var filename = file.FileName; + var ext = Path.GetExtension(filename).ToLowerInvariant(); + var name = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant(); + if (string.IsNullOrEmpty(ext) || !permittedExtensionsH5.Contains(ext)) + { + return BadRequest("Wrong file type"); + } + var folderPath = Path.Combine(Directory.GetCurrentDirectory(), folderName, uploaderId); + //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.type = "h5"; + fileModel.path = fullPath; + fileModel.uploaderId = uploaderId; + fileModel.date = DateTime.Now.ToUniversalTime(); + fileModel = _fileservice.Create(fileModel); + + + return Ok(fileModel); + } + [HttpPost("Csv")] [Authorize(Roles = "User,Guest")] @@ -81,6 +154,7 @@ namespace api.Controllers await file.CopyToAsync(stream); } FileModel fileModel= new FileModel(); + fileModel.type = "csv"; fileModel.path=fullPath; fileModel.uploaderId= uploaderId; fileModel.date = DateTime.Now.ToUniversalTime(); @@ -90,6 +164,35 @@ namespace api.Controllers return Ok(fileModel); } + + //msm generalno moze da se koristi Download samo + [HttpGet("downloadh5")] + [Authorize(Roles = "User,Guest")] + public async Task<ActionResult> DownloadH5(string id) + { + //Get Username + string uploaderId; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + uploaderId = _token.TokenToId(parameter); + if (uploaderId == null) + return null; + } + else + return BadRequest(); + + string filePath = _fileservice.GetFilePath(id, uploaderId); + if (filePath == null) + return BadRequest(); + + return File(System.IO.File.ReadAllBytes(filePath), "application/octet-stream", Path.GetFileName(filePath)); + + } + [HttpGet("Download")] [Authorize(Roles = "User,Guest")] public async Task<ActionResult> DownloadFile(string id) diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs index cdc14632..161271e2 100644 --- a/backend/api/api/Controllers/PredictorController.cs +++ b/backend/api/api/Controllers/PredictorController.cs @@ -77,7 +77,7 @@ namespace api.Controllers // GET api/<PredictorController>/getpredictor/{name} [HttpGet("getpredictor/{id}")] - [Authorize(Roles = "User")] + [Authorize(Roles = "User,Guest")] public ActionResult<Predictor> GetPredictor(string id) { string username; @@ -188,8 +188,8 @@ namespace api.Controllers // POST api/<PredictorController>/usepredictor {predictor,inputs} [HttpPost("usepredictor/{id}")] - [Authorize(Roles = "User")] - public ActionResult UsePredictor(String id, [FromBody] String[] inputs) + [Authorize(Roles = "User,Guest")] + public ActionResult UsePredictor(String id, [FromBody] PredictorColumns[] inputs) { string username; @@ -207,8 +207,8 @@ namespace api.Controllers Predictor predictor = _predictorService.GetPredictor(username, id); - foreach(String i in inputs) - Debug.WriteLine(i); + foreach(PredictorColumns i in inputs) + Debug.WriteLine(i.value.ToString()); return NoContent(); } diff --git a/backend/api/api/Models/FileModel.cs b/backend/api/api/Models/FileModel.cs index 1043309d..47b12110 100644 --- a/backend/api/api/Models/FileModel.cs +++ b/backend/api/api/Models/FileModel.cs @@ -8,6 +8,7 @@ namespace api.Models [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string _id { get; set; } + public string type { get; set; } public string uploaderId { get; set; } public string path { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Utc)] diff --git a/backend/api/api/Models/PredictorColumns.cs b/backend/api/api/Models/PredictorColumns.cs new file mode 100644 index 00000000..82f3e979 --- /dev/null +++ b/backend/api/api/Models/PredictorColumns.cs @@ -0,0 +1,8 @@ +namespace api.Models +{ + public class PredictorColumns + { + public String name { get; set; } + public String value { get; set; } + } +} diff --git a/backend/api/api/Services/PredictorService.cs b/backend/api/api/Services/PredictorService.cs index 01bc8359..b15255ac 100644 --- a/backend/api/api/Services/PredictorService.cs +++ b/backend/api/api/Services/PredictorService.cs @@ -42,7 +42,7 @@ namespace api.Services } public Predictor GetPredictor(string username, string id) { - return _predictor.Find(predictor => predictor.username == username && predictor._id == id).FirstOrDefault(); + return _predictor.Find(predictor => predictor._id == id && (predictor.username == username || predictor.isPublic == true)).FirstOrDefault(); } //last private models |