aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorOgnjen Cirkovic <ciraboxkg@gmail.com>2022-03-16 13:42:32 +0100
committerOgnjen Cirkovic <ciraboxkg@gmail.com>2022-03-16 13:42:32 +0100
commit21811505a810392700de63dd132b5924a5bc08c7 (patch)
treed505fdf5d86e5555850c1bc65e3382d36aba389f /backend
parentdd855dc9241274d1d7e80efd208f243810163d23 (diff)
Omoguceno preuzimanje fajlova uz autorizaciju.
Diffstat (limited to 'backend')
-rw-r--r--backend/api/api/Controllers/FileController.cs31
-rw-r--r--backend/api/api/Services/FileService.cs7
-rw-r--r--backend/api/api/Services/IFileService.cs1
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