diff --git a/domain/build.gradle b/domain/build.gradle index 1bede61..9fbf82c 100644 --- a/domain/build.gradle +++ b/domain/build.gradle @@ -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 { diff --git a/domain/src/main/java/com/codingland/domain/home/dto/ResponseHomeDto.java b/domain/src/main/java/com/codingland/domain/home/dto/ResponseHomeDto.java index 391aa6f..e3ba4ac 100644 --- a/domain/src/main/java/com/codingland/domain/home/dto/ResponseHomeDto.java +++ b/domain/src/main/java/com/codingland/domain/home/dto/ResponseHomeDto.java @@ -5,6 +5,7 @@ public record ResponseHomeDto( //{id, character_id, character_type, character_name} Long id, - ResponseCharacterDto character + ResponseCharacterDto character, + String userPicture ){ } diff --git a/domain/src/main/java/com/codingland/domain/home/service/HomeService.java b/domain/src/main/java/com/codingland/domain/home/service/HomeService.java index c7081e5..f5ad207 100644 --- a/domain/src/main/java/com/codingland/domain/home/service/HomeService.java +++ b/domain/src/main/java/com/codingland/domain/home/service/HomeService.java @@ -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() ); } @@ -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); diff --git a/domain/src/main/java/com/codingland/domain/image/controller/ImageController.java b/domain/src/main/java/com/codingland/domain/image/controller/ImageController.java new file mode 100644 index 0000000..47fa0d7 --- /dev/null +++ b/domain/src/main/java/com/codingland/domain/image/controller/ImageController.java @@ -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 download(@PathVariable String fileName) { + byte[] downloadImage = imageService.downloadImage(fileName); + return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.valueOf("image/png")) + .body(downloadImage); + } +} diff --git a/domain/src/main/java/com/codingland/domain/image/entity/Image.java b/domain/src/main/java/com/codingland/domain/image/entity/Image.java new file mode 100644 index 0000000..efd336a --- /dev/null +++ b/domain/src/main/java/com/codingland/domain/image/entity/Image.java @@ -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; + } +} diff --git a/domain/src/main/java/com/codingland/domain/image/repository/ImageRepository.java b/domain/src/main/java/com/codingland/domain/image/repository/ImageRepository.java new file mode 100644 index 0000000..0a2f2a7 --- /dev/null +++ b/domain/src/main/java/com/codingland/domain/image/repository/ImageRepository.java @@ -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 { + Optional findByName(String fileName); +} diff --git a/domain/src/main/java/com/codingland/domain/image/service/ImageService.java b/domain/src/main/java/com/codingland/domain/image/service/ImageService.java new file mode 100644 index 0000000..e795c08 --- /dev/null +++ b/domain/src/main/java/com/codingland/domain/image/service/ImageService.java @@ -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()); + } +} diff --git a/domain/src/main/java/com/codingland/domain/image/utils/ImageUtils.java b/domain/src/main/java/com/codingland/domain/image/utils/ImageUtils.java new file mode 100644 index 0000000..e835abd --- /dev/null +++ b/domain/src/main/java/com/codingland/domain/image/utils/ImageUtils.java @@ -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()); + } + } +}