Skip to content

Commit

Permalink
[Feat] Home API가 값을 반환할 때 Picture 값도 반환하도록 변경, Image 관련 코드를 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
NARUBROWN committed Nov 23, 2024
1 parent cd49c0b commit 04e095b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 3 deletions.
2 changes: 2 additions & 0 deletions domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

implementation 'org.xerial.snappy:snappy-java:1.1.10.5'
}

bootJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public record ResponseHomeDto(
//{id, character_id, character_type, character_name}
Long id,
ResponseCharacterDto character
ResponseCharacterDto character,
String userPicture
){
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public ResponseHomeDto getHome(Long user_id) {
.level(foundHome.getCharacter().getLevel())
.type(foundHome.getCharacter().getType())
.activityPoints(foundHome.getCharacter().getActivityPoints())
.build()
.build(),
foundHome.getUser().getPicture()
);
}

Expand All @@ -68,7 +69,8 @@ public ResponseHomeListDto getHomeList() {
.level(home.getCharacter().getLevel())
.type(home.getCharacter().getType())
.activityPoints(home.getCharacter().getActivityPoints())
.build()
.build(),
home.getUser().getPicture()
))
.collect(Collectors.toList());
return new ResponseHomeListDto(homeDtoList);
Expand Down
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 domain/src/main/java/com/codingland/domain/image/entity/Image.java
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;
}
}
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);
}
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());
}
}
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());
}
}
}

0 comments on commit 04e095b

Please sign in to comment.