-
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.
[Feat] Home API가 값을 반환할 때 Picture 값도 반환하도록 변경, Image 관련 코드를 추가
- Loading branch information
Showing
8 changed files
with
107 additions
and
3 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
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
25 changes: 25 additions & 0 deletions
25
domain/src/main/java/com/codingland/domain/image/controller/ImageController.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,25 @@ | ||
package com.codingland.domain.image.controller; | ||
|
||
import com.codingland.domain.image.service.ImageService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/v1/api/image") | ||
@RequiredArgsConstructor | ||
public class ImageController { | ||
private final ImageService imageService; | ||
|
||
@GetMapping("/{fileName}") | ||
public ResponseEntity<byte[]> download(@PathVariable String fileName) { | ||
byte[] downloadImage = imageService.downloadImage(fileName); | ||
return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.valueOf("image/png")) | ||
.body(downloadImage); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
domain/src/main/java/com/codingland/domain/image/entity/Image.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,27 @@ | ||
package com.codingland.domain.image.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Image { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
private String type; | ||
@Column(columnDefinition = "MEDIUMBLOB") | ||
private byte[] imageData; | ||
|
||
@Builder | ||
public Image(String name, String type, byte[] imageData) { | ||
this.name = name; | ||
this.type = type; | ||
this.imageData = imageData; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
domain/src/main/java/com/codingland/domain/image/repository/ImageRepository.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,10 @@ | ||
package com.codingland.domain.image.repository; | ||
|
||
import com.codingland.domain.image.entity.Image; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
Optional<Image> findByName(String fileName); | ||
} |
19 changes: 19 additions & 0 deletions
19
domain/src/main/java/com/codingland/domain/image/service/ImageService.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,19 @@ | ||
package com.codingland.domain.image.service; | ||
|
||
import com.codingland.domain.image.entity.Image; | ||
import com.codingland.domain.image.repository.ImageRepository; | ||
import com.codingland.domain.image.utils.ImageUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService { | ||
private final ImageRepository imageRepository; | ||
|
||
public byte[] downloadImage(String fileName) { | ||
Image foundImage = imageRepository.findByName(fileName) | ||
.orElseThrow(() -> new RuntimeException("test")); | ||
return ImageUtils.decompressImage(foundImage.getImageData()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
domain/src/main/java/com/codingland/domain/image/utils/ImageUtils.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,18 @@ | ||
package com.codingland.domain.image.utils; | ||
|
||
import org.xerial.snappy.Snappy; | ||
|
||
import java.io.IOException; | ||
|
||
public class ImageUtils { | ||
public static byte[] compressImage(byte[] data) throws IOException { | ||
return Snappy.compress(data); | ||
} | ||
public static byte[] decompressImage(byte[] data) { | ||
try { | ||
return Snappy.uncompress(data); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e.getMessage()); | ||
} | ||
} | ||
} |