-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc5b9c4
commit 12cdf61
Showing
17 changed files
with
278 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.photo.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.resource.PathResourceResolver; | ||
|
||
@Component | ||
public class WebMvcConfig implements WebMvcConfigurer { // web 설정 파일 | ||
|
||
@Value("${file.path}") | ||
private String imageUploadRoute; | ||
|
||
@Override | ||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||
WebMvcConfigurer.super.addResourceHandlers(registry); | ||
|
||
registry | ||
.addResourceHandler("/upload/**") | ||
.addResourceLocations("file:///"+imageUploadRoute) | ||
.setCachePeriod(60*10*6) | ||
.resourceChain(true) | ||
.addResolver(new PathResourceResolver()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.photo.domain.image; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.photo.domain.BaseTimeEntity; | ||
import com.photo.domain.user.User; | ||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
@Entity | ||
public class Image extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private int id; | ||
|
||
private String caption; | ||
private String imageUrl; | ||
|
||
@JoinColumn(name = "userId") | ||
@ManyToOne | ||
private User user; | ||
|
||
@Builder | ||
public Image(String caption, String imageUrl, User user) { | ||
this.caption = caption; | ||
this.imageUrl = imageUrl; | ||
this.user = user; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.photo.domain.image; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Integer> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/photo/handler/exception/CustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.photo.handler.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Map; | ||
|
||
public class CustomException extends RuntimeException{ | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public CustomException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.photo.service; | ||
|
||
import com.photo.config.auth.CustomUserDetails; | ||
import com.photo.domain.image.Image; | ||
import com.photo.domain.image.ImageRepository; | ||
import com.photo.web.dto.image.ImageUploadDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ImageService { | ||
|
||
private final ImageRepository imageRepository; | ||
|
||
@Value("${file.path}") | ||
private String imageUploadRoute; | ||
|
||
@Transactional | ||
public void imageUpload(CustomUserDetails customUserDetails, ImageUploadDto imageUploadDto) { | ||
UUID uuid = UUID.randomUUID(); | ||
String imageFileName = uuid+"_"+imageUploadDto.getFile().getOriginalFilename(); | ||
|
||
Path imageFilePath = Paths.get(imageUploadRoute+imageFileName); | ||
|
||
try{ | ||
Files.write(imageFilePath, imageUploadDto.getFile().getBytes()); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
|
||
Image image = imageUploadDto.toEntity(imageFileName, customUserDetails.getUser()); | ||
imageRepository.save(image); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
package com.photo.web; | ||
|
||
import com.photo.config.auth.CustomUserDetails; | ||
import com.photo.handler.exception.CustomValidationException; | ||
import com.photo.service.ImageService; | ||
import com.photo.web.dto.image.ImageUploadDto; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class ImageController { | ||
|
||
private final ImageService imageService; | ||
|
||
@GetMapping({"/","/image/story"}) | ||
public String story() { | ||
return "image/story"; | ||
} | ||
|
||
@GetMapping({"/image/popular"}) | ||
@GetMapping("/image/popular") | ||
public String popular() { | ||
return "image/popular"; | ||
} | ||
|
||
@GetMapping({"/image/upload"}) | ||
@GetMapping("/image/upload") | ||
public String upload() { | ||
return "image/upload"; | ||
} | ||
|
||
@PostMapping("/image") | ||
public String imageUpload(@AuthenticationPrincipal CustomUserDetails customUserDetails, ImageUploadDto imageUploadDto) { | ||
if(imageUploadDto.getFile().isEmpty()) { | ||
throw new CustomValidationException("이미지가 첨부되지 않았습니다.",null); | ||
} | ||
|
||
imageService.imageUpload(customUserDetails, imageUploadDto); | ||
return "redirect:/user/"+customUserDetails.getUser().getId(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.photo.web.dto.image; | ||
|
||
import com.photo.domain.image.Image; | ||
import com.photo.domain.user.User; | ||
import lombok.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Data | ||
public class ImageUploadDto { | ||
private MultipartFile file; | ||
private String caption; | ||
|
||
@Builder | ||
public ImageUploadDto(MultipartFile file, String caption) { | ||
this.file = file; | ||
this.caption = caption; | ||
} | ||
|
||
public Image toEntity(String imageUrl, User user){ | ||
return Image.builder() | ||
.caption(caption) | ||
.imageUrl(imageUrl) | ||
.user(user) | ||
.build(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.photo.web.dto.user; | ||
|
||
import com.photo.domain.user.User; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Data | ||
public class UserProfileDto { | ||
private boolean pageUserState; | ||
private int imageCount; | ||
private User user; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.