diff options
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/api/api/.gitignore | 1 | ||||
| -rw-r--r-- | backend/api/api/Controllers/AuthController.cs | 6 | ||||
| -rw-r--r-- | backend/api/api/Controllers/DatasetController.cs | 3 | ||||
| -rw-r--r-- | backend/api/api/Controllers/FileController.cs | 21 | ||||
| -rw-r--r-- | backend/api/api/Controllers/ModelController.cs | 5 | ||||
| -rw-r--r-- | backend/api/api/Models/FileModel.cs | 2 | ||||
| -rw-r--r-- | backend/api/api/Models/JwtToken.cs | 16 | ||||
| -rw-r--r-- | backend/api/api/Program.cs | 2 | ||||
| -rw-r--r-- | backend/api/api/Services/AuthService.cs | 5 | ||||
| -rw-r--r-- | backend/api/api/Services/FileService.cs | 1 | ||||
| -rw-r--r-- | backend/api/api/Services/IAuthService.cs | 1 | ||||
| -rw-r--r-- | backend/api/api/Services/TempFileService.cs | 33 | ||||
| -rw-r--r-- | backend/api/api/Services/TempRemovalService.cs | 73 | ||||
| -rw-r--r-- | backend/api/api/api.csproj | 2 | 
14 files changed, 161 insertions, 10 deletions
| diff --git a/backend/api/api/.gitignore b/backend/api/api/.gitignore index 242abea5..9a89b63c 100644 --- a/backend/api/api/.gitignore +++ b/backend/api/api/.gitignore @@ -5,6 +5,7 @@  ##Ignore contents for UploadedFiles Folder  UploadedFiles/* +TempFiles/*  # User-specific files  *.rsuser diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs index 7167d1bf..901454e1 100644 --- a/backend/api/api/Controllers/AuthController.cs +++ b/backend/api/api/Controllers/AuthController.cs @@ -30,6 +30,12 @@ namespace api.Controllers              return Ok(_auth.Login(user));          } +        [HttpPost("guestToken")] +        public async Task<ActionResult<string>> guestToken() +        { + +            return Ok(_auth.GuestToken()); +        }          [HttpGet("Auth")]          [Authorize(Roles ="User")] diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs index 9f2013f5..bae05ba9 100644 --- a/backend/api/api/Controllers/DatasetController.cs +++ b/backend/api/api/Controllers/DatasetController.cs @@ -155,11 +155,12 @@ namespace api.Controllers          // POST api/<DatasetController>/add          [HttpPost("add")] -        [Authorize(Roles = "User")] +        [Authorize(Roles = "User,Guest")]          public ActionResult<Dataset> Post([FromBody] Dataset dataset)          {              //da li ce preko tokena da se ubaci username ili front salje              //dataset.username = usernameToken; +            //username = "" ako je GUEST DODAO              var existingDataset = _datasetService.GetOneDataset(dataset.username, dataset.name);              if (existingDataset != null) diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs index 3bfdad93..a6bab373 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -24,12 +24,13 @@ namespace api.Controllers          [HttpPost("Csv")] -        [Authorize(Roles = "User")] +        [Authorize(Roles = "User,Guest")]          public async Task<ActionResult<string>> CsvUpload([FromForm]IFormFile file)          {              //get username from jwtToken              string username; +            string folderName;              var header = Request.Headers[HeaderNames.Authorization];              if (AuthenticationHeaderValue.TryParse(header, out var headerValue))              { @@ -41,6 +42,14 @@ namespace api.Controllers                      return null;              }else                   return BadRequest(); +            if (username == "") +            { +                folderName = "TempFiles"; +            } +            else +            { +                folderName = "UploadedFiles"; +            }              //Check filetype @@ -50,7 +59,7 @@ namespace api.Controllers              if (string.IsNullOrEmpty(ext) || ! permittedExtensions.Contains(ext)) {                  return BadRequest("Wrong file type");              } -            var folderPath=Path.Combine(Directory.GetCurrentDirectory(),"UploadedFiles",username); +            var folderPath=Path.Combine(Directory.GetCurrentDirectory(),folderName, username);              //Check Directory              if (!Directory.Exists(folderPath))              { @@ -74,13 +83,15 @@ namespace api.Controllers              FileModel fileModel= new FileModel();              fileModel.path=fullPath;              fileModel.username=username; -            fileModel=_fileservice.Create(fileModel); +            fileModel.date = DateTime.Now.ToUniversalTime(); +            fileModel =_fileservice.Create(fileModel); +                          return Ok(fileModel);          }          [HttpGet("Download")] -        [Authorize(Roles = "User")] +        [Authorize(Roles = "User,Guest")]          public async Task<ActionResult> DownloadFile(string id)          {              //Get Username @@ -106,7 +117,5 @@ namespace api.Controllers          } - -      }  } diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 1c5b3763..b4a4b4f2 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -1,4 +1,4 @@ -using api.Models; +using api.Models;  using api.Services;  using Microsoft.AspNetCore.Authorization;  using Microsoft.AspNetCore.Http; @@ -122,9 +122,10 @@ namespace api.Controllers          // POST api/<ModelController>/add          [HttpPost("add")] -        [Authorize(Roles = "User")] +        [Authorize(Roles = "User,Guest")]          public ActionResult<Model> Post([FromBody] Model model)          { +            //username="" ako je GUEST              if (_modelService.CheckHyperparameters(model.inputNeurons, model.hiddenLayerNeurons, model.hiddenLayers, model.outputNeurons) == false)                  return BadRequest("Bad parameters!"); diff --git a/backend/api/api/Models/FileModel.cs b/backend/api/api/Models/FileModel.cs index 9e7c8b5d..30211372 100644 --- a/backend/api/api/Models/FileModel.cs +++ b/backend/api/api/Models/FileModel.cs @@ -10,5 +10,7 @@ namespace api.Models          public string _id { get; set; }          public string username { get; set; }          public string path { get; set; } +        [BsonDateTimeOptions(Kind = DateTimeKind.Utc)] +        public DateTime date { get; set; }      }  } diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index a98d1967..f262fd23 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -69,6 +69,22 @@ namespace api.Models          } +        public string GenGuestToken() +        { +            var tokenHandler = new JwtSecurityTokenHandler(); +            var key = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); +            var tokenDescriptor = new SecurityTokenDescriptor +            { +                Subject = new ClaimsIdentity(new[] { new Claim("name",""), +                                                    new Claim("role", "Guest")}), +                Expires = DateTime.UtcNow.AddMinutes(20), +                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) +            }; +            var token = tokenHandler.CreateToken(tokenDescriptor); +            return tokenHandler.WriteToken(token); + +        } +      } diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 65399bdf..c8cf0784 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -34,6 +34,8 @@ builder.Services.AddScoped<IPredictorService, PredictorService>();  builder.Services.AddScoped<IFileService, FileService>(); +builder.Services.AddHostedService<TempFileService>(); +  //Add Authentication  builder.Services.AddAuthentication(      JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index 4f838463..8ef96e2e 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -57,6 +57,11 @@ namespace api.Services              return null;          } +        public string GuestToken() +        { +            return _jwt.GenGuestToken(); +        } +      }  } diff --git a/backend/api/api/Services/FileService.cs b/backend/api/api/Services/FileService.cs index e68a0fe3..b02d0da4 100644 --- a/backend/api/api/Services/FileService.cs +++ b/backend/api/api/Services/FileService.cs @@ -30,5 +30,6 @@ namespace api.Services                  return null;              return file.path;          } +              }  } diff --git a/backend/api/api/Services/IAuthService.cs b/backend/api/api/Services/IAuthService.cs index 591d122d..9a109208 100644 --- a/backend/api/api/Services/IAuthService.cs +++ b/backend/api/api/Services/IAuthService.cs @@ -7,5 +7,6 @@ namespace api.Services          string Login(AuthRequest user);          string Register(RegisterRequest user);          string RenewToken(string token); +        public string GuestToken();      }  }
\ No newline at end of file diff --git a/backend/api/api/Services/TempFileService.cs b/backend/api/api/Services/TempFileService.cs new file mode 100644 index 00000000..adfe8c75 --- /dev/null +++ b/backend/api/api/Services/TempFileService.cs @@ -0,0 +1,33 @@ +using api.Interfaces; +using MongoDB.Driver; + +namespace api.Services +{ +    public class TempFileService : IHostedService +    { +        private readonly TempRemovalService _removalService; +        private Timer _timer; + +        public TempFileService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) +        { +            _removalService = new TempRemovalService(settings, mongoClient); +        } +        public Task StartAsync(CancellationToken cancellationToken) +        { +            _timer = new Timer(RemoveTempFiles,null,TimeSpan.Zero,TimeSpan.FromHours(6)); + + +            return Task.CompletedTask; +        } + +        public Task StopAsync(CancellationToken cancellationToken) +        { +            _timer?.Change(Timeout.Infinite, 0); +            return Task.CompletedTask; +        } +        private void RemoveTempFiles(object state) +        { +            _removalService.DeleteTemps(); +        } +    } +} diff --git a/backend/api/api/Services/TempRemovalService.cs b/backend/api/api/Services/TempRemovalService.cs new file mode 100644 index 00000000..342304f0 --- /dev/null +++ b/backend/api/api/Services/TempRemovalService.cs @@ -0,0 +1,73 @@ +using api.Interfaces; +using api.Models; +using MongoDB.Driver; + +namespace api.Services +{ +    public class TempRemovalService +    { +        private readonly IMongoCollection<FileModel> _file; +        private readonly IMongoCollection<Model> _model; +        private readonly IMongoCollection<Dataset> _dataset; + +        public TempRemovalService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) +        { +            var database = mongoClient.GetDatabase(settings.DatabaseName); +            _file = database.GetCollection<FileModel>(settings.FilesCollectionName); +            _model= database.GetCollection<Model>(settings.ModelCollectionName); +            _dataset = database.GetCollection<Dataset>(settings.DatasetCollectionName); +        } +        public void DeleteTemps() +        { +            List<FileModel> files = _file.Find(file => file.username == "").ToList(); +            foreach (var file in files) +            { +                if ((DateTime.Now.ToUniversalTime() - file.date).TotalDays >= 1) +                { +                    DeleteFile(file._id); +                    List<Dataset> datasets = _dataset.Find(dataset => dataset.fileId == file._id && dataset.username=="").ToList(); +                    foreach(var dataset in datasets) +                    { +                        DeleteDataset(dataset._id); +                        List<Model> models = _model.Find(model => model.datasetId == dataset._id && model.username=="").ToList(); +                        foreach(var model in models) +                        { +                            DeleteModel(model._id); +                        } +                    } +                    if (File.Exists(file.path)) +                        File.Delete(file.path); +                } +            } +            //Brisanje modela ukoliko gost koristi vec postojeci dataset +            List<Model> models1= _model.Find(model =>model.username == "").ToList(); +            foreach(var model in models1) +            { +                if ((DateTime.Now.ToUniversalTime() - model.dateCreated.ToUniversalTime()).TotalDays >= 1) +                { +                    DeleteModel(model._id); +                } +            } +             + +        } + + + + +        public void DeleteFile(string id) +        { +            _file.DeleteOne(file => file._id == id); +        } +        public void DeleteModel(string id) +        { +            _model.DeleteOne(model=>model._id==id); +        } +        public void DeleteDataset(string id) +        { +            _dataset.DeleteOne(dataset => dataset._id == id); +        } + + +    } +} diff --git a/backend/api/api/api.csproj b/backend/api/api/api.csproj index f63407ca..658f7c05 100644 --- a/backend/api/api/api.csproj +++ b/backend/api/api/api.csproj @@ -21,8 +21,8 @@    </ItemGroup>    <ItemGroup> +    <Folder Include="TempFiles\" />      <Folder Include="UploadedFiles\" /> -    <Folder Include="UploadedFiles\tester1\" />    </ItemGroup>  </Project> | 
