Skip to content

Commit

Permalink
Upload, store and serve media data as binary instead of base64 #392
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonononoki committed Apr 1, 2024
1 parent 980956c commit 4578964
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
Expand Up @@ -4,15 +4,16 @@
import java.util.Set;

import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@Data
public class ProfileOnboardingDto {

private Long intention;

private List<Long> preferredGenders;
private String profilePicture;

private String profilePictureMime;

private String description;

Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/nonononoki/alovoa/rest/UserController.java
@@ -1,6 +1,7 @@
package com.nonononoki.alovoa.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nonononoki.alovoa.Tools;
import com.nonononoki.alovoa.entity.user.UserImage;
import com.nonononoki.alovoa.entity.user.UserMiscInfo;
Expand Down Expand Up @@ -31,6 +32,8 @@
@RequestMapping("/user")
public class UserController {

@Autowired
private ObjectMapper objectMapper;
@Autowired
private UserService userService;
@Value("${app.audio.max-size}")
Expand Down Expand Up @@ -61,14 +64,14 @@ public ResponseEntity<Resource> getUserdata(@PathVariable UUID uuid) throws Json
return userService.getUserdata(uuid);
}

@PostMapping(value = "/onboarding", consumes = "application/json")
public void onboarding(@RequestBody ProfileOnboardingDto dto) throws AlovoaException, IOException {
userService.onboarding(dto);
}

@PostMapping(value = "/delete/profile-picture")
public void deleteProfilePicture() throws AlovoaException {
userService.deleteProfilePicture();
@PostMapping(value = "/onboarding", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public void onboarding(@RequestParam("file") MultipartFile file, @RequestParam("data") String dto)
throws AlovoaException, IOException {
byte[] bytes = file.getBytes();
if (bytes.length > mediaMaxSize) {
throw new AlovoaException(AlovoaException.MAX_MEDIA_SIZE_EXCEEDED);
}
userService.onboarding(bytes, objectMapper.readValue(dto, ProfileOnboardingDto.class));
}

@PostMapping(value = "/update/profile-picture", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/com/nonononoki/alovoa/service/UserService.java
Expand Up @@ -27,6 +27,7 @@
import org.springframework.http.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
Expand Down Expand Up @@ -381,7 +382,7 @@ public void updateProfilePicture(byte[] bytes, String mimeType) throws AlovoaExc
userRepo.saveAndFlush(user);
}

public void onboarding(ProfileOnboardingDto model) throws AlovoaException, IOException {
public void onboarding(byte[] bytes, ProfileOnboardingDto model) throws AlovoaException, IOException {
User user = authService.getCurrentUser(true);
if (user.getProfilePicture() != null || user.getDescription() != null) {
return;
Expand All @@ -390,7 +391,7 @@ public void onboarding(ProfileOnboardingDto model) throws AlovoaException, IOExc
Date now = new Date();

UserProfilePicture profilePic = new UserProfilePicture();
AbstractMap.SimpleEntry<byte[], String> adjustedImage = adjustPicture(model.getProfilePicture());
AbstractMap.SimpleEntry<byte[], String> adjustedImage = adjustPicture(bytes, model.getProfilePictureMime());
profilePic.setBin(adjustedImage.getKey());
profilePic.setBinMime(adjustedImage.getValue());
user.setProfilePicture(profilePic);
Expand Down Expand Up @@ -611,12 +612,6 @@ public void deleteImage(long id) throws AlovoaException {
}
}

private AbstractMap.SimpleEntry<byte[], String> adjustPicture(String imgB64) throws IOException {
byte[] decodedBytes = Base64.getDecoder().decode(stripB64Type(imgB64));
MediaType mimeType = mediaService.getImageMimeType(imgB64);
return adjustPicture(decodedBytes, Tools.buildMimeTypeString(mimeType));
}

private AbstractMap.SimpleEntry<byte[], String> adjustPicture(byte[] bytes, String mimeType) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bis);
Expand Down Expand Up @@ -872,13 +867,6 @@ public ResponseEntity<Resource> getUserdata(UUID uuid) throws AlovoaException, J
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}

public void deleteProfilePicture() throws AlovoaException {
User user = authService.getCurrentUser(true);
user.setProfilePicture(null);
user.setVerificationPicture(null);
userRepo.saveAndFlush(user);
}

public String getAudio(UUID uuid)
throws NumberFormatException, AlovoaException {
User user = findUserByUuid(uuid);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Expand Up @@ -30,6 +30,9 @@ spring.datasource.url=jdbc:mariadb://localhost:3306/alovoa?createDatabaseIfNotEx

spring.web.locale=en_EN

spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=10MB

spring.security.oauth2.client.registration.google.scope[0]=email
spring.security.oauth2.client.registration.google.scope[1]=profile
spring.security.oauth2.client.registration.facebook.scope[0]=email
Expand Down
Expand Up @@ -200,8 +200,6 @@ void test() throws Exception {
assertEquals(1, authService.getCurrentUser().getImages().size());
userService.deleteImage(authService.getCurrentUser().getImages().get(0).getId());
assertEquals(0, authService.getCurrentUser().getImages().size());
userService.deleteProfilePicture();
assertNull(authService.getCurrentUser().getProfilePicture());
userService.updateProfilePicture(img3, imgMimePng);
assertNotNull(authService.getCurrentUser().getProfilePicture());
userService.updateAudio(Tools.resourceToBytes ("audio/file_example_MP3_700KB.mp3"), "mpeg");
Expand Down

0 comments on commit 4578964

Please sign in to comment.