@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ImageUpload {
@Id @GeneratedValue
private Long id;
private String originFilename;
private String storageImageName;
@ManyToOne
@JoinColumn(name = "post_id")
private Post post;
private ImageUpload(String originFilename, String storageImageName) {
this.originFilename = originFilename;
this.storageImageName = storageImageName;
}
public static List<ImageUpload> createImageUploadList(List<MultipartFile> multipartFiles) {
if( multipartFiles.isEmpty()) {
return null;
}
List<ImageUpload> storeImageList = new ArrayList<>();
for (MultipartFile multipartFile : multipartFiles) {
storeImageList.add(ImageUpload.createImageUpload(multipartFile));
}
return storeImageList;
}
public static ImageUpload createImageUpload(MultipartFile multipartFile) {
if (multipartFile.isEmpty()) {
return null;
}
String originalFilename = multipartFile.getOriginalFilename();
String storeImageName = createStoreImageName(originalFilename);
return new ImageUpload(originalFilename, storeImageName);
}
private static String createStoreImageName(String originalFilename) {
String ext = extractExt(originalFilename);
return UUID.randomUUID() + "." + ext;
}
private static String extractExt(String originalFilename) {
// test.png ->
int pos = originalFilename.lastIndexOf(".");
return originalFilename.substring(pos + 1);
}
protected void setPost(Post post) {
this.post = post;
}
}
@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final ImageUploadRepository imageUploadRepository;
@Transactional
public Long postSave(List<MultipartFile> multipartFiles, String content){
Post post = new Post(content);
List<ImageUpload> imageUploadList = ImageUpload.createImageUploadList(multipartFiles);
if(imageUploadList != null) {
// 이미지 저장 로직 (추가 예정)
post.addImageUploads(imageUploadList);
imageUploadRepository.saveAll(imageUploadList);
}
return postRepository.save(post).getId();
}
}
//@ExtendWith(SpringExtension.class)
//@DataJpaTest
class ImageUploadTest {
@Test
void createImageUploadTest() throws IOException {
// given
URL resource = getClass().getResource("/img/404.png");
MockMultipartFile multipartFile = new MockMultipartFile("image",
"test.png", "image/png",
new FileInputStream(resource.getFile()));
// when
ImageUpload imageUpload = ImageUpload.createImageUpload(multipartFile);
// then
String originFilename = (String) ReflectionTestUtils.getField(imageUpload, "originFilename");
assertThat(originFilename).isEqualTo("test.png");
assertThat(imageUpload).extracting("storageImageName").isNotNull();
}
@Test
void pathTest() throws FileNotFoundException {
URL resource = getClass().getResource("/img/404.png");
System.out.println("URL : " + resource );
FileInputStream fileInputStream = new FileInputStream(resource.getFile());
System.out.println("fileInputStream : " + fileInputStream);
}
@Test
void createImageUploadListTest() throws IOException {
// given
URL resource = getClass().getResource("/img/404.png");
MockMultipartFile multipartFile1 = new MockMultipartFile("image",
"404.png", "image/png",
new FileInputStream(resource.getFile()));
URL resource2 = getClass().getResource("/img/raspberry.png");
MockMultipartFile multipartFile2 = new MockMultipartFile("image2",
"raspberry.png", "image/png",
new FileInputStream(resource2.getFile()));
List<MultipartFile> arrayList = new ArrayList<>();
arrayList.add(multipartFile1);
arrayList.add(multipartFile2);
// when
List<ImageUpload> imageUploadList = ImageUpload.createImageUploadList(arrayList);
// then
assertThat(imageUploadList).extracting("originFilename")
.contains("raspberry.png", "404.png");
assertThat(imageUploadList).extracting("storageImageName")
.isNotNull();
}
}
다음에는 image를 저장하는 내용과 그에 관련된 테스트 코드를 작성 할 예정이다.
깃 잘못 사용해서 코드가 다 날아가고 commit 기록이 없어서 멘붕...
git reflog
명령어로 숨겨진? 커밋 찾아서 복구했다
정말 식겁했다 휴