-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from WhatIsMethIs/4-recog-by-image
Search by image demo.
- Loading branch information
Showing
8 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/WhatIsMethIs/src/dto/medicine/PillImageDto.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,35 @@ | ||
package com.WhatIsMethIs.src.dto.medicine; | ||
|
||
import com.WhatIsMethIs.src.entity.PillImage; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
public class PillImageDto { | ||
private int id; | ||
private String originalFileName; | ||
private String storedFilePath; | ||
private long fileSize; | ||
private LocalDateTime createdAt; | ||
|
||
public static PillImageDto toDto(PillImage pillImage){ | ||
PillImageDto pillImageDto = new PillImageDto(); | ||
|
||
pillImageDto.id = pillImage.getId(); | ||
if(pillImage.getOriginalFileName() != null){ | ||
pillImageDto.originalFileName = pillImage.getOriginalFileName(); | ||
} | ||
if(pillImage.getStoredFilePath() != null){ | ||
pillImageDto.storedFilePath = pillImage.getStoredFilePath(); | ||
} | ||
pillImageDto.fileSize = pillImage.getFileSize(); | ||
if(pillImage.getCreatedAt() != null){ | ||
pillImageDto.createdAt = pillImage.getCreatedAt(); | ||
} | ||
|
||
return pillImageDto; | ||
} | ||
} |
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,53 @@ | ||
package com.WhatIsMethIs.src.entity; | ||
|
||
import com.WhatIsMethIs.src.dto.medicine.PillImageDto; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "pill_image") | ||
@NoArgsConstructor | ||
@Schema(description = "식별 요청 이미지") | ||
public class PillImage { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
@Column(name = "original_file_name") | ||
@NotEmpty | ||
private String originalFileName; | ||
@Column(name = "stored_file_path") | ||
@NotEmpty | ||
private String storedFilePath; | ||
@Column(name = "file_size") | ||
private long fileSize; | ||
@CreationTimestamp | ||
@Column(name = "created_at") | ||
private LocalDateTime createdAt; | ||
|
||
public static PillImage toEntity(PillImageDto pillImageDto){ | ||
PillImage pillImage = new PillImage(); | ||
|
||
pillImage.id = pillImageDto.getId(); | ||
if(pillImageDto.getOriginalFileName() != null){ | ||
pillImage.originalFileName = pillImageDto.getOriginalFileName(); | ||
} | ||
if(pillImageDto.getStoredFilePath() != null){ | ||
pillImage.storedFilePath = pillImageDto.getStoredFilePath(); | ||
} | ||
pillImage.fileSize = pillImageDto.getFileSize(); | ||
if(pillImageDto.getCreatedAt() != null){ | ||
pillImage.createdAt = pillImageDto.getCreatedAt(); | ||
} | ||
|
||
return pillImage; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/WhatIsMethIs/src/repository/PillImageRepository.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,7 @@ | ||
package com.WhatIsMethIs.src.repository; | ||
|
||
import com.WhatIsMethIs.src.entity.PillImage; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PillImageRepository extends JpaRepository<PillImage, Integer> { | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/WhatIsMethIs/src/service/PillImageService.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,57 @@ | ||
package com.WhatIsMethIs.src.service; | ||
|
||
import com.WhatIsMethIs.config.BaseException; | ||
import com.WhatIsMethIs.config.BaseResponseStatus; | ||
import com.WhatIsMethIs.src.dto.medicine.PillImageDto; | ||
import com.WhatIsMethIs.src.entity.PillImage; | ||
import com.WhatIsMethIs.src.repository.PillImageRepository; | ||
import com.WhatIsMethIs.utils.FileHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PillImageService { | ||
private final PillImageRepository pillImageRepository; | ||
|
||
private final FileHandler fileHandler; | ||
|
||
public List<PillImageDto> addPillImage( | ||
List<MultipartFile> files | ||
) throws BaseException { | ||
List<PillImageDto> pillImageDtos = new ArrayList<>(); | ||
|
||
try { | ||
List<PillImageDto> list = fileHandler.parseImageInfo(files); | ||
if(list.isEmpty()){ | ||
throw new BaseException(BaseResponseStatus.FILE_NOT_FOUND_EXCEPTION); | ||
} | ||
|
||
/*for(PillImageDto pillImageDto : list){ | ||
PillImage pillImage = new PillImage(); | ||
pillImage.setOriginalFileName(pillImageDto.getOriginalFileName()); | ||
pillImage.setStoredFilePath(pillImageDto.getStoredFilePath()); | ||
pillImage.setFileSize(pillImageDto.getFileSize()); | ||
pillImage.setCreatedAt(pillImageDto.getCreatedAt()); | ||
System.out.println(pillImage.getId()); | ||
pillImageDtos.add( | ||
PillImageDto.toDto( | ||
pillImageRepository.save( | ||
pillImage | ||
) | ||
) | ||
); | ||
}*/ | ||
} catch (Exception e) { | ||
throw new BaseException(BaseResponseStatus.FILE_NOT_FOUND_EXCEPTION); | ||
} | ||
|
||
return pillImageDtos; | ||
} | ||
} |
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,88 @@ | ||
package com.WhatIsMethIs.utils; | ||
|
||
import com.WhatIsMethIs.src.dto.medicine.PillImageDto; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Component | ||
public class FileHandler { | ||
public List<PillImageDto> parseImageInfo( | ||
List<MultipartFile> multipartFiles | ||
) throws IOException { | ||
// 반환을 할 파일 리스트 | ||
List<PillImageDto> fileList = new ArrayList<>(); | ||
|
||
// 파일이 빈 것이 들어오면 빈 것을 반환 | ||
if(multipartFiles.isEmpty()){ | ||
return fileList; | ||
} | ||
|
||
// 파일 이름을 업로드 한 날짜로 바꾸어서 저장 | ||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); | ||
String current_date = simpleDateFormat.format(new Date()); | ||
|
||
// 프로젝트 폴더에 저장하기 위해 절대경로를 설정 (Window 의 Tomcat 은 Temp 파일을 이용) | ||
String absolutePath = new File("").getAbsolutePath() + "\\"; | ||
|
||
// 경로를 지정하고 이미지 저장 | ||
String path = "images/" + current_date; | ||
File file = new File(path); | ||
// 저장할 위치의 디렉토리가 존지하지 않을 경우 | ||
if(!file.exists()){ | ||
// mkdir() 함수와 다른 점은 상위 디렉토리가 존재하지 않을 때 그것까지 생성 | ||
file.mkdirs(); | ||
} | ||
|
||
// 파일 처리 | ||
for (MultipartFile multipartFile : multipartFiles){ | ||
// 파일이 비어 있지 않을 때 작업을 시작 | ||
if(!multipartFile.isEmpty()){ | ||
// jpeg, png, gif 파일들만 받아서 처리 | ||
String contentType = multipartFile.getContentType(); | ||
String originalFileExtension; | ||
// 확장자 명이 없으면 이 파일은 잘 못 된 것 | ||
if (ObjectUtils.isEmpty(contentType)){ | ||
break; | ||
} | ||
else{ | ||
if(contentType.contains("image/jpeg")){ | ||
originalFileExtension = ".jpg"; | ||
} | ||
else if(contentType.contains("image/png")){ | ||
originalFileExtension = ".png"; | ||
} | ||
else if(contentType.contains("image/gif")){ | ||
originalFileExtension = ".gif"; | ||
} | ||
// 다른 파일 명이면 break | ||
else{ | ||
break; | ||
} | ||
} | ||
// 각 이름은 겹치면 안되므로 나노 초까지 동원하여 지정 | ||
String new_file_name = Long.toString(System.nanoTime()) + originalFileExtension; | ||
// 생성 후 리스트에 추가 | ||
PillImageDto pillImageDto = new PillImageDto(); | ||
pillImageDto.setOriginalFileName(multipartFile.getOriginalFilename()); | ||
pillImageDto.setStoredFilePath(path + "/" + new_file_name); | ||
pillImageDto.setFileSize(multipartFile.getSize()); | ||
fileList.add(pillImageDto); | ||
|
||
// 저장된 파일로 변경하여 이를 보여주기 위함 | ||
System.out.println(absolutePath + path + "/" + new_file_name); | ||
file = new File(absolutePath + path + "/" + new_file_name); | ||
multipartFile.transferTo(file); | ||
} | ||
} | ||
|
||
return fileList; | ||
} | ||
} |