Skip to content

feat: 이미지저장 #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package hello.aimju.controller;

import hello.aimju.image.Service.ImageService;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api")
public class ImageController {
Expand All @@ -18,8 +21,8 @@ public ImageController(ImageService imageService) {
* 사진 업로드 후 재료 인식
* */
@PostMapping("/photo-recognition")
public String uploadImage(@RequestParam("file") MultipartFile file) {
return imageService.uploadAndProcessImage(file);
public String uploadImage(@RequestParam("file") MultipartFile file, HttpSession httpSession) throws IOException {
return imageService.uploadAndProcessImage(file,httpSession);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import hello.aimju.image.Enum.IngredientMapper;
import hello.aimju.login.session.SessionConst;
import hello.aimju.tteesstt.Image;
import hello.aimju.tteesstt.ImageRepository;
import hello.aimju.user.domain.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -22,12 +28,25 @@ public class ImageService {
// Roboflow API Endpoint
@Value("${roboflow.api-endpoint}")
private String ROBOFLOW_API_ENDPOINT;
@Autowired
private ImageRepository imageRepository;

/**
* roboflow 모델 API에 이미지 보내서 인식된 재료를 String 형으로 가져옴
* @return : "파, 마늘, 양파, 삼겹살, ..."
* */
public String uploadAndProcessImage(MultipartFile imageFile) {
public String uploadAndProcessImage(MultipartFile imageFile, HttpSession httpSession) throws IOException {
//이미지 저장 로직
String fileName = imageFile.getOriginalFilename();
byte[] imageData = imageFile.getBytes();
// User user = getUserFromSession(httpSession);

Image image = new Image();
image.setName(fileName);
image.setImage(imageData);
// image.setUser(user);
imageRepository.save(image);

try {
// Base64 인코딩
String encodedFile = new String(Base64.getEncoder().encode(imageFile.getBytes()), StandardCharsets.US_ASCII);
Expand Down Expand Up @@ -171,4 +190,16 @@ private String joinIngredients(Set<Integer> classIds) {
// StringJoiner를 사용하여 최종 결과 문자열 생성
return resultJoiner.toString();
}

private User getUserFromSession(HttpSession session) {
// 세션에서 사용자 정보 가져오기
User loginUser = (User) session.getAttribute(SessionConst.LOGIN_MEMBER);

// 만약 세션에 사용자 정보가 없다면 로그인하지 않은 상태이므로 적절히 처리
if (loginUser == null) {
throw new IllegalArgumentException("로그인이 필요합니다.");
}

return loginUser;
}
}
29 changes: 29 additions & 0 deletions backend/src/main/java/hello/aimju/tteesstt/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hello.aimju.tteesstt;

import hello.aimju.user.domain.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;


@Entity
@Table(name = "images")
@Getter
@Setter
public class Image {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Lob
@Column(name = "image", nullable = false, columnDefinition = "LONGBLOB")
private byte[] image;

// @ManyToOne(fetch = FetchType.LAZY)
// @JoinColumn(name = "user_id")
// private User user;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hello.aimju.tteesstt;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ImageRepository extends JpaRepository<Image, Long> {
}