-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: s3 object-key regex 상수로 분리
- Loading branch information
1 parent
d305827
commit 445a54a
Showing
1 changed file
with
59 additions
and
51 deletions.
There are no files selected for viewing
110 changes: 59 additions & 51 deletions
110
pennyway-infra/src/main/java/kr/co/pennyway/infra/client/aws/s3/ObjectKeyType.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 |
---|---|---|
@@ -1,59 +1,67 @@ | ||
package kr.co.pennyway.infra.client.aws.s3; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public enum ObjectKeyType { | ||
PROFILE("1", "PROFILE", "delete/profile/{userId}/{uuid}_{timestamp}.{ext}", "profile/{userId}/origin/{uuid}_{timestamp}.{ext}"), | ||
FEED("2", "FEED", "delete/feed/{feed_id}/{uuid}_{timestamp}.{ext}", "feed/{feed_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHATROOM_PROFILE("3", "CHATROOM_PROFILE", "delete/chatroom/{chatroom_id}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHAT("4", "CHAT", "delete/chatroom/{chatroom_id}/chat/{chat_id}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/chat/{chat_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHAT_PROFILE("5", "CHAT_PROFILE", "delete/chatroom/{chatroom_id}/chat_profile/{userId}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/chat_profile/{userId}/origin/{uuid}_{timestamp}.{ext}"); | ||
|
||
private final String code; | ||
private final String type; | ||
private final String deleteTemplate; | ||
private final String originTemplate; | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getDeleteTemplate() { | ||
return deleteTemplate; | ||
} | ||
|
||
public String convertDeleteKeyToOriginKey(String deleteKey) { | ||
String regex = deleteTemplate | ||
.replace("{userId}", "([^/]+)") | ||
.replace("{uuid}", "([^_]+)") | ||
.replace("{timestamp}", "([^\\.]+)") | ||
.replace("{ext}", "([^/]+)") | ||
.replace("{feed_id}", "([^/]+)") | ||
.replace("{chatroom_id}", "([^/]+)") | ||
.replace("{chat_id}", "([^/]+)"); | ||
|
||
Pattern pattern = Pattern.compile(regex); | ||
Matcher matcher = pattern.matcher(deleteKey); | ||
|
||
if (matcher.matches()) { | ||
String originKey = originTemplate; | ||
for (int i = 1; i <= matcher.groupCount(); i++) { | ||
originKey = originKey.replaceFirst("\\{[^}]+\\}", matcher.group(i)); | ||
} | ||
return originKey; | ||
} | ||
|
||
throw new IllegalArgumentException("No matching ObjectKeyType for deleteKey: " + deleteKey); | ||
} | ||
PROFILE("1", "PROFILE", "delete/profile/{userId}/{uuid}_{timestamp}.{ext}", "profile/{userId}/origin/{uuid}_{timestamp}.{ext}"), | ||
FEED("2", "FEED", "delete/feed/{feed_id}/{uuid}_{timestamp}.{ext}", "feed/{feed_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHATROOM_PROFILE("3", "CHATROOM_PROFILE", "delete/chatroom/{chatroom_id}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHAT("4", "CHAT", "delete/chatroom/{chatroom_id}/chat/{chat_id}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/chat/{chat_id}/origin/{uuid}_{timestamp}.{ext}"), | ||
CHAT_PROFILE("5", "CHAT_PROFILE", "delete/chatroom/{chatroom_id}/chat_profile/{userId}/{uuid}_{timestamp}.{ext}", | ||
"chatroom/{chatroom_id}/chat_profile/{userId}/origin/{uuid}_{timestamp}.{ext}"); | ||
|
||
private static final String userIdPattern = "([^/]+)"; | ||
private static final String uuidPattern = "([^_]+)"; | ||
private static final String timestampPattern = "([^\\.]+)"; | ||
private static final String extPattern = "([^/]+)"; | ||
private static final String feedIdPattern = "([^/]+)"; | ||
private static final String chatroomIdPattern = "([^/]+)"; | ||
private static final String chatIdPattern = "([^/]+)"; | ||
|
||
private final String code; | ||
private final String type; | ||
private final String deleteTemplate; | ||
private final String originTemplate; | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getDeleteTemplate() { | ||
return deleteTemplate; | ||
} | ||
|
||
public String convertDeleteKeyToOriginKey(String deleteKey) { | ||
String regex = deleteTemplate | ||
.replace("{userId}", userIdPattern) | ||
.replace("{uuid}", uuidPattern) | ||
.replace("{timestamp}", timestampPattern) | ||
.replace("{ext}", extPattern) | ||
.replace("{feed_id}", feedIdPattern) | ||
.replace("{chatroom_id}", chatroomIdPattern) | ||
.replace("{chat_id}", chatIdPattern); | ||
|
||
Pattern pattern = Pattern.compile(regex); | ||
Matcher matcher = pattern.matcher(deleteKey); | ||
|
||
if (matcher.matches()) { | ||
String originKey = originTemplate; | ||
for (int i = 1; i <= matcher.groupCount(); i++) { | ||
originKey = originKey.replaceFirst("\\{[^}]+\\}", matcher.group(i)); | ||
} | ||
return originKey; | ||
} | ||
|
||
throw new IllegalArgumentException("No matching ObjectKeyType for deleteKey: " + deleteKey); | ||
} | ||
} |