Skip to content

Commit

Permalink
Fix upload image (#87)
Browse files Browse the repository at this point in the history
* feature(User) : profile image add

* fix(User) : add image upload and add server information

* feat(User) : get user information add image url to method

* fix (Test) : fix get user information test
  • Loading branch information
mahdijafariii authored Sep 7, 2024
1 parent fa7198e commit 9d43d37
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
migrationBuilder.InsertData(
table: "Users",
columns: new[] { "Id", "Email", "FirstName", "ImageURL", "LastName", "Password", "PhoneNumber", "RoleId", "Username" },
values: new object[] { new Guid("12cb995a-4885-4ec3-9a3b-126b0ff89602"), "[email protected]", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" });
values: new object[] { new Guid("8ab41e25-44bb-4b43-aa2f-6f697edd425f"), "[email protected]", "admin", null, "admin", "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=", "09131111111", 1, "admin" });

migrationBuilder.CreateIndex(
name: "IX_EntityEdges_EntityIDSource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasData(
new
{
Id = new Guid("12cb995a-4885-4ec3-9a3b-126b0ff89602"),
Id = new Guid("8ab41e25-44bb-4b43-aa2f-6f697edd425f"),
Email = "[email protected]",
FirstName = "admin",
LastName = "admin",
Expand Down
3 changes: 2 additions & 1 deletion AnalysisData/AnalysisData/User/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public async Task<IActionResult> GetUserInformation()
FirstName = result.FirstName,
LastName = result.LastName,
PhoneNumber = result.PhoneNumber,
Email = result.Email
Email = result.Email,
Image = result.ImageURL ?? "User do not have information yet !"
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public class S3FileStorageService : IS3FileStorageService

public S3FileStorageService(IConfiguration configuration)
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(configuration["AWS:AccessKey"], configuration["AWS:SecretKey"]);
var config = new AmazonS3Config
{
ServiceURL = configuration["AWS:ServiceURL"]
};
_s3Client = new AmazonS3Client(awsCredentials, config);
_bucketName = configuration["AWS:BucketName"];
_s3Client = new AmazonS3Client(
configuration["AWS:AccessKey"],
configuration["AWS:SecretKey"],
RegionEndpoint.GetBySystemName(configuration["AWS:Region"])
);
}

public async Task<string> UploadFileAsync(IFormFile file, string folderName)
Expand All @@ -32,8 +33,15 @@ public async Task<string> UploadFileAsync(IFormFile file, string folderName)
ContentType = file.ContentType
};

await _s3Client.PutObjectAsync(putRequest);

return $"https://{_bucketName}.s3.amazonaws.com/{fileKey}";
var response = await _s3Client.PutObjectAsync(putRequest);
if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
var fileUrl = $"https://{_bucketName}.s3.{RegionEndpoint.USEast1.SystemName}.amazonaws.com/{fileKey}";
return fileUrl;
}
else
{
return "Could not upload .";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<bool> UploadImageAsync(ClaimsPrincipal claimsPrincipal, IFormF
string imageUrl = null;
if (file != null && file.Length > 0)
{
imageUrl = await _s3FileStorageService.UploadFileAsync(file, "UserImages");
imageUrl = await _s3FileStorageService.UploadFileAsync(file, "usersProfile");
user.ImageURL = imageUrl;
}
user.ImageURL = imageUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public class GetUserInformationDto
public string LastName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }

public string Image { get; set; }
}
9 changes: 5 additions & 4 deletions AnalysisData/AnalysisData/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"UseS3": true
},
"AWS": {
"Region": "us-west-2",
"AccessKey": "956e557a5a204182972e38802cbb5eba",
"SecretKey": "c4bbe0898e3746e08d33ddcde7e6d55e",
"BucketName": "backend-abriment-com"
"Region": "us-east-1",
"AccessKey": "d022cd7a-8e0f-4e0d-b305-ae64116028c0",
"SecretKey": "19b8c45a60e797da10801b01c5a264804e3cafa07c77ec7bf4c40c09161523f2",
"BucketName": "stardata",
"ServiceURL": "https://s3.ir-thr-at1.arvanstorage.ir"
},
"EmailSettings": {
"SmtpServer": "smtp.gmail.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ public async Task GetUserInformation_ShouldReturnOk_WhenUserExist()
FirstName = "test",
LastName = "test",
Email = "[email protected]",
PhoneNumber = "09111111111"
PhoneNumber = "09111111111",
Image = "User do not have information yet !"

});
Assert.Equal(expectedResponseContent, responseContent);

Expand Down

0 comments on commit 9d43d37

Please sign in to comment.