All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.eliux.mega.cmd.ExportInfo Maven / Gradle / Ivy

Go to download

Java client library that works on top of MEGAcmd to provide access to the services of Mega.nz

There is a newer version: 1.6.2
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy