LbryUri parsing improvements. Remove duplicate code.

This commit is contained in:
Akinwale Ariwodola 2020-11-15 09:11:50 +01:00
parent 896c566a02
commit d0f5504c80

View file

@ -10,7 +10,7 @@ import lombok.Data;
@Data @Data
public class LbryUri { public class LbryUri {
public static final String LBRY_TV_BASE_URL = "https://lbry.tv"; public static final String LBRY_TV_BASE_URL = "https://lbry.tv/";
public static final String PROTO_DEFAULT = "lbry://"; public static final String PROTO_DEFAULT = "lbry://";
public static final String REGEX_INVALID_URI = "[ =&#:$@%?;/\\\\\"<>%\\{\\}|^~\\[\\]`\u0000-\u0008\u000b-\u000c\u000e-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]"; public static final String REGEX_INVALID_URI = "[ =&#:$@%?;/\\\\\"<>%\\{\\}|^~\\[\\]`\u0000-\u0008\u000b-\u000c\u000e-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]";
public static final String REGEX_ADDRESS = "^(b)(?=[^0OIl]{32,33})[0-9A-Za-z]{32,33}$"; public static final String REGEX_ADDRESS = "^(b)(?=[^0OIl]{32,33})[0-9A-Za-z]{32,33}$";
@ -183,7 +183,7 @@ public class LbryUri {
return uri; return uri;
} }
public String build(boolean includeProto, String protoDefault, boolean vanity) { public String build(boolean includeProto, String protocol, boolean vanity) {
String formattedChannelName = null; String formattedChannelName = null;
if (channelName != null) { if (channelName != null) {
formattedChannelName = channelName.startsWith("@") ? channelName : String.format("@%s", channelName); formattedChannelName = channelName.startsWith("@") ? channelName : String.format("@%s", channelName);
@ -206,7 +206,7 @@ public class LbryUri {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (includeProto) { if (includeProto) {
sb.append(protoDefault); sb.append(protocol);
} }
sb.append(primaryClaimName); sb.append(primaryClaimName);
if (vanity) { if (vanity) {
@ -224,79 +224,34 @@ public class LbryUri {
if (!Helper.isNullOrEmpty(primaryClaimId)) { if (!Helper.isNullOrEmpty(primaryClaimId)) {
sb.append('#').append(primaryClaimId); sb.append('#').append(primaryClaimId);
} } else if (primaryClaimSequence > 0) {
if (primaryClaimSequence > 0) {
sb.append(':').append(primaryClaimSequence); sb.append(':').append(primaryClaimSequence);
} } else if (primaryBidPosition > 0) {
if (primaryBidPosition > 0) {
sb.append('$').append(primaryBidPosition); sb.append('$').append(primaryBidPosition);
} }
if (!Helper.isNullOrEmpty(secondaryClaimName)) { if (!Helper.isNullOrEmpty(secondaryClaimName)) {
sb.append('/').append(secondaryClaimName); sb.append('/').append(secondaryClaimName);
} }
if (!Helper.isNullOrEmpty(secondaryClaimId)) { if (!Helper.isNullOrEmpty(secondaryClaimId)) {
sb.append('#').append(secondaryClaimId); sb.append('#').append(secondaryClaimId);
} } else if (secondaryClaimSequence > 0) {
if (secondaryClaimSequence > 0) {
sb.append(':').append(secondaryClaimSequence); sb.append(':').append(secondaryClaimSequence);
} } else if (secondaryBidPosition > 0) {
if (secondaryBidPosition > 0) {
sb.append('$').append(secondaryBidPosition); sb.append('$').append(secondaryBidPosition);
} }
return sb.toString(); return sb.toString();
} }
public String toTvString() {
String formattedChannelName = null;
if (channelName != null) {
formattedChannelName = channelName.startsWith("@") ? channelName : String.format("@%s", channelName);
}
String primaryClaimName = claimName;
if (Helper.isNullOrEmpty(primaryClaimName)) {
primaryClaimName = contentName;
}
if (Helper.isNullOrEmpty(primaryClaimName)) {
primaryClaimName = formattedChannelName;
}
if (Helper.isNullOrEmpty(primaryClaimName)) {
primaryClaimName = streamName;
}
String primaryClaimId = claimId;
if (Helper.isNullOrEmpty(primaryClaimId)) {
primaryClaimId = !Helper.isNullOrEmpty(formattedChannelName) ? channelClaimId : streamClaimId;
}
StringBuilder sb = new StringBuilder();
sb.append(LBRY_TV_BASE_URL).append('/');
sb.append(primaryClaimName);
String secondaryClaimName = null;
if (Helper.isNullOrEmpty(claimName) && !Helper.isNullOrEmpty(contentName)) {
secondaryClaimName = contentName;
}
if (Helper.isNullOrEmpty(secondaryClaimName)) {
secondaryClaimName = !Helper.isNullOrEmpty(formattedChannelName) ? streamName : null;
}
String secondaryClaimId = !Helper.isNullOrEmpty(secondaryClaimName) ? streamClaimId : null;
if (!Helper.isNullOrEmpty(primaryClaimId)) {
sb.append(':').append(primaryClaimId);
}
if (!Helper.isNullOrEmpty(secondaryClaimName)) {
sb.append('/').append(secondaryClaimName);
}
if (!Helper.isNullOrEmpty(secondaryClaimId)) {
sb.append(':').append(secondaryClaimId);
}
return sb.toString();
}
public static String normalize(String url) throws LbryUriException { public static String normalize(String url) throws LbryUriException {
return parse(url).toString(); return parse(url).toString();
} }
public String toTvString() {
return build(true, LBRY_TV_BASE_URL, false);
}
public String toVanityString() { public String toVanityString() {
return build(true, PROTO_DEFAULT, true); return build(true, PROTO_DEFAULT, true);
} }