From e9fff5e8331ef90299141a8faecf5485b667f696 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Mon, 18 Jul 2022 19:54:02 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[refactor]=20:=20Spring=20Security=20?= =?UTF-8?q?=EC=97=86=EC=9D=B4=20Jwt=20=EB=B0=9C=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hometoogether/config/SecurityConfig.java | 88 ----------------- .../jwt/JwtAuthenticationEntryPoint.java | 23 ----- .../config/jwt/JwtAuthenticationFilter.java | 54 ----------- .../config/jwt/JwtTokenProvider.java | 95 ++++++++++++------- 4 files changed, 59 insertions(+), 201 deletions(-) delete mode 100644 src/main/java/hometoogether/hometoogether/config/SecurityConfig.java delete mode 100644 src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationEntryPoint.java delete mode 100644 src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationFilter.java diff --git a/src/main/java/hometoogether/hometoogether/config/SecurityConfig.java b/src/main/java/hometoogether/hometoogether/config/SecurityConfig.java deleted file mode 100644 index 1b5b740..0000000 --- a/src/main/java/hometoogether/hometoogether/config/SecurityConfig.java +++ /dev/null @@ -1,88 +0,0 @@ -package hometoogether.hometoogether.config; - -import hometoogether.hometoogether.config.jwt.JwtAuthenticationEntryPoint; -import hometoogether.hometoogether.config.jwt.JwtAuthenticationFilter; -import hometoogether.hometoogether.domain.user.service.CustomUserDetailsService; -import lombok.RequiredArgsConstructor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpMethod; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.config.BeanIds; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.config.http.SessionCreationPolicy; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; -import org.springframework.web.cors.CorsUtils; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import org.springframework.web.servlet.config.annotation.CorsRegistry; - -//@Configuration -//@EnableWebSecurity -//@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true) -@RequiredArgsConstructor -public class SecurityConfig extends WebSecurityConfigurerAdapter { - -// private final JwtAuthenticationEntryPoint unauthorizedHandler; -// private final CustomUserDetailsService customUserDetailsService; -// private final JwtAuthenticationFilter jwtAuthenticationFilter; -// -// -// @Override -// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { -//// authenticationManagerBuilder.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder()); -// authenticationManagerBuilder.userDetailsService(customUserDetailsService); -// } -// -// @Bean(BeanIds.AUTHENTICATION_MANAGER) -// @Override -// public AuthenticationManager authenticationManagerBean() throws Exception { -// return super.authenticationManagerBean(); -// } -// -// @Bean -// public PasswordEncoder passwordEncoder() { -// return new BCryptPasswordEncoder(); -// } -// -// @Override -// protected void configure(HttpSecurity http) throws Exception { -// // For CORS error -//// http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues()); -//// http.authorizeRequests().requestMatchers(CorsUtils::isPreFlightRequest).permitAll(); -//// http.csrf().disable() -//// // dont authenticate this particular request -//// .authorizeRequests().antMatchers("*").permitAll(). -////// "/api/users/login", "/api/users/signup", "/api/users/find/password", -////// "/api/users/find/userName/**", "/api/forums/count/**", "/api/forums/**", -////// "/routine/**", "/rooms", "/routines", "/routine", "/myRoutines/**", -////// "/room/**", "/start", "/ws-stomp/**", "/api/challenges/**", "/api/trials/**", "/demo/similarity", -//// // all other requests need to be authenticated -//// anyRequest().authenticated().and(). -//// sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); -// // Add our custom JWT security filter -//// http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); -// } -// -// @Bean -// public CorsConfigurationSource corsConfigurationSource() { -// CorsConfiguration configuration = new CorsConfiguration(); -// // - (3) -// configuration.addAllowedOrigin("*"); -// configuration.addAllowedMethod("*"); -// configuration.addAllowedHeader("*"); -// configuration.setAllowCredentials(true); -// configuration.setMaxAge(3600L); -// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); -// source.registerCorsConfiguration("/**", configuration); -// return source; -// } - -} diff --git a/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationEntryPoint.java b/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationEntryPoint.java deleted file mode 100644 index 7c5d0fd..0000000 --- a/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationEntryPoint.java +++ /dev/null @@ -1,23 +0,0 @@ -package hometoogether.hometoogether.config.jwt; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.AuthenticationEntryPoint; -import org.springframework.stereotype.Component; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -@Component -public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { - private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationEntryPoint.class); - - @Override - public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { - logger.error("Responding with unauthorized error. Message - {}", authException.getMessage()); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); - } -} diff --git a/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationFilter.java b/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationFilter.java deleted file mode 100644 index dd83054..0000000 --- a/src/main/java/hometoogether/hometoogether/config/jwt/JwtAuthenticationFilter.java +++ /dev/null @@ -1,54 +0,0 @@ -package hometoogether.hometoogether.config.jwt; - -import hometoogether.hometoogether.domain.user.service.CustomUserDetailsService; -import lombok.RequiredArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; -import org.springframework.web.filter.OncePerRequestFilter; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -@RequiredArgsConstructor -@Component -public class JwtAuthenticationFilter extends OncePerRequestFilter { - - private final JwtTokenProvider tokenProvider; - private final CustomUserDetailsService customUserDetailsService; - private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationFilter.class); - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - try { - String jwt = getJwtFromRequest(request); - if (StringUtils.hasText(jwt) && tokenProvider.validateToken(jwt)) { - Long userId = tokenProvider.getUserIdFromJWT(jwt); - UserDetails userDetails = customUserDetailsService.loadUserById(userId); - UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( userDetails, null, userDetails.getAuthorities()); - authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); - SecurityContextHolder.getContext().setAuthentication(authentication); - } - } catch (Exception ex) { - logger.error("Could not set user authentication in security context", ex); - } - filterChain.doFilter(request, response); - } - - private String getJwtFromRequest(HttpServletRequest request) { - String bearerToken = request.getHeader("Authorization"); - if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { - return bearerToken.substring(7, bearerToken.length()); - } - return null; - } - -} diff --git a/src/main/java/hometoogether/hometoogether/config/jwt/JwtTokenProvider.java b/src/main/java/hometoogether/hometoogether/config/jwt/JwtTokenProvider.java index 2243c41..ff51c5c 100644 --- a/src/main/java/hometoogether/hometoogether/config/jwt/JwtTokenProvider.java +++ b/src/main/java/hometoogether/hometoogether/config/jwt/JwtTokenProvider.java @@ -1,61 +1,84 @@ package hometoogether.hometoogether.config.jwt; -import hometoogether.hometoogether.domain.user.domain.UserPrincipal; +import hometoogether.hometoogether.domain.user.domain.User; +import hometoogether.hometoogether.domain.user.repository.UserRepository; import io.jsonwebtoken.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; -import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import javax.servlet.http.HttpServletRequest; import java.util.Date; @Component +@RequiredArgsConstructor public class JwtTokenProvider { - private static final Logger logger = LoggerFactory.getLogger(JwtTokenProvider.class); - @Value("${app.jwtSecret}") - private String jwtSecret; - - @Value("${app.jwtExpirationInMs}") - private int jwtExpirationInMs; + private String secretKey; + private long accessTokenValidTime = 1000L * 60 * 60; + private long refreshTokenValidTime = 1000L * 60 * 60 * 24; - public String generateToken(Authentication authentication) { - - UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); + private final UserRepository userRepository; + public String generateToken(Long userId) { Date now = new Date(); - Date expiryDate = new Date(now.getTime() + jwtExpirationInMs); - - logger.debug("user : " + userPrincipal.getUsername(), userPrincipal.getId()); - String jwt = Jwts.builder().setSubject(Long.toString(userPrincipal.getId())).setIssuedAt(new Date()) - .setExpiration(expiryDate).signWith(SignatureAlgorithm.HS512, jwtSecret).compact(); - logger.debug("jwt : " + jwt); - return jwt; + return Jwts.builder() + .setIssuedAt(now) // 토큰 발급시간 + .setExpiration(new Date(System.currentTimeMillis() + accessTokenValidTime)) // 토큰 유효시간 + .claim("userId", userId) // 토큰에 담을 데이터 + .signWith(SignatureAlgorithm.HS256, secretKey.getBytes()) // secretKey를 사용하여 해싱 암호화 알고리즘 처리 + .compact(); // 직렬화, 문자열로 변경 } - public Long getUserIdFromJWT(String token) { - Claims claims = Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(token).getBody(); + public String createRefreshToken() { + Date now = new Date(); + return Jwts.builder() + .setIssuedAt(now) // 토큰 발급시간 + .setExpiration(new Date(System.currentTimeMillis() + refreshTokenValidTime)) // 토큰 유효시간 + .signWith(SignatureAlgorithm.HS256, secretKey.getBytes()) // secretKey를 사용하여 해싱 암호화 알고리즘 처리 + .compact(); // 직렬화, 문자열로 변경 + } - return Long.parseLong(claims.getSubject()); + public boolean isValid(String token) { + try { + Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token); + return true; + } catch (Exception e) { + throw new RuntimeException("토큰이 유효하지 않습니다."); + } } - public boolean validateToken(String authToken) { + public boolean isValidExceptExp(String token) { try { - Jwts.parser().setSigningKey(jwtSecret).parseClaimsJws(authToken); + Jws claims = Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token); + return claims.getBody().getExpiration().before(new Date()); + } catch (ExpiredJwtException e) { return true; - } catch (SignatureException ex) { - logger.error("Invalid JWT signature"); - } catch (MalformedJwtException ex) { - logger.error("Invalid JWT token"); - } catch (ExpiredJwtException ex) { - logger.error("Expired JWT token"); - } catch (UnsupportedJwtException ex) { - logger.error("Unsupported JWT token"); - } catch (IllegalArgumentException ex) { - logger.error("JWT claims string is empty."); + } catch (Exception e) { + return false; } - return false; + } + + public Long getTokenInfo() { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); + String jwt = request.getHeader("Authorization"); + Jws claims = null; + try { + claims = Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(jwt); // secretKey를 사용하여 복호화 + } catch (Exception e) { + throw new RuntimeException("토큰 정보를 불러올 수 없습니다."); + } + Object userId = claims.getBody().get("userId"); + return Long.valueOf(userId.toString()); + } + + public User getUserFromJwt() { + Long userId = this.getTokenInfo(); + User user = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("존재하지 않는 유저입니다.")); + return user; } } From 93d290735db0ea0d71858000c40947f4b32c7917 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:44:50 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[refactor]=20:=20WebClient=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=9C=20Kakao=20API=20=ED=98=B8=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../pose/algorithm/SimilarityAlgorithm.java | 2 + .../domain/pose/domain/ChallengePose.java | 59 --- .../domain/pose/domain/Keypoints.java | 37 -- .../domain/pose/{ => domain}/Pose.java | 0 .../domain/pose/{ => domain}/PoseType.java | 0 .../domain/pose/domain/TrialPose.java | 55 --- .../pose/dto/ChallengePoseResponseDto.java | 4 - .../domain/pose/dto/KakaoPosePhotoReq.java | 8 + .../domain/pose/dto/KakaoPosePhotoRes.java | 12 + .../domain/pose/dto/KakaoPoseVideoReq.java | 8 + .../domain/pose/dto/KakaoPoseVideoRes.java | 8 + .../pose/dto/KakaoPoseVideoResultReq.java | 8 + .../pose/dto/KakaoPoseVideoResultRes.java | 20 + .../domain/pose/dto/TrialPoseResponseDto.java | 4 - .../pose/dto/kakaoApiType/Annotation.java | 4 + .../pose/dto/kakaoApiType/Category.java | 4 + .../domain/pose/dto/kakaoApiType/Info.java | 13 + .../domain/pose/dto/kakaoApiType/Video.java | 11 + .../repository/ChallengePoseRepository.java | 7 - .../pose/repository/KeypointsRepository.java | 7 - .../pose/repository/TrialPoseRepository.java | 7 - .../domain/pose/service/KakaoService.java | 2 + .../domain/pose/service/PoseService.java | 440 ++---------------- 24 files changed, 146 insertions(+), 576 deletions(-) create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/domain/ChallengePose.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/domain/Keypoints.java rename src/main/java/hometoogether/hometoogether/domain/pose/{ => domain}/Pose.java (100%) rename src/main/java/hometoogether/hometoogether/domain/pose/{ => domain}/PoseType.java (100%) delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/domain/TrialPose.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/ChallengePoseResponseDto.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoReq.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoRes.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoReq.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoRes.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultReq.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultRes.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/TrialPoseResponseDto.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Annotation.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Category.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Info.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Video.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/repository/ChallengePoseRepository.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/repository/KeypointsRepository.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/repository/TrialPoseRepository.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java diff --git a/build.gradle b/build.gradle index 09f16a0..1d307f2 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-webflux' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' @@ -50,7 +51,6 @@ dependencies { implementation 'org.jcodec:jcodec-javase:0.2.3' implementation 'org.springframework.boot:spring-boot-starter-websocket' - implementation 'org.springframework.boot:spring-boot-starter-security' // providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' } diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java b/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java new file mode 100644 index 0000000..85c5966 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java @@ -0,0 +1,2 @@ +package hometoogether.hometoogether.domain.pose.algorithm;public class SimilarityAlgorithm { +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/domain/ChallengePose.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/ChallengePose.java deleted file mode 100644 index 26bb037..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/domain/ChallengePose.java +++ /dev/null @@ -1,59 +0,0 @@ -package hometoogether.hometoogether.domain.pose.domain; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.user.domain.User; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import javax.persistence.*; -import java.util.List; - -@Getter -@Setter -@NoArgsConstructor -@Entity(name = "challenge_pose") -public class ChallengePose { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String url; - - private String thumbnail_url; - - private String job_id; - - @OneToMany(fetch = FetchType.LAZY, mappedBy = "challenge_pose") - private List keypointsList; - - public void addKeypoints(Keypoints keypoints) { - keypoints.setChallenge_pose(this); - this.keypointsList.add(keypoints); - } - - @ManyToOne - private User user; - - @OneToOne - private Challenge challenge; - - @Builder - private ChallengePose(String url, String thumbnail_url, List keypointsList, User user, Challenge challenge) { - this.url = url; - this.thumbnail_url = thumbnail_url; - this.keypointsList = keypointsList; - this.user = user; - this.challenge = challenge; - } - - public void update_Job_id(String job_id){ - this.job_id = job_id; - } - - public void update_KeypointsList(List keypointsList){ - this.keypointsList = keypointsList; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/domain/Keypoints.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/Keypoints.java deleted file mode 100644 index 9e581ef..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/domain/Keypoints.java +++ /dev/null @@ -1,37 +0,0 @@ -package hometoogether.hometoogether.domain.pose.domain; - -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import javax.persistence.*; -import java.util.List; - -@Getter -@Setter -@NoArgsConstructor -@Entity -public class Keypoints { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @ElementCollection - private List keypoints; - - @ManyToOne - @JoinColumn(name = "challenge_pose_id") - private ChallengePose challenge_pose; - - @ManyToOne - @JoinColumn(name = "trial_pose_id") - private TrialPose trial_pose; - - @Builder - public Keypoints(List keypoints){ - this.keypoints = keypoints; - } - -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/Pose.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java similarity index 100% rename from src/main/java/hometoogether/hometoogether/domain/pose/Pose.java rename to src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/PoseType.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java similarity index 100% rename from src/main/java/hometoogether/hometoogether/domain/pose/PoseType.java rename to src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/domain/TrialPose.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/TrialPose.java deleted file mode 100644 index 3a7ad12..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/domain/TrialPose.java +++ /dev/null @@ -1,55 +0,0 @@ -package hometoogether.hometoogether.domain.pose.domain; - -import hometoogether.hometoogether.domain.trial.domain.Trial; -import hometoogether.hometoogether.domain.user.domain.User; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import javax.persistence.*; -import java.util.List; - -@Getter -@Setter -@NoArgsConstructor -@Entity(name = "trial_pose") -public class TrialPose { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String url; - - private String thumbnail_url; - - private String job_id; - - @OneToMany(fetch = FetchType.LAZY, mappedBy = "trial_pose") - private List keypointsList; - - public void addKeypoints(Keypoints keypoints) { - keypoints.setTrial_pose(this); - this.keypointsList.add(keypoints); - } - - @ManyToOne - private User user; - - @OneToOne - private Trial trial; - - @Builder - private TrialPose(String url, String thumbnail_url, List keypointsList, User user, Trial trial) { - this.url = url; - this.thumbnail_url = thumbnail_url; - this.keypointsList = keypointsList; - this.user = user; - this.trial = trial; - } - - public void update(String job_id){ - this.job_id = job_id; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/ChallengePoseResponseDto.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/ChallengePoseResponseDto.java deleted file mode 100644 index cda23ad..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/dto/ChallengePoseResponseDto.java +++ /dev/null @@ -1,4 +0,0 @@ -package hometoogether.hometoogether.domain.pose.dto; - -public class ChallengePoseResponseDto { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoReq.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoReq.java new file mode 100644 index 0000000..adecb17 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoReq.java @@ -0,0 +1,8 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import lombok.Data; + +@Data +public class KakaoPosePhotoReq { + private String image_url; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoRes.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoRes.java new file mode 100644 index 0000000..8186c19 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPosePhotoRes.java @@ -0,0 +1,12 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import lombok.Data; + +@Data +public class KakaoPosePhotoRes { + private float area; + private float[] bbox; + private int category_id; + private float[] keypoints; + private float score; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoReq.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoReq.java new file mode 100644 index 0000000..1f1c725 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoReq.java @@ -0,0 +1,8 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import lombok.Data; + +@Data +public class KakaoPoseVideoReq { + private String video_url; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoRes.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoRes.java new file mode 100644 index 0000000..51f3db0 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoRes.java @@ -0,0 +1,8 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import lombok.Data; + +@Data +public class KakaoPoseVideoRes { + private String job_id; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultReq.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultReq.java new file mode 100644 index 0000000..7dfc9c7 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultReq.java @@ -0,0 +1,8 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import lombok.Data; + +@Data +public class KakaoPoseVideoResultReq { + private String job_id; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultRes.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultRes.java new file mode 100644 index 0000000..046213b --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/KakaoPoseVideoResultRes.java @@ -0,0 +1,20 @@ +package hometoogether.hometoogether.domain.pose.dto; + +import hometoogether.hometoogether.domain.pose.dto.kakaoApiType.Annotation; +import hometoogether.hometoogether.domain.pose.dto.kakaoApiType.Category; +import hometoogether.hometoogether.domain.pose.dto.kakaoApiType.Info; +import hometoogether.hometoogether.domain.pose.dto.kakaoApiType.Video; +import lombok.Data; + +import java.util.List; + +@Data +public class KakaoPoseVideoResultRes { + private String job_id; + private String status; + private List annotations; + private List categories; + private Info info; + private Video video; + private String description; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/TrialPoseResponseDto.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/TrialPoseResponseDto.java deleted file mode 100644 index 41a9783..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/dto/TrialPoseResponseDto.java +++ /dev/null @@ -1,4 +0,0 @@ -package hometoogether.hometoogether.domain.pose.dto; - -public class TrialPoseResponseDto { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Annotation.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Annotation.java new file mode 100644 index 0000000..3c9b238 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Annotation.java @@ -0,0 +1,4 @@ +package hometoogether.hometoogether.domain.pose.dto.kakaoApiType; + +public class Annotation { +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Category.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Category.java new file mode 100644 index 0000000..caf353c --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Category.java @@ -0,0 +1,4 @@ +package hometoogether.hometoogether.domain.pose.dto.kakaoApiType; + +public class Category { +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Info.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Info.java new file mode 100644 index 0000000..df86a62 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Info.java @@ -0,0 +1,13 @@ +package hometoogether.hometoogether.domain.pose.dto.kakaoApiType; + +import lombok.Data; + +@Data +public class Info { + private String contributor; + private String date_created; + private String description; + private String url; + private String version; + private int year; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Video.java b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Video.java new file mode 100644 index 0000000..1df9758 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/dto/kakaoApiType/Video.java @@ -0,0 +1,11 @@ +package hometoogether.hometoogether.domain.pose.dto.kakaoApiType; + +import lombok.Data; + +@Data +public class Video { + private float fps; + private int frames; + private int height; + private int width; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/repository/ChallengePoseRepository.java b/src/main/java/hometoogether/hometoogether/domain/pose/repository/ChallengePoseRepository.java deleted file mode 100644 index d2774bb..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/repository/ChallengePoseRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hometoogether.hometoogether.domain.pose.repository; - -import hometoogether.hometoogether.domain.pose.domain.ChallengePose; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface ChallengePoseRepository extends JpaRepository { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/repository/KeypointsRepository.java b/src/main/java/hometoogether/hometoogether/domain/pose/repository/KeypointsRepository.java deleted file mode 100644 index 9ec8c4d..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/repository/KeypointsRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hometoogether.hometoogether.domain.pose.repository; - -import hometoogether.hometoogether.domain.pose.domain.Keypoints; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface KeypointsRepository extends JpaRepository { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/repository/TrialPoseRepository.java b/src/main/java/hometoogether/hometoogether/domain/pose/repository/TrialPoseRepository.java deleted file mode 100644 index 2d8e36d..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/repository/TrialPoseRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hometoogether.hometoogether.domain.pose.repository; - -import hometoogether.hometoogether.domain.pose.domain.TrialPose; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface TrialPoseRepository extends JpaRepository { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java b/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java new file mode 100644 index 0000000..fc0ab09 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java @@ -0,0 +1,2 @@ +package hometoogether.hometoogether.domain.pose.service;public class KakaoService { +} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/service/PoseService.java b/src/main/java/hometoogether/hometoogether/domain/pose/service/PoseService.java index 70b3fe9..33a017b 100644 --- a/src/main/java/hometoogether/hometoogether/domain/pose/service/PoseService.java +++ b/src/main/java/hometoogether/hometoogether/domain/pose/service/PoseService.java @@ -1,32 +1,62 @@ package hometoogether.hometoogether.domain.pose.service; -import hometoogether.hometoogether.domain.pose.domain.*; -import hometoogether.hometoogether.domain.pose.repository.*; +import hometoogether.hometoogether.domain.pose.domain.Pose; +import hometoogether.hometoogether.domain.pose.domain.PoseType; +import hometoogether.hometoogether.domain.pose.dto.KakaoPosePhotoRes; +import hometoogether.hometoogether.domain.pose.dto.KakaoPoseVideoRes; +import hometoogether.hometoogether.domain.pose.dto.KakaoPoseVideoResultReq; +import hometoogether.hometoogether.domain.pose.dto.KakaoPoseVideoResultRes; +import hometoogether.hometoogether.domain.post.repository.PostRepository; import lombok.RequiredArgsConstructor; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.springframework.http.*; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.List; -@RequiredArgsConstructor @Service +@RequiredArgsConstructor public class PoseService { - private final KeypointsRepository keypointsRepository; - private final ChallengePoseRepository challengePoseRepository; - private final TrialPoseRepository trialPoseRepository; + private final KakaoService kakaoService; + private final PostRepository postRepository; + + @Transactional + public void estimatePose(Pose pose) { + if (pose.getPoseType().equals(PoseType.PHOTO)) { + estimatePosePhoto(pose); + } + else { + estimatePoseVideo(pose); + } + } -// public String test(String url) throws IOException { + @Async + @Transactional + public void estimatePosePhoto(Pose pose) { + KakaoPosePhotoRes kakaoPosePhotoRes = kakaoService.kakaoPosePhoto(pose); + } + + @Async + @Transactional + public void estimatePoseVideo(Pose pose) { + KakaoPoseVideoRes kakaoPoseVideoRes = kakaoService.kakaoPoseVideo(pose); + +// try { +// Thread.sleep(60000); +// estimatePoseDetailVideo(job_id, pose_id, pose_type); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + } + + @Transactional + public void estimatePoseVideoResult(KakaoPoseVideoResultReq kakaoPoseVideoResultReq) { + KakaoPoseVideoResultRes kakaoPoseVideoResultRes = kakaoService.kakaoPoseVideoResult(kakaoPoseVideoResultReq); + } + + // public String test(String url) throws IOException { // // HttpHeaders headers = new HttpHeaders(); // headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); @@ -65,385 +95,5 @@ public class PoseService { // return jsonbody; // } - @Transactional - @Async - public void estimatePosePhoto(Long pose_id, String url, String pose_type) throws ParseException { - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.add("Authorization", "KakaoAK 19a4097fe8917a985bb1a7acc9ce2fb1"); - - MultiValueMap params = new LinkedMultiValueMap<>(); - params.add("image_url", "http://221.143.144.143:80/"+url); -// params.add("image_url", "http://58.122.7.167:9000/"+url); - - HttpEntity> entity = new HttpEntity<>(params, headers); - - RestTemplate rt = new RestTemplate(); - ResponseEntity response = rt.exchange( - "https://cv-api.kakaobrain.com/pose", - HttpMethod.POST, - entity, - String.class - ); - - JSONParser jsonParse = new JSONParser(); - JSONArray jsonArr = (JSONArray) jsonParse.parse(response.getBody()); - JSONObject jsonObj = (JSONObject) jsonArr.get(0); - JSONArray JSON_keypoints = (JSONArray) jsonObj.get("keypoints"); - List keypoints = (List) JSON_keypoints; - Keypoints kp = Keypoints.builder() - .keypoints(keypoints) - .build(); - keypointsRepository.save(kp); - - if (pose_type == "challenge") - { - ChallengePose challengePose = challengePoseRepository.getById(pose_id); - challengePose.addKeypoints(kp); - challengePoseRepository.save(challengePose); - } - else if (pose_type == "trial"){ - TrialPose trialPose = trialPoseRepository.getById(pose_id); - trialPose.addKeypoints(kp); - trialPoseRepository.save(trialPose); - } - return; - } - - @Transactional - @Async - public void estimatePoseVideo(Long pose_id, String url, String pose_type) throws ParseException { - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.add("Authorization", "KakaoAK e77f96acc7076a928b06d2fa9a480474"); - - MultiValueMap params = new LinkedMultiValueMap<>(); - params.add("video_url", "http://221.143.144.143:80/"+url); -// params.add("video_url", "http://58.122.7.167:9000/"+url); - - HttpEntity> entity = new HttpEntity<>(params, headers); - - RestTemplate rt = new RestTemplate(); - ResponseEntity response = rt.exchange( - "https://cv-api.kakaobrain.com/pose/job", - HttpMethod.POST, - entity, - String.class - ); - - JSONParser jsonParse = new JSONParser(); - JSONObject jsonObj = (JSONObject) jsonParse.parse(response.getBody()); - String job_id = (String) jsonObj.get("job_id"); - System.out.println("job_id = " + job_id); - - if (pose_type == "challenge") - { - System.out.println("I'm challenge\n"); - ChallengePose challengePose = challengePoseRepository.findById(pose_id) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + pose_id)); - challengePose.setJob_id(job_id); - challengePoseRepository.save(challengePose); - } - else if (pose_type == "trial"){ - System.out.println("I'm trial\n"); - TrialPose trialPose = trialPoseRepository.findById(pose_id) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + pose_id)); - trialPose.setJob_id(job_id); - trialPoseRepository.save(trialPose); - } - - try { - Thread.sleep(60000); - estimatePoseDetailVideo(job_id, pose_id, pose_type); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - return; - } - - @Transactional - public void estimatePoseDetailVideo(String job_id, Long pose_id, String pose_type) throws ParseException, InterruptedException { - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.add("Authorization", "KakaoAK e77f96acc7076a928b06d2fa9a480474"); - - MultiValueMap params = new LinkedMultiValueMap<>(); - params.add("job_id", job_id); - - HttpEntity> entity = new HttpEntity<>(params, headers); - - RestTemplate rt = new RestTemplate(); - ResponseEntity response = rt.exchange( - "https://cv-api.kakaobrain.com/pose/job/"+job_id, - HttpMethod.GET, - entity, - String.class - ); - - JSONParser jsonParse = new JSONParser(); - JSONObject jsonObj = (JSONObject) jsonParse.parse(response.getBody()); - - String status = (String) jsonObj.get("status"); - while ("success".equals(status) == false){ - Thread.sleep(60000); - response = rt.exchange( - "https://cv-api.kakaobrain.com/pose/job/"+job_id, - HttpMethod.GET, - entity, - String.class - ); - jsonObj = (JSONObject) jsonParse.parse(response.getBody()); - status = (String) jsonObj.get("status"); - } - - JSONArray jsonArr = (JSONArray) jsonObj.get("annotations"); - - JSONObject example = (JSONObject) jsonArr.get(5); - JSONArray example2 = (JSONArray) example.get("objects"); - JSONObject example3 = (JSONObject) example2.get(0); - JSONArray example4 = (JSONArray) example3.get("keypoints"); - List example5 = (List) example4; - System.out.println("example = " + example5); - - if ("challenge".equals(pose_type)) - { - ChallengePose challengePose = challengePoseRepository.getById(pose_id); - for (int i=0; i keypoints = (List) JSON_keypoints; - Keypoints kp = Keypoints.builder() - .keypoints(keypoints) - .build(); - challengePose.addKeypoints(kp); - keypointsRepository.save(kp); - } - } - challengePoseRepository.save(challengePose); - } - else if ("trial".equals(pose_type)){ - TrialPose trialPose = trialPoseRepository.getById(pose_id); - for (int i=0; i keypoints = (List) JSON_keypoints; - Keypoints kp = Keypoints.builder() - .keypoints(keypoints) - .build(); - trialPose.addKeypoints(kp); - keypointsRepository.save(kp); - } - } - trialPoseRepository.save(trialPose); - } - -// List keypointsList = new ArrayList(); -// for (int i=0; i keypoints = (List) JSON_keypoints; -// Keypoints kp = Keypoints.builder() -// .keypoints(keypoints) -// .build(); -// keypointsRepository.save(kp); -// keypointsList.add(kp); -// } -// } - - return; - } - - public double estimateSimilarity(List pose1, List pose2){ - -// List pose1 = poseInfoRepository.getById(6L).getKeypoints(); -// List pose2 = poseInfoRepository.getById(7L).getKeypoints(); - - ArrayList> vectorInfo1 = vectorize(pose1); - ArrayList> vectorInfo2 = vectorize(pose2); - - ArrayList vectorPose1XY = vectorInfo1.get(0); - ArrayList vectorMinMax1 = vectorInfo1.get(1); - ArrayList vectorPose1Scores = vectorInfo1.get(2); - - ArrayList vectorPose2XY = vectorInfo2.get(0); - ArrayList vectorMinMax2 = vectorInfo2.get(1); - ArrayList vectorPose2Scores = vectorInfo2.get(2); - - vectorPose1XY = sclaeAndtranslate(vectorPose1XY, vectorMinMax1); - vectorPose2XY = sclaeAndtranslate(vectorPose2XY, vectorMinMax2); - - vectorPose1XY = L2Normalize(vectorPose1XY); - vectorPose2XY = L2Normalize(vectorPose2XY); - - double cosineSimilarity = cosineSimilarity(vectorPose1XY, vectorPose2XY); - -// double cosineDistance = cosineDistanceMatching(vectorPose1XY, vectorPose2XY); -// double weightedDistance = weightedDistanceMatching(vectorPose1XY, vectorPose2XY, vectorPose2Scores); - - return cosineSimilarity; - } - - public ArrayList> vectorize(List pose) { - - ArrayList> vectorInfo = new ArrayList>(); - ArrayList vectorposeXY = new ArrayList<>(); - ArrayList vectorMinMax = new ArrayList<>(); - ArrayList vectorposeScores = new ArrayList<>(); - - double x_min = 10000; - double y_min = 10000; - double xy_max = -1; - double score_sum = 0; - - for(int i = 0; i< pose.size(); i++){ - double item = pose.get(i); - if (i%3 != 2){ - if (i%3 == 0) { - x_min = Math.min(item, x_min); - } - else { - y_min = Math.min(item, y_min); - } - xy_max = Math.max(item, xy_max); - vectorposeXY.add(item); - } - else{ - score_sum += item; - vectorposeScores.add(item); - } - } - vectorposeScores.add(score_sum); - - vectorMinMax.add(x_min); - vectorMinMax.add(y_min); - vectorMinMax.add(xy_max); - - vectorInfo.add(vectorposeXY); - vectorInfo.add(vectorMinMax); - vectorInfo.add(vectorposeScores); - - return vectorInfo; - } - - public ArrayList sclaeAndtranslate(ArrayList vectorPoseXY, ArrayList vectorMinMax) { - - for(int i = 0; i< vectorPoseXY.size(); i++){ - double item = vectorPoseXY.get(i); - if (i%2 == 0){ - item -= vectorMinMax.get(0); - } - else { - item -= vectorMinMax.get(1); - } - item /= vectorMinMax.get(2); - vectorPoseXY.set(i, item); - } - - return vectorPoseXY; - } - - public ArrayList L2Normalize(ArrayList vectorPoseXY) { - double absVectorPoseXY = 0; - for (Double pos: vectorPoseXY) { - absVectorPoseXY += Math.pow(pos, 2); - } - absVectorPoseXY = Math.sqrt(absVectorPoseXY); - for (Double pos: vectorPoseXY) { - pos /= absVectorPoseXY; - } - return vectorPoseXY; - } - - public double cosineSimilarity(ArrayList vectorPose1XY, ArrayList vectorPose2XY) { - double v1DotV2 = 0; - double absV1 = 0; - double absV2 = 0; - double v1 = 0; - double v2 = 0; - - for(int i = 0; i< vectorPose1XY.size(); i++){ - v1 = vectorPose1XY.get(i); - v2 = vectorPose2XY.get(i); - v1DotV2 += v1*v2; - absV1 += v1*v1; - absV2 += v2*v2; - } - - absV1 = Math.sqrt(absV1); - absV2 = Math.sqrt(absV2); - - return v1DotV2 / (absV1 * absV2); - } - - public double cosineDistanceMatching(ArrayList vectorPose1XY, ArrayList vectorPose2XY) { - double cosineSimilarity = cosineSimilarity(vectorPose1XY, vectorPose2XY); - System.out.println("cosineSimilarity = " + cosineSimilarity); - return Math.sqrt(2*(1-cosineSimilarity)); - } - - public double weightedDistanceMatching(ArrayList vectorPose1XY, ArrayList vectorPose2XY, ArrayList vectorPose2Scores) { - - double summation1 = 1 / vectorPose2Scores.get(vectorPose2Scores.size()-1); - double summation2 = 0; - - for (int i = 0; i< vectorPose1XY.size(); i++){ - int confIndex = (int) Math.floor(i/2); - summation2 = vectorPose2Scores.get(confIndex) * Math.abs(vectorPose1XY.get(i) - vectorPose2XY.get(i)); - } - - return summation1 * summation2; - } - - public double DTWDistance(List keypointsListA, List keypointsListB){ - - int lengthA = keypointsListA.size(); - int lengthB = keypointsListB.size(); - double[][] DTW = new double[lengthA+1][lengthB+1]; - - for (int i = 0; i < lengthA+1; i++){ - for (int j = 0; j < lengthB+1; j++) { - DTW[i][j] = 10000; - } - } - DTW[0][0] = 0; - - double cost = 0; - for (int i = 1; i < lengthA+1; i++){ - for (int j = 1; j < lengthB+1; j++) { - ArrayList kpListA = new ArrayList(); - kpListA.addAll(keypointsListA.get(i-1).getKeypoints()); - ArrayList kpListB = new ArrayList(); - kpListB.addAll(keypointsListB.get(j-1).getKeypoints()); - cost = estimateSimilarity(kpListA , kpListB); - if (DTW[i-1][j] == 10000 && DTW[i][j-1] == 10000){ - DTW[i][j] = (cost + DTW[i-1][j-1]) / 2; - } else if (DTW[i-1][j-1] == 10000 && DTW[i-1][j] == 10000){ - DTW[i][j] = (cost + DTW[i][j-1]) / 2; - } else if (DTW[i-1][j-1] == 10000 && DTW[i][j-1] == 10000){ - DTW[i][j] = (cost + DTW[i-1][j]) / 2; - } else { - DTW[i][j] = (cost + Math.max(Math.max(DTW[i-1][j], DTW[i][j-1]), DTW[i-1][j-1])) / 2; - } - } - } - - return DTW[lengthA][lengthB]; - } } \ No newline at end of file From 0612a9561bf1e4f42b4cff1a85d9329fa252de77 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:45:39 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[refactor]=20:=20WebClient=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=9C=20Kakao=20API=20=ED=98=B8=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/pose/service/KakaoService.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java b/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java index fc0ab09..7753efd 100644 --- a/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java +++ b/src/main/java/hometoogether/hometoogether/domain/pose/service/KakaoService.java @@ -1,2 +1,53 @@ -package hometoogether.hometoogether.domain.pose.service;public class KakaoService { +package hometoogether.hometoogether.domain.pose.service; + +import hometoogether.hometoogether.domain.pose.domain.Pose; +import hometoogether.hometoogether.domain.pose.dto.*; +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +@Service +public class KakaoService { + + private final String fileServerURL = ""; + private final String kakaoSecretKey = ""; + + private WebClient webClient = WebClient + .builder() + .baseUrl("https://cv-api.kakaobrain.com") + .defaultHeader("Authorization", kakaoSecretKey) + .defaultHeader(HttpHeaders.CONTENT_TYPE, String.valueOf(MediaType.APPLICATION_FORM_URLENCODED)) + .build(); + + public KakaoPosePhotoRes kakaoPosePhoto(Pose pose) { + KakaoPosePhotoReq kakaoPosePhotoReq = new KakaoPosePhotoReq(); + kakaoPosePhotoReq.setImage_url(fileServerURL + pose.getOriginalUrl()); + + return webClient.post() + .uri("/pose") + .bodyValue(kakaoPosePhotoReq) + .retrieve() + .bodyToMono(KakaoPosePhotoRes.class) + .block(); + } + + public KakaoPoseVideoRes kakaoPoseVideo(Pose pose) { + KakaoPoseVideoReq kakaoPoseVideoReq = new KakaoPoseVideoReq(); + kakaoPoseVideoReq.setVideo_url(fileServerURL + pose.getOriginalUrl()); + + return webClient.post() + .uri("/pose/job") + .bodyValue(kakaoPoseVideoReq) + .retrieve() + .bodyToMono(KakaoPoseVideoRes.class) + .block(); + } + + public KakaoPoseVideoResultRes kakaoPoseVideoResult(KakaoPoseVideoResultReq kakaoPoseVideoResultReq) { + return webClient.get() + .uri("/pose/job/" + kakaoPoseVideoResultReq.getJob_id()) + .retrieve() + .bodyToMono(KakaoPoseVideoResultRes.class) + .block(); + } } From 0153c080856ea234b5c3c8551c3b142c35ebf249 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:47:09 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[refactor]=20:=20pose=20=EB=94=94=EB=A0=89?= =?UTF-8?q?=ED=86=A0=EB=A6=AC=20=EA=B5=AC=EC=A1=B0=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pose/algorithm/SimilarityAlgorithm.java | 184 +++++++++++++++++- .../pose/controller/PoseController.java | 48 ----- .../domain/pose/domain/Pose.java | 2 +- .../domain/pose/domain/PoseType.java | 2 +- 4 files changed, 185 insertions(+), 51 deletions(-) delete mode 100644 src/main/java/hometoogether/hometoogether/domain/pose/controller/PoseController.java diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java b/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java index 85c5966..8ed9e0e 100644 --- a/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java +++ b/src/main/java/hometoogether/hometoogether/domain/pose/algorithm/SimilarityAlgorithm.java @@ -1,2 +1,184 @@ -package hometoogether.hometoogether.domain.pose.algorithm;public class SimilarityAlgorithm { +package hometoogether.hometoogether.domain.pose.algorithm; + +import java.util.ArrayList; +import java.util.List; + +public class SimilarityAlgorithm { +// public double estimateSimilarity(List pose1, List pose2){ +// +//// List pose1 = poseInfoRepository.getById(6L).getKeypoints(); +//// List pose2 = poseInfoRepository.getById(7L).getKeypoints(); +// +// ArrayList> vectorInfo1 = vectorize(pose1); +// ArrayList> vectorInfo2 = vectorize(pose2); +// +// ArrayList vectorPose1XY = vectorInfo1.get(0); +// ArrayList vectorMinMax1 = vectorInfo1.get(1); +// ArrayList vectorPose1Scores = vectorInfo1.get(2); +// +// ArrayList vectorPose2XY = vectorInfo2.get(0); +// ArrayList vectorMinMax2 = vectorInfo2.get(1); +// ArrayList vectorPose2Scores = vectorInfo2.get(2); +// +// vectorPose1XY = sclaeAndtranslate(vectorPose1XY, vectorMinMax1); +// vectorPose2XY = sclaeAndtranslate(vectorPose2XY, vectorMinMax2); +// +// vectorPose1XY = L2Normalize(vectorPose1XY); +// vectorPose2XY = L2Normalize(vectorPose2XY); +// +// double cosineSimilarity = cosineSimilarity(vectorPose1XY, vectorPose2XY); +// +//// double cosineDistance = cosineDistanceMatching(vectorPose1XY, vectorPose2XY); +// +//// double weightedDistance = weightedDistanceMatching(vectorPose1XY, vectorPose2XY, vectorPose2Scores); +// +// return cosineSimilarity; +// } +// +// public ArrayList> vectorize(List pose) { +// +// ArrayList> vectorInfo = new ArrayList>(); +// ArrayList vectorposeXY = new ArrayList<>(); +// ArrayList vectorMinMax = new ArrayList<>(); +// ArrayList vectorposeScores = new ArrayList<>(); +// +// double x_min = 10000; +// double y_min = 10000; +// double xy_max = -1; +// double score_sum = 0; +// +// for(int i = 0; i< pose.size(); i++){ +// double item = pose.get(i); +// if (i%3 != 2){ +// if (i%3 == 0) { +// x_min = Math.min(item, x_min); +// } +// else { +// y_min = Math.min(item, y_min); +// } +// xy_max = Math.max(item, xy_max); +// vectorposeXY.add(item); +// } +// else{ +// score_sum += item; +// vectorposeScores.add(item); +// } +// } +// vectorposeScores.add(score_sum); +// +// vectorMinMax.add(x_min); +// vectorMinMax.add(y_min); +// vectorMinMax.add(xy_max); +// +// vectorInfo.add(vectorposeXY); +// vectorInfo.add(vectorMinMax); +// vectorInfo.add(vectorposeScores); +// +// return vectorInfo; +// } +// +// public ArrayList sclaeAndtranslate(ArrayList vectorPoseXY, ArrayList vectorMinMax) { +// +// for(int i = 0; i< vectorPoseXY.size(); i++){ +// double item = vectorPoseXY.get(i); +// if (i%2 == 0){ +// item -= vectorMinMax.get(0); +// } +// else { +// item -= vectorMinMax.get(1); +// } +// item /= vectorMinMax.get(2); +// vectorPoseXY.set(i, item); +// } +// +// return vectorPoseXY; +// } +// +// public ArrayList L2Normalize(ArrayList vectorPoseXY) { +// double absVectorPoseXY = 0; +// for (Double pos: vectorPoseXY) { +// absVectorPoseXY += Math.pow(pos, 2); +// } +// absVectorPoseXY = Math.sqrt(absVectorPoseXY); +// for (Double pos: vectorPoseXY) { +// pos /= absVectorPoseXY; +// } +// return vectorPoseXY; +// } +// +// public double cosineSimilarity(ArrayList vectorPose1XY, ArrayList vectorPose2XY) { +// double v1DotV2 = 0; +// double absV1 = 0; +// double absV2 = 0; +// double v1 = 0; +// double v2 = 0; +// +// for(int i = 0; i< vectorPose1XY.size(); i++){ +// v1 = vectorPose1XY.get(i); +// v2 = vectorPose2XY.get(i); +// v1DotV2 += v1*v2; +// absV1 += v1*v1; +// absV2 += v2*v2; +// } +// +// absV1 = Math.sqrt(absV1); +// absV2 = Math.sqrt(absV2); +// +// return v1DotV2 / (absV1 * absV2); +// } +// +// public double cosineDistanceMatching(ArrayList vectorPose1XY, ArrayList vectorPose2XY) { +// double cosineSimilarity = cosineSimilarity(vectorPose1XY, vectorPose2XY); +// System.out.println("cosineSimilarity = " + cosineSimilarity); +// return Math.sqrt(2*(1-cosineSimilarity)); +// } +// +// public double weightedDistanceMatching(ArrayList vectorPose1XY, ArrayList vectorPose2XY, ArrayList vectorPose2Scores) { +// +// double summation1 = 1 / vectorPose2Scores.get(vectorPose2Scores.size()-1); +// double summation2 = 0; +// +// for (int i = 0; i< vectorPose1XY.size(); i++){ +// int confIndex = (int) Math.floor(i/2); +// summation2 = vectorPose2Scores.get(confIndex) * Math.abs(vectorPose1XY.get(i) - vectorPose2XY.get(i)); +// } +// +// return summation1 * summation2; +// } +// +// public double DTWDistance(List keypointsListA, List keypointsListB){ +// +// int lengthA = keypointsListA.size(); +// int lengthB = keypointsListB.size(); +// double[][] DTW = new double[lengthA+1][lengthB+1]; +// +// for (int i = 0; i < lengthA+1; i++){ +// for (int j = 0; j < lengthB+1; j++) { +// DTW[i][j] = 10000; +// } +// } +// DTW[0][0] = 0; +// +// double cost = 0; +// for (int i = 1; i < lengthA+1; i++){ +// for (int j = 1; j < lengthB+1; j++) { +// ArrayList kpListA = new ArrayList(); +// kpListA.addAll(keypointsListA.get(i-1).getKeypoints()); +// ArrayList kpListB = new ArrayList(); +// kpListB.addAll(keypointsListB.get(j-1).getKeypoints()); +// cost = estimateSimilarity(kpListA , kpListB); +// if (DTW[i-1][j] == 10000 && DTW[i][j-1] == 10000){ +// DTW[i][j] = (cost + DTW[i-1][j-1]) / 2; +// } else if (DTW[i-1][j-1] == 10000 && DTW[i-1][j] == 10000){ +// DTW[i][j] = (cost + DTW[i][j-1]) / 2; +// } else if (DTW[i-1][j-1] == 10000 && DTW[i][j-1] == 10000){ +// DTW[i][j] = (cost + DTW[i-1][j]) / 2; +// } else { +// DTW[i][j] = (cost + Math.max(Math.max(DTW[i-1][j], DTW[i][j-1]), DTW[i-1][j-1])) / 2; +// } +// } +// } +// +// return DTW[lengthA][lengthB]; +// } } diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/controller/PoseController.java b/src/main/java/hometoogether/hometoogether/domain/pose/controller/PoseController.java deleted file mode 100644 index 22c0974..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/pose/controller/PoseController.java +++ /dev/null @@ -1,48 +0,0 @@ -package hometoogether.hometoogether.domain.pose.controller; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.challenge.dto.ChallengeRequestDto; -import hometoogether.hometoogether.domain.challenge.repository.ChallengeRepository; -import hometoogether.hometoogether.domain.challenge.service.ChallengeService; -import hometoogether.hometoogether.domain.pose.domain.Keypoints; -import hometoogether.hometoogether.domain.pose.service.PoseService; -import hometoogether.hometoogether.domain.trial.domain.Trial; -import hometoogether.hometoogether.domain.trial.repository.TrialRepository; -import hometoogether.hometoogether.domain.trial.service.TrialService; -import lombok.RequiredArgsConstructor; -import org.json.simple.parser.ParseException; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; - -import javax.transaction.Transactional; -import java.io.IOException; -import java.util.List; - -@RequiredArgsConstructor -@CrossOrigin("*") -@RestController -public class PoseController { - - private final PoseService poseService; - private final ChallengeRepository challengeRepository; - private final TrialRepository trialRepository; - - @GetMapping("/demo/similarity") - @Transactional - public double similarity(){ - Challenge challenge = challengeRepository.findById(14L) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. challenge_id=" + 1)); - - Trial trial = trialRepository.findById(9L) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. trial_id=" + 1)); - - List keypointsList1 = challenge.getChallengePose().getKeypointsList(); - List keypointsList2 = trial.getTrialPose().getKeypointsList(); - - double similarity = poseService.DTWDistance(keypointsList1, keypointsList2); - - return similarity; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java index 801e591..5cb933d 100644 --- a/src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java +++ b/src/main/java/hometoogether/hometoogether/domain/pose/domain/Pose.java @@ -1,4 +1,4 @@ -package hometoogether.hometoogether.domain.pose; +package hometoogether.hometoogether.domain.pose.domain; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java b/src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java index 33e65fd..db38e62 100644 --- a/src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java +++ b/src/main/java/hometoogether/hometoogether/domain/pose/domain/PoseType.java @@ -1,4 +1,4 @@ -package hometoogether.hometoogether.domain.pose; +package hometoogether.hometoogether.domain.pose.domain; public enum PoseType { PHOTO, VIDEO From 51e2f0a08ad6e5857d52b0b329f4081a9c721350 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:48:50 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[feat]=20:=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=ED=9B=84=20=EC=9E=90=EC=84=B8=20=EB=B6=84?= =?UTF-8?q?=EC=84=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hometoogether/domain/post/domain/Post.java | 2 +- .../domain/post/service/ChallengeService.java | 13 ++----------- .../domain/post/service/TrialService.java | 4 ++-- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/hometoogether/hometoogether/domain/post/domain/Post.java b/src/main/java/hometoogether/hometoogether/domain/post/domain/Post.java index 36b21c3..c3085d3 100644 --- a/src/main/java/hometoogether/hometoogether/domain/post/domain/Post.java +++ b/src/main/java/hometoogether/hometoogether/domain/post/domain/Post.java @@ -1,6 +1,6 @@ package hometoogether.hometoogether.domain.post.domain; -import hometoogether.hometoogether.domain.pose.Pose; +import hometoogether.hometoogether.domain.pose.domain.Pose; import hometoogether.hometoogether.domain.user.domain.User; import hometoogether.hometoogether.entity.comment.CommentEntity; import hometoogether.hometoogether.entity.heart.HeartEntity; diff --git a/src/main/java/hometoogether/hometoogether/domain/post/service/ChallengeService.java b/src/main/java/hometoogether/hometoogether/domain/post/service/ChallengeService.java index 906a321..89c3d83 100644 --- a/src/main/java/hometoogether/hometoogether/domain/post/service/ChallengeService.java +++ b/src/main/java/hometoogether/hometoogether/domain/post/service/ChallengeService.java @@ -1,6 +1,6 @@ package hometoogether.hometoogether.domain.post.service; -import hometoogether.hometoogether.domain.pose.Pose; +import hometoogether.hometoogether.domain.pose.domain.Pose; import hometoogether.hometoogether.domain.post.domain.Post; import hometoogether.hometoogether.domain.post.dto.challenge.ReadChallengeRes; import hometoogether.hometoogether.domain.post.dto.challenge.CreateChallengeReq; @@ -11,22 +11,13 @@ import hometoogether.hometoogether.domain.user.domain.User; import hometoogether.hometoogether.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; -import org.jcodec.api.FrameGrab; -import org.jcodec.api.JCodecException; -import org.jcodec.common.model.Picture; -import org.jcodec.scale.AWTUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; import java.util.List; -import java.util.UUID; @RequiredArgsConstructor @Service @@ -63,7 +54,7 @@ public Long createChallenge(CreateChallengeReq createChallengeReq) throws IOExce user.addPost(post); // TODO: 비동기 처리로 자세 분석 완료되면 유저에게 알림(SSE) -// poseService.estimatePose(pose); + poseService.estimatePose(pose); return postRepository.save(post).getId(); } diff --git a/src/main/java/hometoogether/hometoogether/domain/post/service/TrialService.java b/src/main/java/hometoogether/hometoogether/domain/post/service/TrialService.java index ed68057..15cb0dd 100644 --- a/src/main/java/hometoogether/hometoogether/domain/post/service/TrialService.java +++ b/src/main/java/hometoogether/hometoogether/domain/post/service/TrialService.java @@ -1,6 +1,6 @@ package hometoogether.hometoogether.domain.post.service; -import hometoogether.hometoogether.domain.pose.Pose; +import hometoogether.hometoogether.domain.pose.domain.Pose; import hometoogether.hometoogether.domain.post.domain.Post; import hometoogether.hometoogether.domain.post.dto.trial.ReadTrialRes; import hometoogether.hometoogether.domain.post.dto.trial.UpdateTrialReq; @@ -51,7 +51,7 @@ public Long createTrial(CreateTrialReq createTrialReq) throws IOException { user.addPost(post); // TODO: 비동기 처리로 자세 분석 완료되면 유저에게 알림(SSE) -// poseService.estimatePose(pose); + poseService.estimatePose(pose); return postRepository.save(post).getId(); } From adbf482e2ba66d8a86080148243ea26618349b1e Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:50:08 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[fix]=20:=20Mysql=20DB=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- src/main/resources/application.properties | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 1d307f2..d91dfe2 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' runtimeOnly 'com.h2database:h2' -// implementation 'mysql:mysql-connector-java' + implementation 'mysql:mysql-connector-java' implementation 'io.jsonwebtoken:jjwt:0.9.0' implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16' diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index de980c2..b44e3cc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,17 @@ -spring.datasource.url=jdbc:h2:mem:hometoogether -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.username=sa +#spring.datasource.url=jdbc:h2:mem:hometoogether +#spring.datasource.driver-class-name=org.h2.Driver +#spring.datasource.username=sa -spring.jpa.database=h2 -spring.jpa.hibernate.ddl-auto=create +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/hometoo_db?allowPublicKeyRetrieval=true&useSSL=false +spring.datasource.username=hometoogether +spring.datasource.password=hometoogether + +spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect +spring.jpa.generate-ddl=true + +#spring.jpa.database=h2 +spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true #spring.jpa.open-in-view=false @@ -18,5 +26,5 @@ spring.servlet.multipart.max-file-size=50MB spring.servlet.multipart.max-request-size=50MB ## App Properties -app.jwtSecret= JWTSuperSecretKey +app.jwtSecret= hometoogether app.jwtExpirationInMs = 604800000 \ No newline at end of file From dcbb7f4ff9339aacf4f0061f5c75e5b41e908085 Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:51:13 +0900 Subject: [PATCH 7/8] =?UTF-8?q?[fix]=20:=20Spring=20Security=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hometoogether/hometoogether/HometoogetherApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hometoogether/hometoogether/HometoogetherApplication.java b/src/main/java/hometoogether/hometoogether/HometoogetherApplication.java index 4db0a43..509613a 100644 --- a/src/main/java/hometoogether/hometoogether/HometoogetherApplication.java +++ b/src/main/java/hometoogether/hometoogether/HometoogetherApplication.java @@ -5,7 +5,7 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.web.servlet.ServletComponentScan; -@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@SpringBootApplication //@ServletComponentScan public class HometoogetherApplication { From 0144c44b01e185a2830e31bf9de3cd130e90af6e Mon Sep 17 00:00:00 2001 From: rlacksgus97 Date: Tue, 19 Jul 2022 13:52:19 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[refactor]=20:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChallengeController.java | 69 ----- .../domain/challenge/domain/Challenge.java | 56 ---- .../dto/ChallengeDetailResponseDto.java | 35 --- .../challenge/dto/ChallengeRequestDto.java | 19 -- .../challenge/dto/ChallengeResponseDto.java | 28 -- .../challenge/service/ChallengeService.java | 203 -------------- .../post/dto/Challenge_vs_TrialDto.java | 12 + .../dto/challenge/CreateChallengeReq.java | 2 +- .../{trial => post}/service/MyComparator.java | 4 +- .../domain/room/service/RoomService.java | 6 +- .../trial/controller/TrialController.java | 64 ----- .../domain/trial/domain/Trial.java | 38 --- .../trial/dto/Challenge_vs_TrialDto.java | 21 -- .../domain/trial/dto/TrialRequestDto.java | 14 - .../domain/trial/dto/TrialResponseDto.java | 21 -- .../trial/repository/TrialRepository.java | 7 - .../domain/trial/service/TrialService.java | 256 ------------------ 17 files changed, 18 insertions(+), 837 deletions(-) delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/controller/ChallengeController.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/domain/Challenge.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeDetailResponseDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeRequestDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeResponseDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/challenge/service/ChallengeService.java create mode 100644 src/main/java/hometoogether/hometoogether/domain/post/dto/Challenge_vs_TrialDto.java rename src/main/java/hometoogether/hometoogether/domain/{trial => post}/service/MyComparator.java (76%) delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/controller/TrialController.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/domain/Trial.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/dto/Challenge_vs_TrialDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialRequestDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialResponseDto.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/repository/TrialRepository.java delete mode 100644 src/main/java/hometoogether/hometoogether/domain/trial/service/TrialService.java diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/controller/ChallengeController.java b/src/main/java/hometoogether/hometoogether/domain/challenge/controller/ChallengeController.java deleted file mode 100644 index f57746c..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/controller/ChallengeController.java +++ /dev/null @@ -1,69 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.controller; - -import hometoogether.hometoogether.domain.challenge.dto.ChallengeDetailResponseDto; -import hometoogether.hometoogether.domain.challenge.dto.ChallengeRequestDto; -import hometoogether.hometoogether.domain.challenge.dto.ChallengeResponseDto; -import hometoogether.hometoogether.domain.challenge.service.ChallengeService; -import lombok.RequiredArgsConstructor; -import org.jcodec.api.JCodecException; -import org.json.simple.parser.ParseException; -import org.springframework.data.domain.Pageable; -import org.springframework.web.bind.annotation.*; - -import java.io.IOException; -import java.util.List; - -@RequestMapping("/api") -@CrossOrigin("*") -@RequiredArgsConstructor -@RestController -public class ChallengeController { - - private final ChallengeService challengeService; - - @PostMapping("/challenges") - public Long save(ChallengeRequestDto param) throws IOException, ParseException, JCodecException, InterruptedException { - System.out.println("param.getType() = " + param.getType()); - if ("photo".equals(param.getType())){ - System.out.println("HI IM HERE"); - return challengeService.saveChallengePhoto(param); - } - System.out.println("NO IM THERE"); - return challengeService.saveChallengeVideo(param); - } - - @GetMapping("/challenges/{id}") - public ChallengeDetailResponseDto getDetail(@PathVariable("id") Long challengeId) { - return challengeService.getChallenge(challengeId); - } - - @GetMapping("/challenges/my/{username}") - public List getMyList(@PathVariable("username") String username){ - return challengeService.getMyList(username); - } - - @GetMapping("/challenges/my/count/{username}") - public int getMyCount(@PathVariable("username") String username){ - return challengeService.getMyList(username).size(); - } - - @GetMapping("/challenges") - public List getList(Pageable pageable){ - return challengeService.getChallengeList(pageable); - } - - @GetMapping("/challenges/trending") - public List getTrendingList(){ - return challengeService.getTrendingChallenges(); - } - - @PutMapping("/challenges/{id}") - public Long update(@PathVariable("id") Long challengeId, ChallengeRequestDto param){ - return challengeService.updateChallenge(challengeId, param); - } - - @DeleteMapping("/challenges/{id}") - public Long delete(@PathVariable("id") Long challengeId){ - return challengeService.deleteChallenge(challengeId); - } -} \ No newline at end of file diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/domain/Challenge.java b/src/main/java/hometoogether/hometoogether/domain/challenge/domain/Challenge.java deleted file mode 100644 index 1591ce2..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/domain/Challenge.java +++ /dev/null @@ -1,56 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.domain; - -import hometoogether.hometoogether.domain.pose.domain.ChallengePose; -import hometoogether.hometoogether.domain.trial.domain.Trial; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import javax.persistence.*; -import java.util.List; - -@Getter -@Setter -@NoArgsConstructor -@Entity -public class Challenge { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String type; - - @OneToOne - private ChallengePose challengePose; - - private String title; - - private String context; - - @OneToMany - private List trialList; - - private Integer trialCount = 0; - - public void addTrial(Trial trial) { - trial.setChallenge(this); - this.trialList.add(trial); - this.trialCount += 1; - } - - @Builder - public Challenge(ChallengePose challengePose, String title, String context, String type) { - this.type = type; - this.challengePose = challengePose; - this.title = title; - this.context = context; - } - - public void update(ChallengePose challengePose, String title, String context) { - this.challengePose = challengePose; - this.title = title; - this.context = context; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeDetailResponseDto.java b/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeDetailResponseDto.java deleted file mode 100644 index d73fdbd..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeDetailResponseDto.java +++ /dev/null @@ -1,35 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.dto; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.trial.domain.Trial; -import lombok.Getter; - -import java.util.ArrayList; -import java.util.List; - -@Getter -public class ChallengeDetailResponseDto { - private Long id; - private String type; - private String url; - private String username; - private String title; - private String context; - private List trial_user_List; - - public ChallengeDetailResponseDto(Challenge entity) { - this.id = entity.getId(); - this.type = entity.getType(); - this.url = entity.getChallengePose().getUrl(); - this.username = entity.getChallengePose().getUser().getUserName(); - this.title = entity.getTitle(); - this.context = entity.getContext(); - if(!entity.getTrialList().isEmpty()){ - List trial_user_List = new ArrayList<>(); - for (Trial t : entity.getTrialList()){ - trial_user_List.add(t.getTrialPose().getUser().getUserName()); - } - this.trial_user_List = trial_user_List; - } - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeRequestDto.java b/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeRequestDto.java deleted file mode 100644 index 5efe2e1..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeRequestDto.java +++ /dev/null @@ -1,19 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.dto; - -import lombok.Data; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import org.springframework.web.multipart.MultipartFile; - -@Getter -@Setter -@NoArgsConstructor -@Data -public class ChallengeRequestDto { - private String type; - private String username; - private String title; - private String context; - private MultipartFile file; -} diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeResponseDto.java b/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeResponseDto.java deleted file mode 100644 index 8250a4c..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/dto/ChallengeResponseDto.java +++ /dev/null @@ -1,28 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.dto; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import lombok.Getter; - -@Getter -public class ChallengeResponseDto { - private Long id; - private String type; - private String url; - private String username; - private String title; - - public ChallengeResponseDto(Challenge entity) { - - this.id = entity.getId(); - this.type = entity.getType(); - if (entity.getType() == "video"){ - this.url = entity.getChallengePose().getThumbnail_url(); - } - else{ - this.url = entity.getChallengePose().getUrl(); - } - - this.username = entity.getChallengePose().getUser().getUserName(); - this.title = entity.getTitle(); - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/challenge/service/ChallengeService.java b/src/main/java/hometoogether/hometoogether/domain/challenge/service/ChallengeService.java deleted file mode 100644 index e736b21..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/challenge/service/ChallengeService.java +++ /dev/null @@ -1,203 +0,0 @@ -package hometoogether.hometoogether.domain.challenge.service; - -import hometoogether.hometoogether.domain.challenge.dto.ChallengeDetailResponseDto; -import hometoogether.hometoogether.domain.challenge.dto.ChallengeRequestDto; -import hometoogether.hometoogether.domain.challenge.dto.ChallengeResponseDto; -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.challenge.repository.ChallengeRepository; -import hometoogether.hometoogether.domain.pose.domain.ChallengePose; -import hometoogether.hometoogether.domain.pose.repository.ChallengePoseRepository; -import hometoogether.hometoogether.domain.pose.service.PoseService; -import hometoogether.hometoogether.domain.user.domain.User; -import hometoogether.hometoogether.domain.user.repository.UserRepository; -import lombok.RequiredArgsConstructor; -import org.jcodec.api.FrameGrab; -import org.jcodec.api.JCodecException; -import org.jcodec.common.model.Picture; -import org.jcodec.scale.AWTUtil; -import org.json.simple.parser.ParseException; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import javax.imageio.ImageIO; -import javax.transaction.Transactional; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@Service -public class ChallengeService { - - private final UserRepository userRepository; - private final ChallengeRepository challengeRepository; - private final ChallengePoseRepository challengePoseRepository; - private final PoseService poseService; - - @Value("${spring.servlet.multipart.location}") - String videoPath; - - @Transactional - public Long saveChallengePhoto(ChallengeRequestDto challengeRequestDto) throws IOException, ParseException { - // parameter로 SessionUser 받아오게 구현 예정 - - //ChallengePose 생성 - //url, poseInfoList, user - MultipartFile multipartFile = challengeRequestDto.getFile(); - String url = UUID.randomUUID().toString() + "_" + multipartFile.getOriginalFilename(); - File file = new File(url); - multipartFile.transferTo(file); - - User user = userRepository.findUserByUserName(challengeRequestDto.getUsername()) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + challengeRequestDto.getUsername())); - - ChallengePose challengePose = ChallengePose.builder() - .url(url) - .user(user) - .build(); - - challengePoseRepository.save(challengePose); - - poseService.estimatePosePhoto(challengePose.getId(), url, "challenge"); -// List poseDetailList = poseService.estimatePosePhoto(url); -// List poseInfoList = new ArrayList<>(); -// for (PoseDetail pd : poseDetailList){ -// PoseInfo poseInfo = PoseInfo.builder() -// .poseDetail(pd) -// .build(); -// poseInfoList.add(poseInfo); -// } - - //User <-> ChallengePose 매핑 - user.addChallengePose(challengePose); - - //Challenge 생성 - Challenge challenge = Challenge.builder() - .type("photo") - .challengePose(challengePose) - .title(challengeRequestDto.getTitle()) - .context(challengeRequestDto.getContext()) - .build(); - - //challengePose <-> challenge 매핑 - challengePose.setChallenge(challenge); - - return challengeRepository.save(challenge).getId(); - } - - @Transactional - public Long saveChallengeVideo(ChallengeRequestDto challengeRequestDto) throws IOException, ParseException, JCodecException, InterruptedException { - // parameter로 SessionUser 받아오게 구현 예정 - - //ChallengePose 생성 - //url, poseInfoList, user - MultipartFile multipartFile = challengeRequestDto.getFile(); - String url = UUID.randomUUID().toString() + "_" + multipartFile.getOriginalFilename(); - File file = new File(url); - multipartFile.transferTo(file); - - -// Thread.sleep(5000); -// String thumbnail_url = UUID.randomUUID().toString(); -// Picture picture = FrameGrab.getFrameFromFile(file, 15); -// BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture); -// ImageIO.write(bufferedImage, "jpg", new File(url)); - - User user = userRepository.findUserByUserName(challengeRequestDto.getUsername()) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + challengeRequestDto.getUsername())); - - ChallengePose challengePose = ChallengePose.builder() - .url(url) -// .thumbnail_url(thumbnail_url) - .user(user) - .build(); - challengePoseRepository.save(challengePose); - - poseService.estimatePoseVideo(challengePose.getId(), url, "challenge"); - - //User <-> ChallengePose 매핑 - user.addChallengePose(challengePose); - - //Challenge 생성 - Challenge challenge = Challenge.builder() - .type("video") - .challengePose(challengePose) - .title(challengeRequestDto.getTitle()) - .context(challengeRequestDto.getContext()) - .build(); - - //challengePose <-> challenge 매핑 - challengePose.setChallenge(challenge); - - return challengeRepository.save(challenge).getId(); - } - - @Transactional - public String createThumbnail(File source) throws IOException, JCodecException { - String url = UUID.randomUUID().toString(); - Picture picture = FrameGrab.getFrameFromFile(source, 0); - BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture); - ImageIO.write(bufferedImage, "jpg", new File(url)); - return url; - } - - @Transactional - public ChallengeDetailResponseDto getChallenge(Long challengeId) { - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + challengeId)); - return new ChallengeDetailResponseDto(challenge); - } - - @Transactional - public List getMyList(String username) { - User user = userRepository.findUserByUserName(username) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + username)); - - List challenges = new ArrayList<>(); - - for (ChallengePose challengePose : user.getChallengePoseList()) { - challenges.add(challengePose.getChallenge()); - } - - return challenges.stream().map(ChallengeResponseDto::new).collect(Collectors.toList()); - } - - @Transactional - public List getTrendingChallenges() { - List challenges = challengeRepository.findTop5ByOrderByTrialCountDesc(); - return challenges.stream().map(ChallengeResponseDto::new).collect(Collectors.toList()); - } - - @Transactional - public List getChallengeList(Pageable pageable) { -// Sort sort = Sort.by(Sort.Direction.DESC, "create_date"); -// List challenges = challengeRepository.findAll(); -// return challenges.stream().map(ChallengeResponseDto::new).collect(Collectors.toList()); - return challengeRepository.findAll(pageable).map(ChallengeResponseDto::new).getContent(); - } - - @Transactional - public Long updateChallenge(Long challengeId, ChallengeRequestDto param) { - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + challengeId)); - ChallengePose challengePose = challenge.getChallengePose(); -// challengePose.setUrl(param.getUrl()); - challenge.update(challengePose, param.getTitle(), param.getContext()); - return challengeId; - } - - @Transactional - public Long deleteChallenge(Long challengeId) { - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + challengeId)); - challengeRepository.delete(challenge); - return challengeId; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/post/dto/Challenge_vs_TrialDto.java b/src/main/java/hometoogether/hometoogether/domain/post/dto/Challenge_vs_TrialDto.java new file mode 100644 index 0000000..9410953 --- /dev/null +++ b/src/main/java/hometoogether/hometoogether/domain/post/dto/Challenge_vs_TrialDto.java @@ -0,0 +1,12 @@ +package hometoogether.hometoogether.domain.post.dto; + +import lombok.Getter; + +@Getter +public class Challenge_vs_TrialDto { + private Long id; + private String type; + private String challenge_url; + private String trial_url; + private Double score; +} diff --git a/src/main/java/hometoogether/hometoogether/domain/post/dto/challenge/CreateChallengeReq.java b/src/main/java/hometoogether/hometoogether/domain/post/dto/challenge/CreateChallengeReq.java index f21e3ab..e7b5411 100644 --- a/src/main/java/hometoogether/hometoogether/domain/post/dto/challenge/CreateChallengeReq.java +++ b/src/main/java/hometoogether/hometoogether/domain/post/dto/challenge/CreateChallengeReq.java @@ -1,6 +1,6 @@ package hometoogether.hometoogether.domain.post.dto.challenge; -import hometoogether.hometoogether.domain.pose.PoseType; +import hometoogether.hometoogether.domain.pose.domain.PoseType; import lombok.Data; import org.springframework.web.multipart.MultipartFile; diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/service/MyComparator.java b/src/main/java/hometoogether/hometoogether/domain/post/service/MyComparator.java similarity index 76% rename from src/main/java/hometoogether/hometoogether/domain/trial/service/MyComparator.java rename to src/main/java/hometoogether/hometoogether/domain/post/service/MyComparator.java index 561f7bb..4057bbf 100644 --- a/src/main/java/hometoogether/hometoogether/domain/trial/service/MyComparator.java +++ b/src/main/java/hometoogether/hometoogether/domain/post/service/MyComparator.java @@ -1,6 +1,6 @@ -package hometoogether.hometoogether.domain.trial.service; +package hometoogether.hometoogether.domain.post.service; -import hometoogether.hometoogether.domain.trial.domain.Trial; +import hometoogether.hometoogether.domain.post.domain.Trial; import java.util.Comparator; diff --git a/src/main/java/hometoogether/hometoogether/domain/room/service/RoomService.java b/src/main/java/hometoogether/hometoogether/domain/room/service/RoomService.java index ff671ff..2b84a05 100644 --- a/src/main/java/hometoogether/hometoogether/domain/room/service/RoomService.java +++ b/src/main/java/hometoogether/hometoogether/domain/room/service/RoomService.java @@ -78,10 +78,10 @@ public CanEnterAndRoutineIdDto canEnterRoom(Long roomId){ } public void addClientToRoomMember(Long roomId, String email){ - String userName = userRepository.findByEmail(email).getUserName(); +// String userName = userRepository.findByEmail(email).getUserName(); Room room = roomRepository.findById(roomId).orElse(null); - room.setClientUserName(userName); +// room.setClientUserName(userName); roomRepository.save(room); } @@ -128,7 +128,7 @@ public Long createRoom(String routine, Long routineId, String email){ Room room = Room.builder() .cur_num(0L).max_num(2L) .routineId(routineId) - .hostUserName(userRepository.findByEmail(email).getUserName()) +// .hostUserName(userRepository.findByEmail(email).getUserName()) .trainings(trainings).build(); return roomRepository.save(room).getId(); diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/controller/TrialController.java b/src/main/java/hometoogether/hometoogether/domain/trial/controller/TrialController.java deleted file mode 100644 index 2f89cd7..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/controller/TrialController.java +++ /dev/null @@ -1,64 +0,0 @@ -package hometoogether.hometoogether.domain.trial.controller; - -import hometoogether.hometoogether.domain.trial.dto.Challenge_vs_TrialDto; -import hometoogether.hometoogether.domain.trial.dto.TrialRequestDto; -import hometoogether.hometoogether.domain.trial.dto.TrialResponseDto; -import hometoogether.hometoogether.domain.trial.service.TrialService; -import lombok.RequiredArgsConstructor; -import org.jcodec.api.JCodecException; -import org.json.simple.parser.ParseException; -import org.springframework.web.bind.annotation.*; - -import javax.transaction.Transactional; -import java.io.IOException; -import java.util.List; - -@RequestMapping("/api") -@CrossOrigin("*") -@RequiredArgsConstructor -@RestController -public class TrialController { - - private final TrialService trialService; - - @Transactional - @PostMapping("/challenges/{challengeId}/trials") - public Long save(@PathVariable("challengeId") Long challengeId, TrialRequestDto param) throws IOException, ParseException, JCodecException { - return trialService.save(challengeId, param); - } - - @GetMapping("/trials/{trialId}/estimate/detail") - public Challenge_vs_TrialDto compare(@PathVariable("trialId") Long trialId){ - return trialService.compareDetail(trialId); - } - - @GetMapping("/trials/{trialId}/estimate") - public double estimate(@PathVariable("trialId") Long trialId){ - return trialService.runSimilarity(trialId); - } - - @GetMapping("/challenges/{challengeId}/best_trials") - public List getBestTrialList(@PathVariable("challengeId") Long challengeId){ - return trialService.getBestTrials(challengeId); - } - - @GetMapping("/trials/{trialid}") - public TrialResponseDto getDetail(@PathVariable("trialid") Long trialId){ - return trialService.getTrial(trialId); - } - - @GetMapping("/trials/my/{username}") - public List getMyList(@PathVariable("username") String username){ - return trialService.getMyList(username); - } - - @GetMapping("/challenges/{challengeId}/trials") - public List getList(@PathVariable("challengeId") Long challengeId){ - return trialService.getTrialList(challengeId); - } - - @DeleteMapping("/challenges/{challengeId}/trials/{trialid}") - public Long delete(@PathVariable("trialid") Long trialId){ - return trialService.deleteTrial(trialId); - } -} \ No newline at end of file diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/domain/Trial.java b/src/main/java/hometoogether/hometoogether/domain/trial/domain/Trial.java deleted file mode 100644 index 95612b7..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/domain/Trial.java +++ /dev/null @@ -1,38 +0,0 @@ -package hometoogether.hometoogether.domain.trial.domain; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.pose.domain.TrialPose; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -import javax.persistence.*; - -@Getter -@Setter -@NoArgsConstructor -@Entity -public class Trial { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - private String type; - - private Double score = 0.0; - - @OneToOne - private TrialPose trialPose; - - @ManyToOne - private Challenge challenge; - - @Builder - public Trial(TrialPose trialPose, Challenge challenge) { -// this.type = challenge.getType(); - this.trialPose = trialPose; - this.challenge = challenge; - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/dto/Challenge_vs_TrialDto.java b/src/main/java/hometoogether/hometoogether/domain/trial/dto/Challenge_vs_TrialDto.java deleted file mode 100644 index 049e1ad..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/dto/Challenge_vs_TrialDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package hometoogether.hometoogether.domain.trial.dto; - -import hometoogether.hometoogether.domain.trial.domain.Trial; -import lombok.Getter; - -@Getter -public class Challenge_vs_TrialDto { - private Long id; - private String type; - private String challenge_url; - private String trial_url; - private Double score; - - public Challenge_vs_TrialDto(Trial entity) { - this.id = entity.getId(); - this.type = entity.getType(); - this.challenge_url = entity.getChallenge().getChallengePose().getUrl(); - this.trial_url = entity.getTrialPose().getUrl(); - this.score = entity.getScore(); - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialRequestDto.java b/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialRequestDto.java deleted file mode 100644 index b372c66..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialRequestDto.java +++ /dev/null @@ -1,14 +0,0 @@ -package hometoogether.hometoogether.domain.trial.dto; - -import lombok.Data; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.springframework.web.multipart.MultipartFile; - -@Getter -@NoArgsConstructor -@Data -public class TrialRequestDto { - private MultipartFile file; - private String userName; -} diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialResponseDto.java b/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialResponseDto.java deleted file mode 100644 index 458e967..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/dto/TrialResponseDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package hometoogether.hometoogether.domain.trial.dto; - -import hometoogether.hometoogether.domain.trial.domain.Trial; -import lombok.Getter; - -@Getter -public class TrialResponseDto { - private Long id; - private String type; - private String url; - private String username; - private Double score; - - public TrialResponseDto(Trial entity) { - this.id = entity.getId(); - this.type = entity.getType(); - this.url = entity.getTrialPose().getUrl(); - this.username = entity.getTrialPose().getUser().getUserName(); - this.score = entity.getScore(); - } -} diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/repository/TrialRepository.java b/src/main/java/hometoogether/hometoogether/domain/trial/repository/TrialRepository.java deleted file mode 100644 index 07a391f..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/repository/TrialRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hometoogether.hometoogether.domain.trial.repository; - -import hometoogether.hometoogether.domain.trial.domain.Trial; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface TrialRepository extends JpaRepository { -} diff --git a/src/main/java/hometoogether/hometoogether/domain/trial/service/TrialService.java b/src/main/java/hometoogether/hometoogether/domain/trial/service/TrialService.java deleted file mode 100644 index 305a3cd..0000000 --- a/src/main/java/hometoogether/hometoogether/domain/trial/service/TrialService.java +++ /dev/null @@ -1,256 +0,0 @@ -package hometoogether.hometoogether.domain.trial.service; - -import hometoogether.hometoogether.domain.challenge.domain.Challenge; -import hometoogether.hometoogether.domain.challenge.repository.ChallengeRepository; -import hometoogether.hometoogether.domain.pose.domain.Keypoints; -import hometoogether.hometoogether.domain.pose.domain.TrialPose; -import hometoogether.hometoogether.domain.pose.repository.TrialPoseRepository; -import hometoogether.hometoogether.domain.pose.service.PoseService; -import hometoogether.hometoogether.domain.trial.domain.Trial; -import hometoogether.hometoogether.domain.trial.dto.Challenge_vs_TrialDto; -import hometoogether.hometoogether.domain.trial.dto.TrialRequestDto; -import hometoogether.hometoogether.domain.trial.dto.TrialResponseDto; -import hometoogether.hometoogether.domain.trial.repository.TrialRepository; -import hometoogether.hometoogether.domain.user.domain.User; -import hometoogether.hometoogether.domain.user.repository.UserRepository; -import lombok.RequiredArgsConstructor; -import org.jcodec.api.FrameGrab; -import org.jcodec.api.JCodecException; -import org.jcodec.common.model.Picture; -import org.jcodec.scale.AWTUtil; -import org.json.simple.parser.ParseException; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import javax.imageio.ImageIO; -import javax.transaction.Transactional; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; -import java.util.stream.Collectors; - -@RequiredArgsConstructor -@Service -public class TrialService { - - private final UserRepository userRepository; - private final TrialRepository trialRepository; - private final ChallengeRepository challengeRepository; - private final TrialPoseRepository trialPoseRepository; - private final PoseService poseService; - - @Transactional - public Long save(Long challengeId, TrialRequestDto trialRequestDto) throws JCodecException, IOException, ParseException { - Challenge challenge = challengeRepository.getById(challengeId); - if ("photo".equals(challenge.getType())){ - return saveTrialPhoto(challengeId, trialRequestDto); - } - return saveTrialVideo(challengeId, trialRequestDto); - } - - @Transactional - public Long saveTrialPhoto(Long challengeId, TrialRequestDto trialRequestDto) throws IOException, ParseException, JCodecException { - // parameter로 SessionUser 받아오게 구현 예정 - - //TrialPose 생성 - //url, poseInfoList, user - MultipartFile multipartFile = trialRequestDto.getFile(); - String url = UUID.randomUUID().toString() + "_" + multipartFile.getOriginalFilename(); - File file = new File(url); - multipartFile.transferTo(file); - -// String thumbnail_url = createThumbnail(file); - - User user = userRepository.findUserByUserName(trialRequestDto.getUserName()) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + trialRequestDto.getUserName())); - - TrialPose trialPose = TrialPose.builder() - .url(url) -// .thumbnail_url(thumbnail_url) - .user(user) - .build(); - trialPoseRepository.save(trialPose); - - poseService.estimatePosePhoto(trialPose.getId(), url, "trial"); -// poseService.estimatePoseVideo(trialPose.getId(), url, "trial"); - - //User <-> ChallengePose 매핑 - user.addTrialPose(trialPose); - - //Trial 생성 - Trial trial = Trial.builder() - .trialPose(trialPose) - .build(); - - //trialPose <-> trial 상호 매핑 - trialPose.setTrial(trial); - - //Trial <-> Challenge 상호 매핑 - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + challengeId)); - challenge.addTrial(trial); - - trial.setType(challenge.getType()); - trial.setChallenge(challenge); - - return trialRepository.save(trial).getId(); - } - - @Transactional - public Long saveTrialVideo(Long challengeId, TrialRequestDto trialRequestDto) throws IOException, ParseException, JCodecException { - // parameter로 SessionUser 받아오게 구현 예정 - - //TrialPose 생성 - //url, poseInfoList, user - MultipartFile multipartFile = trialRequestDto.getFile(); - String url = UUID.randomUUID().toString() + "_" + multipartFile.getOriginalFilename(); - File file = new File(url); - multipartFile.transferTo(file); - -// String thumbnail_url = createThumbnail(file); - - User user = userRepository.findUserByUserName(trialRequestDto.getUserName()) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + trialRequestDto.getUserName())); - - TrialPose trialPose = TrialPose.builder() - .url(url) -// .thumbnail_url(thumbnail_url) - .user(user) - .build(); - trialPoseRepository.save(trialPose); - - poseService.estimatePoseVideo(trialPose.getId(), url, "trial"); - - //User <-> ChallengePose 매핑 - user.addTrialPose(trialPose); - - //Trial 생성 - Trial trial = Trial.builder() - .trialPose(trialPose) - .build(); - - //trialPose <-> trial 상호 매핑 - trialPose.setTrial(trial); - - //Trial <-> Challenge 상호 매핑 - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + challengeId)); - challenge.addTrial(trial); - - trial.setType(challenge.getType()); - trial.setChallenge(challenge); - - return trialRepository.save(trial).getId(); - } - - @Transactional - public String createThumbnail(File source) throws IOException, JCodecException { - String url = UUID.randomUUID().toString(); - Picture picture = FrameGrab.getFrameFromFile(source, 0); - BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture); - ImageIO.write(bufferedImage, "jpg", new File(url)); - return url; - } - - @Transactional - public Challenge_vs_TrialDto compareDetail(Long trialId){ - Trial trial = trialRepository.findById(trialId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + trialId)); - return new Challenge_vs_TrialDto(trial); - } - - @Transactional - public double runSimilarity(Long trialId){ - - Trial trial = trialRepository.findById(trialId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + trialId)); - - Challenge challenge = trial.getChallenge(); - - List keypointsListA = challenge.getChallengePose().getKeypointsList(); - List keypointsListB = trial.getTrialPose().getKeypointsList(); - - double similarity = 0.0; - - System.out.println("photo.equals(challenge.getType()) = " + "photo".equals(challenge.getType())); - - if ("photo".equals(challenge.getType())){ - System.out.println("hihi1"); - List keypointsA = keypointsListA.get(0).getKeypoints(); - List keypointsB = keypointsListB.get(0).getKeypoints(); - similarity = poseService.estimateSimilarity(keypointsA , keypointsB); - System.out.println("similarity for photo = " + similarity); - } - else { - System.out.println("hihi2"); - System.out.println("challengeId = " + challenge.getId()); - System.out.println("trialId = " + trialId); - similarity = poseService.DTWDistance(keypointsListA, keypointsListB); - System.out.println("similarity for video = " + similarity); - } - System.out.println("similarity result = " + similarity); - similarity = similarity * 100; - similarity = Math.round(similarity * 100) / 100.0; - trial.setScore(similarity); - trialRepository.save(trial); - - return similarity; - } - - @Transactional - public List getBestTrials(Long challengeId) { - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + 0)); - List trials = challenge.getTrialList(); - MyComparator comparator = new MyComparator(); - Collections.sort(trials, comparator); - List best_trials; - if (trials.size() < 5){ - best_trials = trials.subList(0, trials.size()); - } else{ - best_trials = trials.subList(0, 5); - } - return best_trials.stream().map(TrialResponseDto::new).collect(Collectors.toList()); - } - - @Transactional - public List getMyList(String username) { - User user = userRepository.findUserByUserName(username) - .orElseThrow(() -> new IllegalArgumentException("해당 유저가 없습니다. username=" + username)); - - List trials = new ArrayList<>(); - - for (TrialPose trialPose : user.getTrialPoseList()) { - trials.add(trialPose.getTrial()); - } - - return trials.stream().map(TrialResponseDto::new).collect(Collectors.toList()); - } - - @Transactional - public TrialResponseDto getTrial(Long trialId) { - Trial trial = trialRepository.findById(trialId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + trialId)); - return new TrialResponseDto(trial); - } - - @Transactional - public List getTrialList(Long challengeId) { - Challenge challenge = challengeRepository.findById(challengeId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + 0)); - List trials = challenge.getTrialList(); - return trials.stream().map(TrialResponseDto::new).collect(Collectors.toList()); - } - - @Transactional - public Long deleteTrial(Long trialId) { - Trial trial = trialRepository.findById(trialId) - .orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + trialId)); - trialRepository.delete(trial); - return trialId; - } -} \ No newline at end of file