Images upload for Questions
This commit is contained in:
parent
0e4059936a
commit
14b3b2ddae
|
|
@ -98,6 +98,17 @@ namespace OnlineAssessment.Controllers
|
|||
return Ok(new { isSucess = result });
|
||||
}
|
||||
|
||||
[Route("uploadQuestionImage")]
|
||||
[HttpPost]
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
public async Task<IActionResult> UploadQuestionImageAsync(int practice_id, IFormFile file)
|
||||
{
|
||||
int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
|
||||
int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
|
||||
var result = await _AWSS3FileService.UploadQuestionImage(institute_id, file);
|
||||
return Ok(new { isSucess = result });
|
||||
}
|
||||
|
||||
[Route("filesList")]
|
||||
[HttpGet]
|
||||
[Authorize(Roles = "Admin,Teacher")]
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace OnlineAssessment.Helpers
|
|||
{
|
||||
public interface IAWSS3BucketHelper
|
||||
{
|
||||
Task<bool> UploadFile(System.IO.Stream inputStream, string fileName);
|
||||
Task<string> UploadFile(System.IO.Stream inputStream, string fileName);
|
||||
Task<bool> UploadFileWithMeta(System.IO.Stream inputStream, string fileName, string meta);
|
||||
Task<ListVersionsResponse> FilesList();
|
||||
Task<Stream> GetFile(string key);
|
||||
|
|
@ -27,7 +27,7 @@ namespace OnlineAssessment.Helpers
|
|||
this._amazonS3 = s3Client;
|
||||
this._settings = settings.Value;
|
||||
}
|
||||
public async Task<bool> UploadFile(System.IO.Stream inputStream, string fileName)
|
||||
public async Task<string> UploadFile(System.IO.Stream inputStream, string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -39,9 +39,9 @@ namespace OnlineAssessment.Helpers
|
|||
};
|
||||
PutObjectResponse response = await _amazonS3.PutObjectAsync(request);
|
||||
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
|
||||
return true;
|
||||
return $"https://{_settings.AWSS3.BucketName}.s3.{_settings.AWSS3.Region}.amazonaws.com/{fileName}";
|
||||
else
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -12,15 +12,16 @@ namespace OnlineAssessment.Services
|
|||
{
|
||||
public interface IAWSS3FileService
|
||||
{
|
||||
Task<bool> UploadFile(string uploadFileName);
|
||||
Task<string> UploadFile(string uploadFileName);
|
||||
//Task<bool> UploadProfileImage(int institute_id, int user_id, string uploadFileName);
|
||||
Task<Object> UploadProfileImage(int institute_id, int user_id, IFormFile file);
|
||||
Task<Object> UploadExamImage(int institute_id, int user_id, int exam_id, IFormFile file);
|
||||
Task<Object> UploadPracticeImage(int institute_id, int user_id, int practice_id, IFormFile file);
|
||||
Task<Object> UploadQuestionImage(int institute_id, IFormFile file);
|
||||
|
||||
Task<List<FilePathWithMeta>> FilesList();
|
||||
Task<Stream> GetFile(int institute_id, int user_id, string folder, string key);
|
||||
Task<bool> UpdateFile(UploadFileName uploadFileName, string key);
|
||||
Task<string> UpdateFile(UploadFileName uploadFileName, string key);
|
||||
Task<bool> DeleteFile(string key);
|
||||
Task<bool> DeleteProfileImage(int institute_id, int user_id);
|
||||
}
|
||||
|
|
@ -32,7 +33,7 @@ namespace OnlineAssessment.Services
|
|||
{
|
||||
this._AWSS3BucketHelper = AWSS3BucketHelper;
|
||||
}
|
||||
public async Task<bool> UploadFile(string uploadFileName)
|
||||
public async Task<string> UploadFile(string uploadFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -76,7 +77,7 @@ namespace OnlineAssessment.Services
|
|||
{
|
||||
try
|
||||
{
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png", ".jpg" };
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png", ".jpg", ".jpeg" };
|
||||
string fileExtension = Path.GetExtension(file.FileName.ToString()).ToLower();
|
||||
if (!AllowedFileExtensions.Contains(fileExtension))
|
||||
{
|
||||
|
|
@ -100,15 +101,16 @@ namespace OnlineAssessment.Services
|
|||
{
|
||||
try
|
||||
{
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png" };
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png", ".jpg", ".jpeg" };
|
||||
string fileExtension = Path.GetExtension(file.FileName.ToString()).ToLower();
|
||||
if (!AllowedFileExtensions.Contains(fileExtension))
|
||||
{
|
||||
var message = string.Format("Please Upload image of type .png.");
|
||||
var message = string.Format("Please Upload image of type .png., jpg, jpeg");
|
||||
return message;
|
||||
}
|
||||
string fileName = string.Empty;
|
||||
string folder = "/exams/";
|
||||
fileExtension = ".png";
|
||||
fileName = $"{institute_id.ToString()}{folder}{exam_id.ToString()}{fileExtension}";
|
||||
return await _AWSS3BucketHelper.UploadFile(file.OpenReadStream(), fileName);
|
||||
}
|
||||
|
|
@ -122,7 +124,7 @@ namespace OnlineAssessment.Services
|
|||
{
|
||||
try
|
||||
{
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png" };
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png", ".jpg", ".jpeg" };
|
||||
string fileExtension = Path.GetExtension(file.FileName.ToString()).ToLower();
|
||||
if (!AllowedFileExtensions.Contains(fileExtension))
|
||||
{
|
||||
|
|
@ -131,6 +133,7 @@ namespace OnlineAssessment.Services
|
|||
}
|
||||
string fileName = string.Empty;
|
||||
string folder = "/practices/";
|
||||
fileExtension = ".png";
|
||||
fileName = $"{institute_id.ToString()}{folder}{practice_id.ToString()}{fileExtension}";
|
||||
return await _AWSS3BucketHelper.UploadFile(file.OpenReadStream(), fileName);
|
||||
}
|
||||
|
|
@ -140,6 +143,36 @@ namespace OnlineAssessment.Services
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<Object> UploadQuestionImage(int institute_id, IFormFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Allowed file types
|
||||
IList<string> AllowedFileExtensions = new List<string> { ".png", ".jpg", ".jpeg" };
|
||||
string fileExtension = Path.GetExtension(file.FileName).ToLower();
|
||||
|
||||
if (!AllowedFileExtensions.Contains(fileExtension))
|
||||
{
|
||||
throw new Exception("Please upload an image of type .png, .jpg, or .jpeg");
|
||||
}
|
||||
|
||||
string folder = "/questions/";
|
||||
fileExtension = ".png";
|
||||
|
||||
// Create a unique file name (so older uploads don't get overwritten)
|
||||
string uniqueFileName = $"{institute_id}/{folder}/{Guid.NewGuid()}{fileExtension}";
|
||||
|
||||
// Upload file to S3
|
||||
return await _AWSS3BucketHelper.UploadFile(file.OpenReadStream(), uniqueFileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Image upload failed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<FilePathWithMeta>> FilesList()
|
||||
{
|
||||
try
|
||||
|
|
@ -202,7 +235,7 @@ namespace OnlineAssessment.Services
|
|||
throw ex;
|
||||
}
|
||||
}
|
||||
public async Task<bool> UpdateFile(UploadFileName uploadFileName, string key)
|
||||
public async Task<string> UpdateFile(UploadFileName uploadFileName, string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue