74. User service implementation - Save profile image (#8)

This commit is contained in:
Art
2021-09-09 17:01:13 +03:00
parent 35fe1a39a2
commit bff1992842
5 changed files with 64 additions and 11 deletions

View File

@ -1,17 +1,13 @@
package net.shyshkin.study.fullstack.supportportal.backend;
import net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.File;
@SpringBootApplication
public class SupportPortalBackendApplication {
public static void main(String[] args) {
SpringApplication.run(SupportPortalBackendApplication.class, args);
new File(FileConstant.USER_FOLDER).mkdirs();
}
}

View File

@ -7,6 +7,7 @@ public class FileConstant {
public static final String USER_FOLDER = System.getProperty("user.home") + "/supportportal/user/";
public static final String DIRECTORY_CREATED = "Created directory for: ";
public static final String DEFAULT_USER_IMAGE_PATH = "/user/image/profile/";
public static final String USER_IMAGE_FILENAME = "avatar.jpg";
public static final String FILE_SAVED_IN_FILE_SYSTEM = "Saved file in file system by name: ";
public static final String DOT = ".";
public static final String FORWARD_SLASH = "/";

View File

@ -2,7 +2,6 @@ package net.shyshkin.study.fullstack.supportportal.backend.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant;
import net.shyshkin.study.fullstack.supportportal.backend.domain.Role;
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
import net.shyshkin.study.fullstack.supportportal.backend.domain.UserPrincipal;
@ -22,11 +21,17 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.transaction.Transactional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static net.shyshkin.study.fullstack.supportportal.backend.constant.FileConstant.*;
@Slf4j
@Service
@RequiredArgsConstructor
@ -81,10 +86,11 @@ public class UserServiceImpl implements UserService {
return addNewUser(newUserDto);
}
private String getTemporaryProfileImageUrl(String username) {
private String generateProfileImageUrl(String userId) {
return ServletUriComponentsBuilder.fromCurrentContextPath()
.path(FileConstant.DEFAULT_USER_IMAGE_PATH)
.pathSegment(username)
.path(DEFAULT_USER_IMAGE_PATH)
.pathSegment(userId)
.pathSegment(USER_IMAGE_FILENAME)
.toUriString();
}
@ -130,7 +136,7 @@ public class UserServiceImpl implements UserService {
newUser.setPassword(encodedPassword);
newUser.setUserId(generateUserId());
newUser.setProfileImageUrl(getTemporaryProfileImageUrl(username));
newUser.setProfileImageUrl(generateProfileImageUrl(newUser.getUserId()));
userRepository.save(newUser);
saveProfileImage(newUser, userDto.getProfileImage());
@ -147,6 +153,18 @@ public class UserServiceImpl implements UserService {
private void saveProfileImage(User user, MultipartFile profileImage) {
if (profileImage == null) return;
Path userFolder = Paths.get(USER_FOLDER, user.getUserId());
try {
if (Files.notExists(userFolder)) {
Files.createDirectories(userFolder);
log.debug(DIRECTORY_CREATED);
}
profileImage.transferTo(userFolder.resolve(USER_IMAGE_FILENAME));
log.debug(FILE_SAVED_IN_FILE_SYSTEM + profileImage.getOriginalFilename());
} catch (IOException exception) {
log.error("Can't save to file", exception);
}
}
@Override