-
Notifications
You must be signed in to change notification settings - Fork 2
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
Change avatar #143
Open
audat03
wants to merge
4
commits into
main
Choose a base branch
from
change-avatar
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
Change avatar #143
Changes from all commits
Commits
Show all changes
4 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 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 |
---|---|---|
|
@@ -31,4 +31,6 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
.env | ||
.env | ||
|
||
src/main/resources/serviceAccountKey.json |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/fjb/sunrise/config/security/FirebaseConfig.java
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,29 @@ | ||
package com.fjb.sunrise.config.security; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import jakarta.annotation.PostConstruct; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class FirebaseConfig { | ||
|
||
@PostConstruct | ||
public void initialize() throws IOException { | ||
FileInputStream serviceAccount = new FileInputStream("src/main/resources/serviceAccountKey.json"); | ||
|
||
FirebaseOptions options = FirebaseOptions.builder() | ||
.setCredentials(GoogleCredentials.fromStream(serviceAccount)) | ||
.setStorageBucket("sun-rise-4ebbb.appspot.com") | ||
.build(); | ||
|
||
if (FirebaseApp.getApps().isEmpty()) { | ||
FirebaseApp.initializeApp(options); | ||
} else { | ||
FirebaseApp.getApps(); | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/com/fjb/sunrise/services/impl/FirebaseStorageService.java
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,57 @@ | ||
package com.fjb.sunrise.services.impl; | ||
|
||
import com.google.auth.Credentials; | ||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
||
|
||
@Service | ||
public class FirebaseStorageService { | ||
|
||
private final Storage storage; | ||
private static final Logger logger = LoggerFactory.getLogger(FirebaseStorageService.class); | ||
|
||
public FirebaseStorageService() throws IOException { | ||
Credentials credentials = GoogleCredentials.fromStream( | ||
Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("serviceAccountKey.json")) | ||
); | ||
storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService(); | ||
} | ||
|
||
public String uploadFile(MultipartFile file, Long userId) throws IOException { | ||
// Generate a unique file name | ||
String fileName = "avatars/userId_" + userId + "/" + UUID.randomUUID().toString() + "-" | ||
+ file.getOriginalFilename(); | ||
|
||
// Ensure the file is not empty | ||
if (file.isEmpty()) { | ||
logger.error("File is empty. Upload failed."); | ||
throw new IOException("Cannot upload an empty file."); | ||
} | ||
|
||
try (InputStream inputStream = file.getInputStream()) { | ||
// Create the BlobId and BlobInfo | ||
BlobId blobId = BlobId.of("sun-rise-4ebbb.appspot.com", fileName); // Replace with your bucket name | ||
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(file.getContentType()).build(); | ||
|
||
// Upload the file to Firebase Storage | ||
storage.create(blobInfo, inputStream); | ||
logger.info("File uploaded to Firebase Storage: {}", fileName); | ||
} | ||
|
||
// Return the publicly accessible URL | ||
return String.format("https://storage.googleapis.com/%s/%s", "sun-rise-4ebbb.appspot.com", fileName); // Correct URL | ||
} | ||
} |
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
Oops, something went wrong.
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.
Check notice
Code scanning / SonarCloud
Logging should not be vulnerable to injection attacks Low