-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[211] Store profile images retrieved using OAuth2 metadata in our dat…
…abase Bug: #211 Signed-off-by: Stéphane Bégaudeau <[email protected]>
- Loading branch information
1 parent
2c14b24
commit 4644fc1
Showing
35 changed files
with
448 additions
and
102 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
74 changes: 74 additions & 0 deletions
74
...ion/src/main/java/com/svalyn/studio/application/controllers/account/AvatarController.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,74 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.svalyn.studio.application.controllers.account; | ||
|
||
import com.svalyn.studio.domain.account.repositories.IAccountRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Controller used to retrieve avatar image. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
@Controller | ||
public class AvatarController { | ||
|
||
private final IAccountRepository accountRepository; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(AvatarController.class); | ||
|
||
public AvatarController(IAccountRepository accountRepository) { | ||
this.accountRepository = Objects.requireNonNull(accountRepository); | ||
} | ||
|
||
@GetMapping(value = "/api/avatars/{username}") | ||
public ResponseEntity<byte[]> getAvatar(@PathVariable String username) { | ||
return this.accountRepository.findByUsername(username) | ||
.filter(account -> account.getImage() != null && account.getImageContentType() != null) | ||
.map(account -> ResponseEntity.ok() | ||
.header(HttpHeaders.CONTENT_TYPE, account.getImageContentType()) | ||
.body(account.getImage())) | ||
.orElse(this.defaultAvatar()); | ||
} | ||
|
||
public ResponseEntity<byte[]> defaultAvatar() { | ||
byte[] content = new byte[] {}; | ||
var avatar = new ClassPathResource("images/avatar.png"); | ||
try { | ||
content = avatar.getContentAsByteArray(); | ||
} catch (IOException exception) { | ||
logger.warn(exception.getMessage(), exception); | ||
} | ||
|
||
return ResponseEntity.ok() | ||
.header(HttpHeaders.CONTENT_TYPE, "image/png") | ||
.body(content); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...cation/src/main/java/com/svalyn/studio/application/services/account/AvatarUrlService.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,42 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.svalyn.studio.application.services.account; | ||
|
||
import com.svalyn.studio.application.services.account.api.IAvatarUrlService; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | ||
|
||
/** | ||
* Used to compute the url of the avatar of a profile. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
@Service | ||
public class AvatarUrlService implements IAvatarUrlService { | ||
@Override | ||
public String imageUrl(String username) { | ||
var currentUri = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri(); | ||
var uri = currentUri.getScheme() + "://" + currentUri.getHost(); | ||
if (currentUri.getPort() != -1) { | ||
uri = uri + ":" + currentUri.getPort(); | ||
} | ||
uri = uri + "/api/avatars/" + username; | ||
return uri; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...n/src/main/java/com/svalyn/studio/application/services/account/api/IAvatarUrlService.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,28 @@ | ||
/* | ||
* Copyright (c) 2023 Stéphane Bégaudeau. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial | ||
* portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | ||
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.svalyn.studio.application.services.account.api; | ||
|
||
/** | ||
* Used to compute the url of the avatar of a profile. | ||
* | ||
* @author sbegaudeau | ||
*/ | ||
public interface IAvatarUrlService { | ||
String imageUrl(String username); | ||
} |
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
Oops, something went wrong.