-
Notifications
You must be signed in to change notification settings - Fork 3
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
π Feature image enhancement #85
base: feature
Are you sure you want to change the base?
Changes from all commits
82f51ea
0bd8a1f
88c5d92
e83f62c
bebc18a
300b564
ed3bb10
de94e13
e16d0a5
ff039e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import com.backendoori.ootw.avatar.domain.ItemType; | ||
import com.backendoori.ootw.avatar.domain.Sex; | ||
import com.backendoori.ootw.common.validation.Enum; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μλλ μ¬κΈ°μ Enum μ΄λ
Έν
μ΄μ
μ μλ ν΄λμ€κ° μ§κΈ λ°λ PRμλ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. νμΈμ΄ νμνκ² λ€μ |
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record AvatarItemRequest( | ||
@Enum(enumClass = ItemType.class) | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.backendoori.ootw.image.domain; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Table(name = "images") | ||
@Getter | ||
@Entity | ||
@Builder | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Image { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(name = "image_url", unique = true) | ||
private String ImageUrl; | ||
|
||
@Column(name = "filename") | ||
private String fileName; | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.backendoori.ootw.image.dto; | ||
|
||
import com.backendoori.ootw.image.domain.Image; | ||
|
||
public record ImageFile( | ||
String url, | ||
String fileName | ||
) { | ||
public static ImageFile from(Image image){ | ||
return new ImageFile(image.getImageUrl(), image.getFileName()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.backendoori.ootw.image.repository; | ||
|
||
import com.backendoori.ootw.image.domain.Image; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.backendoori.ootw.image.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import com.backendoori.ootw.avatar.domain.AvatarItem; | ||
import com.backendoori.ootw.avatar.repository.AvatarItemRepository; | ||
import com.backendoori.ootw.image.domain.Image; | ||
import com.backendoori.ootw.image.repository.ImageRepository; | ||
import com.backendoori.ootw.post.domain.Post; | ||
import com.backendoori.ootw.post.repository.PostRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageUpdateService { | ||
|
||
private final PostRepository postRepository; | ||
private final AvatarItemRepository avatarItemRepository; | ||
private final ImageRepository imageRepository; | ||
private final ImageService imageService; | ||
public static final String TIMEZONE = "Asia/Seoul"; | ||
public static final String CRON = "0 20 00 10 * ?"; | ||
|
||
@Scheduled(cron = CRON, zone = TIMEZONE) // 맀μ 20μΌ μ€μ 00μ 10λΆμ μ€ν | ||
@Async | ||
@Transactional | ||
public void deleteUnused(){ | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π |
||
|
||
List<String> urls = new ArrayList<>(); | ||
|
||
postRepository.findAll() | ||
.stream() | ||
.map(Post::getImageUrl) | ||
.forEach(urls::add); | ||
|
||
avatarItemRepository.findAll() | ||
.stream() | ||
.map(AvatarItem::getImageUrl) | ||
.forEach(urls::add); | ||
|
||
List<Image> images = imageRepository.findAll(); | ||
|
||
for(Image image : images){ | ||
if(!urls.contains(image.getImageUrl())){ | ||
imageService.delete(image.getFileName()); | ||
} | ||
} | ||
Comment on lines
+33
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ°°μ΄μ μ¬μ©νλ€ λ³΄λ μ΄λ―Έμ§ μκ° λ§μ κ²½μ°μλ μκ°μ΄ μλΉν μ€λ걸릴 μ μκ² λ€μ |
||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
package com.backendoori.ootw.common.image; | ||
package com.backendoori.ootw.image.service; | ||
|
||
import static com.backendoori.ootw.common.image.exception.ImageException.IMAGE_ROLLBACK_FAIL_MESSAGE; | ||
import static com.backendoori.ootw.common.image.exception.ImageException.IMAGE_UPLOAD_FAIL_MESSAGE; | ||
import static com.backendoori.ootw.common.validation.ImageValidator.validateImage; | ||
import static com.backendoori.ootw.image.validation.ImageValidator.validateImage; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
import com.backendoori.ootw.common.image.exception.ImageException; | ||
import com.backendoori.ootw.image.domain.Image; | ||
import com.backendoori.ootw.image.dto.ImageFile; | ||
import com.backendoori.ootw.image.exception.ImageException; | ||
import com.backendoori.ootw.config.MiniOConfig; | ||
import com.backendoori.ootw.image.repository.ImageRepository; | ||
import io.minio.GetPresignedObjectUrlArgs; | ||
import io.minio.MinioClient; | ||
import io.minio.PutObjectArgs; | ||
import io.minio.RemoveObjectArgs; | ||
import io.minio.http.Method; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
|
@@ -29,28 +34,46 @@ public class MiniOImageServiceImpl implements ImageService { | |
|
||
private final MinioClient minioClient; | ||
private final MiniOConfig miniOConfig; | ||
private Path path; | ||
private final ImageRepository imageRepository; | ||
|
||
@Override | ||
public ImageFile upload(MultipartFile file) { | ||
validateImage(file); | ||
String randomFileName = getUniqueFileName(file); | ||
try { | ||
path = Path.of(file.getOriginalFilename()); | ||
InputStream inputStream = file.getInputStream(); | ||
String contentType = file.getContentType(); | ||
PutObjectArgs args = PutObjectArgs.builder() | ||
.bucket(miniOConfig.getBucket()) | ||
.object(path.toString()) | ||
.object(randomFileName) | ||
.stream(inputStream, inputStream.available(), -1) | ||
.contentType(contentType) | ||
.build(); | ||
minioClient.putObject(args); | ||
return new ImageFile(getUrl(), path.toString()); | ||
String url = getUrl(randomFileName); | ||
Image image = saveImage(url, randomFileName); | ||
imageRepository.save(image); | ||
return ImageFile.from(image); | ||
} catch (Exception e) { | ||
throw new ImageException(IMAGE_UPLOAD_FAIL_MESSAGE); | ||
throw new ImageException(ImageException.IMAGE_UPLOAD_FAIL_MESSAGE); | ||
} | ||
} | ||
|
||
private static Image saveImage(String url, String randomFileName) { | ||
return Image.builder() | ||
.ImageUrl(url) | ||
.fileName(randomFileName) | ||
.build(); | ||
} | ||
|
||
@NotNull | ||
private static String getUniqueFileName(MultipartFile file) { | ||
String randomUUID = UUID.randomUUID().toString(); | ||
String originalFileName = file.getOriginalFilename(); | ||
|
||
return randomUUID + originalFileName; | ||
} | ||
|
||
Comment on lines
+70
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π
Comment on lines
+69
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ν΄λΉ λΆλΆμ νλΌλ―Έν°λ‘ file μμ²΄κ° μλ file nameμ λ°λ‘ μ£Όλ κ²λ μ’μ κ² κ°μμ |
||
@Override | ||
public void delete(String fileName) { | ||
try { | ||
|
@@ -59,21 +82,21 @@ public void delete(String fileName) { | |
.object(fileName) | ||
.build()); | ||
} catch (Exception e) { | ||
throw new ImageException(IMAGE_ROLLBACK_FAIL_MESSAGE); | ||
throw new ImageException(ImageException.IMAGE_ROLLBACK_FAIL_MESSAGE); | ||
} | ||
} | ||
|
||
private String getUrl() { | ||
private String getUrl(String fileName) { | ||
try { | ||
return minioClient.getPresignedObjectUrl( | ||
GetPresignedObjectUrlArgs.builder() | ||
.method(Method.GET) | ||
.bucket(miniOConfig.getBucket()) | ||
.object(path.toString()) | ||
.object(fileName) | ||
.expiry(DURATION, TimeUnit.HOURS) | ||
.build()); | ||
} catch (Exception e) { | ||
throw new ImageException(IMAGE_UPLOAD_FAIL_MESSAGE); | ||
throw new ImageException(ImageException.IMAGE_UPLOAD_FAIL_MESSAGE); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.backendoori.ootw.image.validation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
@Target(value = {ElementType.PARAMETER, ElementType.FIELD}) | ||
@Retention(value = RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = EnumValidator.class) | ||
public @interface Enum { | ||
|
||
String message() default "μ ν¨νμ§ μμ κ°μ λλ€ λ€μ μ λ ₯ν΄μ£ΌμΈμ"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
Class<? extends java.lang.Enum<?>> enumClass(); | ||
Comment on lines
+15
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μΈν°νμ΄μ€μμλ λͺ¨λ νμ λν κ°νμ΄ νμν κ² κ°μμ |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.backendoori.ootw.image.validation; | ||
|
||
import java.util.Arrays; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class EnumValidator implements ConstraintValidator<Enum, String> { | ||
|
||
private Enum annotation; | ||
|
||
@Override | ||
public void initialize(Enum constraintAnnotation) { | ||
this.annotation = constraintAnnotation; | ||
} | ||
|
||
@Override | ||
public boolean isValid(String type, ConstraintValidatorContext context) { | ||
if (type == null) { | ||
return false; | ||
} | ||
|
||
return Arrays.stream(this.annotation.enumClass().getEnumConstants()) | ||
.anyMatch(e -> e.name().equals(type)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ μ»¬λΌ λͺ¨λ uniqueμΈ κ² κ°μλ° λ§μκΉμ?