io.github.eliux.mega.cmd.ExportInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of megacmd4j Show documentation
Show all versions of megacmd4j Show documentation
Java client library that works on top of MEGAcmd to provide access to the services of Mega.nz
package io.github.eliux.mega.cmd;
import io.github.eliux.mega.MegaUtils;
import io.github.eliux.mega.error.MegaInvalidResponseException;
import java.time.LocalDate;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Handles the information of an exported resource in MEGA
*/
public class ExportInfo {
private static final Pattern LIST_PATTERN =
Pattern.compile("(?\\S+) \\(.+link: (?http[s]?://mega.nz/\\S*#.+)\\)");
private static final Pattern EXPORT_PATTERN =
Pattern.compile("\\s(?\\S+)(:)\\s(?http[s]?://mega.nz/\\S*#\\S*)( expires at (?.+))?");
private final String remotePath;
private final String publicLink;
private Optional expireDate;
public ExportInfo(String remotePath, String publicLink) {
this.remotePath = remotePath;
this.publicLink = publicLink;
}
public static ExportInfo parseExportInfo(String exportInfoStr) {
final Matcher matcher = EXPORT_PATTERN.matcher(exportInfoStr);
if (matcher.find()) {
String remotePath = matcher.group("remotePath");
String publicLink = matcher.group("publicLink");
final ExportInfo result = new ExportInfo(remotePath, publicLink);
Optional.ofNullable(matcher.group("expireDate"))
.map(MegaUtils::parseBasicISODate)
.ifPresent(result::setExpireDate);
return result;
}
throw new MegaInvalidResponseException("Unexpected export info format : "+exportInfoStr);
}
public static ExportInfo parseExportListInfo(String exportInfoLine) {
try {
final Matcher matcher = LIST_PATTERN.matcher(exportInfoLine);
if (matcher.find()) {
return new ExportInfo(
matcher.group("remotePath"),
matcher.group("publicLink")
);
}
} catch (IllegalStateException | IndexOutOfBoundsException ex) {
//Don't let it go outside
}
throw new MegaInvalidResponseException(exportInfoLine);
}
public String getRemotePath() {
return remotePath;
}
public String getPublicLink() {
return publicLink;
}
public Optional getExpireDate() {
return expireDate;
}
public ExportInfo setExpireDate(LocalDate expireDate) {
this.expireDate = Optional.of(expireDate);
return this;
}
}