Skip to content

Commit fa50184

Browse files
전상현전상현
authored andcommitted
Fix: add 2 image url using cloudinary
1 parent 3314e2f commit fa50184

8 files changed

Lines changed: 15088 additions & 2 deletions

File tree

build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
id 'java'
33
id 'org.springframework.boot' version '3.4.3'
44
id 'io.spring.dependency-management' version '1.1.7'
5+
id 'application' // 이 줄을 추가
56
}
67

78
group = 'com.leafvillage'
@@ -32,7 +33,7 @@ dependencies {
3233
annotationProcessor 'org.projectlombok:lombok'
3334
testImplementation 'org.springframework.boot:spring-boot-starter-test'
3435
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
35-
36+
implementation 'com.cloudinary:cloudinary-http44:1.29.0' // 최신 버전 확인
3637
// 로컬 개발용 DB
3738
runtimeOnly 'com.h2database:h2'
3839

@@ -44,3 +45,9 @@ dependencies {
4445
tasks.named('test') {
4546
useJUnitPlatform()
4647
}
48+
49+
50+
application {
51+
// 실행할 메인 클래스의 FQCN(패키지명을 포함한 클래스 이름)을 지정합니다.
52+
mainClassName = 'com.leafvillage.backend.DemoApplication'
53+
}

logs/backend.log

Lines changed: 14902 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leafvillage.backend.Controller;
2+
3+
import com.leafvillage.backend.service.ImageService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
import org.springframework.web.multipart.MultipartFile;
8+
9+
@RestController
10+
@RequestMapping("/images")
11+
public class ImageController {
12+
13+
@Autowired
14+
private ImageService imageService;
15+
16+
// 회사 로고 업데이트 API – company_id를 기준으로 업데이트
17+
// 예: POST /images/upload?companyId=c000018 와 함께 file 전송
18+
@PostMapping("/upload")
19+
public ResponseEntity<String> uploadImage(@RequestParam("companyId") String companyId,
20+
@RequestParam("file") MultipartFile file) {
21+
boolean updated = imageService.updateCompanyLogoById(companyId, file);
22+
if (updated) {
23+
return ResponseEntity.ok("회사 로고 업데이트 성공");
24+
} else {
25+
return ResponseEntity.badRequest().body("회사 로고 업데이트 실패");
26+
}
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.leafvillage.backend;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(DemoApplication.class, args);
10+
}
11+
}

src/main/java/com/leafvillage/backend/entity/Company.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import jakarta.persistence.*;
44

55
@Entity
6-
@Table(name = "company")
6+
@Table(name = "company", schema = "news_db")
77
public class Company {
88

99
// 회사 고유 식별자(PK) - VARCHAR(36)으로 UUID 등 수동 저장을 가정
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.leafvillage.backend.entity;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
@Table(name = "company", schema = "news_db")
7+
public class Image {
8+
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private Long id;
12+
13+
// 회사 ID (예: "c000018")
14+
@Column(name = "company_id", unique = true)
15+
private String companyId;
16+
17+
// (옵션) 회사명 – 필요에 따라 유지하거나 제거할 수 있습니다.
18+
@Column(name = "company")
19+
private String company;
20+
21+
// 로고 이미지 URL
22+
@Column(name = "logo_image")
23+
private String imageUrl;
24+
25+
public Image() {}
26+
27+
// 생성자나 getter/setter는 필요에 따라 추가
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public String getCompanyId() {
33+
return companyId;
34+
}
35+
36+
public String getCompany() {
37+
return company;
38+
}
39+
40+
public String getImageUrl() {
41+
return imageUrl;
42+
}
43+
44+
public void setId(Long id) {
45+
this.id = id;
46+
}
47+
48+
public void setCompanyId(String companyId) {
49+
this.companyId = companyId;
50+
}
51+
52+
public void setCompany(String company) {
53+
this.company = company;
54+
}
55+
56+
public void setImageUrl(String imageUrl) {
57+
this.imageUrl = imageUrl;
58+
}
59+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.leafvillage.backend.repository;
2+
3+
import com.leafvillage.backend.entity.Image;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Modifying;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
@Repository
11+
public interface ImageRepository extends JpaRepository<Image, Long> {
12+
13+
// company_id 기준으로 로고 이미지 업데이트
14+
@Modifying
15+
@Query("UPDATE Image i SET i.imageUrl = :imageUrl WHERE i.companyId = :companyId")
16+
int updateLogoImageByCompanyId(@Param("imageUrl") String imageUrl, @Param("companyId") String companyId);
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.leafvillage.backend.service;
2+
3+
import com.cloudinary.Cloudinary;
4+
import com.cloudinary.utils.ObjectUtils;
5+
import com.leafvillage.backend.repository.ImageRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
import org.springframework.web.multipart.MultipartFile;
10+
11+
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
@Service
18+
public class ImageService {
19+
20+
private final Cloudinary cloudinary;
21+
22+
@Autowired
23+
private ImageRepository imageRepository;
24+
25+
public ImageService() {
26+
// Cloudinary 계정 정보 (본인 계정의 값으로 변경)
27+
Map<String, String> config = new HashMap<>();
28+
config.put("cloud_name", "dncizjyjo");
29+
config.put("api_key", "451525654937799");
30+
config.put("api_secret", "HeW5BQcEvIl87MvohARwUPdQO2s");
31+
this.cloudinary = new Cloudinary(config);
32+
}
33+
34+
// MultipartFile을 임시 파일로 변환 후 Cloudinary에 업로드하여 이미지 URL 반환
35+
public String uploadToCloudinary(MultipartFile multipartFile) throws IOException {
36+
File file = convertMultiPartToFile(multipartFile);
37+
Map uploadResult = cloudinary.uploader().upload(file, ObjectUtils.emptyMap());
38+
file.delete(); // 임시 파일 삭제
39+
return (String) uploadResult.get("secure_url");
40+
}
41+
42+
private File convertMultiPartToFile(MultipartFile file) throws IOException {
43+
File convFile = new File(System.getProperty("java.io.tmpdir") + "/" + file.getOriginalFilename());
44+
try (FileOutputStream fos = new FileOutputStream(convFile)) {
45+
fos.write(file.getBytes());
46+
}
47+
return convFile;
48+
}
49+
50+
// Cloudinary 업로드 후, company_id 기준으로 DB 업데이트 수행
51+
@Transactional
52+
public boolean updateCompanyLogoById(String companyId, MultipartFile file) {
53+
try {
54+
String imageUrl = uploadToCloudinary(file);
55+
int updatedCount = imageRepository.updateLogoImageByCompanyId(imageUrl, companyId);
56+
return updatedCount > 0;
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
return false;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)