diff options
Diffstat (limited to 'backend/api')
-rw-r--r-- | backend/api/api/Controllers/FileController.cs | 31 | ||||
-rw-r--r-- | backend/api/api/Services/FileService.cs | 7 | ||||
-rw-r--r-- | backend/api/api/Services/IFileService.cs | 1 |
3 files changed, 38 insertions, 1 deletions
diff --git a/backend/api/api/Controllers/FileController.cs b/backend/api/api/Controllers/FileController.cs index 395c8cea..3bfdad93 100644 --- a/backend/api/api/Controllers/FileController.cs +++ b/backend/api/api/Controllers/FileController.cs @@ -78,6 +78,35 @@ namespace api.Controllers return Ok(fileModel); } + + [HttpGet("Download")] + [Authorize(Roles = "User")] + public async Task<ActionResult> DownloadFile(string id) + { + //Get Username + 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(); + + string filePath = _fileservice.GetFilePath(id, username); + if (filePath == null) + return BadRequest(); + + return File(System.IO.File.ReadAllBytes(filePath),"application/octet-stream", Path.GetFileName(filePath)); + + } + + + } } - diff --git a/backend/api/api/Services/FileService.cs b/backend/api/api/Services/FileService.cs index 29520387..e68a0fe3 100644 --- a/backend/api/api/Services/FileService.cs +++ b/backend/api/api/Services/FileService.cs @@ -23,5 +23,12 @@ namespace api.Services return file; } + public string GetFilePath(string id, string username) + { + FileModel file = _file.Find(x => x._id == id && x.username == username).FirstOrDefault(); + if (file == null) + return null; + return file.path; + } } } diff --git a/backend/api/api/Services/IFileService.cs b/backend/api/api/Services/IFileService.cs index 14d843ca..7446e283 100644 --- a/backend/api/api/Services/IFileService.cs +++ b/backend/api/api/Services/IFileService.cs @@ -5,5 +5,6 @@ namespace api.Services public interface IFileService { FileModel Create(FileModel file); + string GetFilePath(string id, string username); } }
\ No newline at end of file |