-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(User) :fix upload image api * fix(User) :fix upload image api
- Loading branch information
1 parent
6482913
commit ac9fa32
Showing
11 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...Data/AnalysisData/User/Services/S3FileStorageService/Abstraction/IS3FileStorageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace AnalysisData.Services.S3FileStorageService; | ||
|
||
public interface IS3FileStorageService | ||
{ | ||
Task<string> UploadFileAsync(IFormFile file, string folderName); | ||
} |
38 changes: 38 additions & 0 deletions
38
AnalysisData/AnalysisData/User/Services/S3FileStorageService/S3FileStorageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Amazon; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
|
||
namespace AnalysisData.Services.S3FileStorageService; | ||
|
||
public class S3FileStorageService : IS3FileStorageService | ||
{ | ||
private readonly IAmazonS3 _s3Client; | ||
private readonly string _bucketName; | ||
|
||
public S3FileStorageService(IConfiguration configuration) | ||
{ | ||
_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) | ||
{ | ||
var fileKey = Path.Combine(folderName, file.FileName).Replace("\\", "/"); | ||
|
||
var putRequest = new PutObjectRequest | ||
{ | ||
BucketName = _bucketName, | ||
Key = fileKey, | ||
InputStream = file.OpenReadStream(), | ||
ContentType = file.ContentType | ||
}; | ||
|
||
await _s3Client.PutObjectAsync(putRequest); | ||
|
||
return $"https://{_bucketName}.s3.amazonaws.com/{fileKey}"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
AnalysisData/AnalysisData/User/Services/UserService/Abstraction/IUploadImageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Security.Claims; | ||
|
||
namespace AnalysisData.Services.Abstraction; | ||
|
||
public interface IUploadImageService | ||
{ | ||
Task<bool> UploadImageAsync(ClaimsPrincipal claimsPrincipal, IFormFile file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
AnalysisData/AnalysisData/User/Services/UserService/UploadImageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Security.Claims; | ||
using AnalysisData.Exception; | ||
using AnalysisData.Repository.UserRepository.Abstraction; | ||
using AnalysisData.Services.Abstraction; | ||
using AnalysisData.Services.S3FileStorageService; | ||
|
||
namespace AnalysisData.Services; | ||
|
||
public class UploadImageService : IUploadImageService | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
private readonly IS3FileStorageService _s3FileStorageService; | ||
|
||
public UploadImageService(IUserRepository userRepository, IS3FileStorageService s3FileStorageService) | ||
{ | ||
_userRepository = userRepository; | ||
_s3FileStorageService = s3FileStorageService; | ||
} | ||
|
||
public async Task<bool> UploadImageAsync(ClaimsPrincipal claimsPrincipal, IFormFile file) | ||
{ | ||
var userName = claimsPrincipal.FindFirstValue("username"); | ||
var user = await _userRepository.GetUserByUsernameAsync(userName); | ||
if (user == null) | ||
{ | ||
throw new UserNotFoundException(); | ||
} | ||
|
||
string imageUrl = null; | ||
if (file != null && file.Length > 0) | ||
{ | ||
imageUrl = await _s3FileStorageService.UploadFileAsync(file, "UserImages"); | ||
user.ImageURL = imageUrl; | ||
} | ||
user.ImageURL = imageUrl; | ||
await _userRepository.UpdateUserAsync(user.Id, user); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters