-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/new upload image #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshuakudo
wants to merge
3
commits into
main
Choose a base branch
from
feat/new-upload-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
9 changes: 0 additions & 9 deletions
9
src/main/java/com/markguiang/backend/event/utils/DateUtils.java
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/main/java/com/markguiang/backend/event/utils/Utils.java
This file contains hidden or 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,18 @@ | ||
| package com.markguiang.backend.event.utils; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
|
|
||
| public class Utils { | ||
| public static boolean onSameDate(OffsetDateTime a, OffsetDateTime b) { | ||
| return a.toLocalDate().equals(b.toLocalDate()); | ||
| } | ||
|
|
||
| public static String concatenateStr(String... s) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (String string : s) { | ||
| sb.append(string); | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } | ||
| } |
111 changes: 111 additions & 0 deletions
111
src/main/java/com/markguiang/backend/infrastructure/storage/GCPObjectStore.java
This file contains hidden or 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,111 @@ | ||
| package com.markguiang.backend.infrastructure.storage; | ||
|
|
||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.cloud.storage.*; | ||
| import com.markguiang.backend.infrastructure.storage.base.ObjectStore; | ||
| import com.markguiang.backend.infrastructure.storage.base.StorageDetails; | ||
| import com.markguiang.backend.infrastructure.storage.base.StoreProperties; | ||
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static com.markguiang.backend.event.utils.Utils.concatenateStr; | ||
|
|
||
| @Component | ||
| @Primary | ||
| public class GCPObjectStore implements ObjectStore { | ||
| @Value("${GCP_PROJECTID}") | ||
| private String projectId; | ||
|
|
||
| @Value("${GCP_BUCKET_PUBLIC}") | ||
| private String publicBucket; | ||
|
|
||
| @Value("${GCP_BUCKET_PRIVATE}") | ||
| private String privateBucket; | ||
|
|
||
| @Value("${GOOGLE_CLOUD_STORAGE_JSON}") | ||
| private String sa; | ||
|
|
||
| private static final String SUFFIX = "/"; | ||
| private static final String BUCKET_PATH_EVENT = "event/"; | ||
|
|
||
| private static final String BUCKET_PATH_USER = "users/"; | ||
|
|
||
| private StoreProperties initProperties(Boolean isPublic) { | ||
| return StoreProperties | ||
| .builder() | ||
| .projectId(projectId) | ||
| .bucket(isPublic ? publicBucket : privateBucket) | ||
| .sa(sa) | ||
| .build(); | ||
| } | ||
|
|
||
| public URL generatePresignedUrlForDownload(String key) throws IOException { | ||
| return generateSignedUrl(key); | ||
| } | ||
|
|
||
| public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, boolean isPublic) throws IOException { | ||
| String filename = RandomStringUtils.randomAlphabetic(20) + "." + fileExtension; | ||
|
|
||
| String filePath = concatenateStr(BUCKET_PATH_EVENT, id.toString(), SUFFIX, filename); | ||
|
|
||
| URL signedUrl = generateV4PutObjectSignedUrl(initProperties(isPublic), filePath, fileType); | ||
|
|
||
| return new StorageDetails(filePath, signedUrl); | ||
| } | ||
|
|
||
|
|
||
| private URL generateV4PutObjectSignedUrl(StoreProperties properties, String path, String contentType) throws IOException { | ||
|
|
||
| Storage storage = StorageOptions.newBuilder().setProjectId(properties.getProjectId()).build().getService(); | ||
|
|
||
| BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(properties.getBucket(), path)).build(); | ||
|
|
||
| Map<String, String> extensionHeaders = new HashMap<>(); | ||
| extensionHeaders.put("Content-Type", contentType); | ||
|
|
||
| return storage.signUrl( | ||
| blobInfo, | ||
| 15, | ||
| TimeUnit.MINUTES, | ||
| Storage.SignUrlOption.httpMethod(HttpMethod.PUT), | ||
| Storage.SignUrlOption.withExtHeaders(extensionHeaders), | ||
| Storage.SignUrlOption.withV4Signature(), | ||
| Storage.SignUrlOption.signWith( | ||
| ServiceAccountCredentials | ||
| .fromStream(new ByteArrayInputStream(properties.getSa().getBytes(StandardCharsets.UTF_8))) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public URL generateSignedUrl(String objectPath) throws IOException { | ||
| Storage storage = StorageOptions.newBuilder() | ||
| .setProjectId(projectId) | ||
| .setCredentials( | ||
| ServiceAccountCredentials.fromStream( | ||
| new ByteArrayInputStream(sa.getBytes(StandardCharsets.UTF_8)) // since sa is a JSON string | ||
| ) | ||
| ) | ||
| .build() | ||
| .getService(); | ||
|
|
||
| BlobInfo blobInfo = BlobInfo.newBuilder(privateBucket, objectPath).build(); | ||
|
|
||
| return storage.signUrl( | ||
| blobInfo, | ||
| 15, | ||
| TimeUnit.MINUTES, | ||
| Storage.SignUrlOption.withV4Signature() | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 5 additions & 3 deletions
8
src/main/java/com/markguiang/backend/infrastructure/storage/base/ObjectStore.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| package com.markguiang.backend.infrastructure.storage.base; | ||
|
|
||
| import java.net.URI; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.util.UUID; | ||
|
|
||
| public interface ObjectStore { | ||
| public URI generatePresignedUrlForDownload(String key); | ||
| public URL generatePresignedUrlForDownload(String key) throws IOException; | ||
|
|
||
| public URI generatePresignedUrlForUpload(String key); | ||
| public StorageDetails generatePresignedUrlForUpload(UUID id, String fileType, String fileExtension, boolean isPublic) throws IOException; | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/markguiang/backend/infrastructure/storage/base/StorageDetails.java
This file contains hidden or 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 @@ | ||
| package com.markguiang.backend.infrastructure.storage.base; | ||
|
|
||
| import java.net.URL; | ||
|
|
||
| public record StorageDetails(String filePath, URL url) { | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StorageService is a generic component that should not be exposed to implementation details. The existing ObjectStore is already the parent class of the new GCPObjectStore.
What you need to do is create another StorageService bean and use it in non dev controllers.
So you would have devStorageService and storageService beans. One would use LocalObjectStore and the other would use GCPObjectStore, both of these are implementations of the ObjectStore class. So you would need no changes in the dependencies of the StorageService class.
And you would use each in the different storageControllers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 controllers???
devcontroller, storagecontroller, gcpcontroller (will change it to ProdStorageController)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just create multiple ObjectStore beans for the different profiles and make spring insert them into the single storageService bean