diff options
| author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-03-19 16:15:47 +0100 | 
|---|---|---|
| committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-03-19 16:15:47 +0100 | 
| commit | bfc6000083eac78e411ba39b02b8859101f23706 (patch) | |
| tree | a3e666e6774634644896a9f89999b1a0dae12ea5 /backend/api | |
| parent | e2661ad98598e2458fecb713e0de6e405a78b101 (diff) | |
Omoguceno brisanje temp fajlova.
Diffstat (limited to 'backend/api')
| -rw-r--r-- | backend/api/api/.gitignore | 1 | ||||
| -rw-r--r-- | backend/api/api/Controllers/FileController.cs | 51 | ||||
| -rw-r--r-- | backend/api/api/Models/FileModel.cs | 2 | ||||
| -rw-r--r-- | backend/api/api/Program.cs | 2 | ||||
| -rw-r--r-- | backend/api/api/Services/FileService.cs | 18 | ||||
| -rw-r--r-- | backend/api/api/Services/IFileService.cs | 2 | ||||
| -rw-r--r-- | backend/api/api/Services/TempFileService.cs | 33 | ||||
| -rw-r--r-- | backend/api/api/api.csproj | 2 | 
8 files changed, 109 insertions, 2 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/FileController.cs b/backend/api/api/Controllers/FileController.cs index 3bfdad93..fb3df61a 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -74,7 +74,9 @@ 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);          } @@ -107,6 +109,53 @@ namespace api.Controllers          } +        [HttpPost("TempUpload")] +        public async Task<ActionResult<string>> TempUpload([FromForm] IFormFile file) +        { + +            //get username from jwtToken +            string username = ""; +            //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(), "TempFiles"); +            //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.date = DateTime.Now.ToUniversalTime(); +            fileModel = _fileservice.Create(fileModel); +             + +            return Ok(fileModel); +        } + +      }  } 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/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/FileService.cs b/backend/api/api/Services/FileService.cs index e68a0fe3..7b101af5 100644 --- a/backend/api/api/Services/FileService.cs +++ b/backend/api/api/Services/FileService.cs @@ -30,5 +30,23 @@ namespace api.Services                  return null;              return file.path;          } +        public void Delete(string id) +        { +            _file.DeleteOne(file => file._id == id); + +        } +        public void DeleteTempFiles() +        { +            List<FileModel> files = _file.Find(file => file.username == "").ToList(); +            foreach (var file in files) +            { +                if ((DateTime.Now.ToUniversalTime() - file.date).TotalMinutes >= 2) +                { +                    Delete(file._id); +                    if (File.Exists(file.path)) +                        File.Delete(file.path); +                } +            } +        }      }  } diff --git a/backend/api/api/Services/IFileService.cs b/backend/api/api/Services/IFileService.cs index 7446e283..66bfc93d 100644 --- a/backend/api/api/Services/IFileService.cs +++ b/backend/api/api/Services/IFileService.cs @@ -5,6 +5,8 @@ namespace api.Services      public interface IFileService      {          FileModel Create(FileModel file); +        void Delete(string id); +        void DeleteTempFiles();          string GetFilePath(string id, string username);      }  }
\ 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..300c81ee --- /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 FileService _fileService; +        private Timer _timer; + +        public TempFileService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) +        { +            _fileService = new FileService(settings,mongoClient); +        } +        public Task StartAsync(CancellationToken cancellationToken) +        { +            _timer = new Timer(RemoveTempFiles,null,TimeSpan.Zero,TimeSpan.FromMinutes(1)); + + +            return Task.CompletedTask; +        } + +        public Task StopAsync(CancellationToken cancellationToken) +        { +            _timer?.Change(Timeout.Infinite, 0); +            return Task.CompletedTask; +        } +        private void RemoveTempFiles(object state) +        { +            _fileService.DeleteTempFiles(); +        } +    } +} 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> | 
