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

com.github.eliux.mega.cmd.MegaCmdExport 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 com.github.eliux.mega.cmd;

import com.github.eliux.mega.MegaUtils;
import com.github.eliux.mega.error.MegaIOException;
import com.github.eliux.mega.error.MegaInvalidResponseException;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class MegaCmdExport extends AbstractMegaCmdCallerWithParams {

    private final Optional remotePath;

    private boolean exportDeleted;

    private boolean listOnly;

    public MegaCmdExport(String remotePath) {
        this.remotePath = Optional.of(remotePath);
    }

    @Override
    String cmdParams() {
        final StringBuilder cmdParamsBuilder = new StringBuilder();

        remotePath
                .filter(x -> !listOnly)
                .ifPresent(x -> cmdParamsBuilder
                        .append(exportDeleted ? "-d " : "-a ")
                        .append("-f ")
                );

        remotePath.ifPresent(cmdParamsBuilder::append);

        return cmdParamsBuilder.toString();
    }

    @Override
    public String getCmd() {
        return "export";
    }

    @Override
    public ExportInfo call() {
        try {
            return MegaUtils.execCmdWithOutput(executableCommand())
                    .stream().findFirst()
                    .map(ExportInfo::parseExportInfo)
                    .orElseThrow(() -> new MegaInvalidResponseException(
                            "Invalid response while exporting '%s'", remotePath
                    ));
        } catch (IOException e) {
            throw new MegaIOException("Error while exporting " + remotePath);
        }
    }

    public List list() {
        justList();
        try {
            return MegaUtils.execCmdWithOutput(executableCommand()).stream()
                    .map((ExportInfo::parseExportListInfo))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            throw new MegaIOException("Error while exporting " + remotePath);
        }
    }

    public MegaCmdExport enablePublicLink() {
        exportDeleted = false;
        return this;
    }

    public MegaCmdExport removePublicLink() {
        exportDeleted = true;
        return this;
    }

    public boolean isExportDeleted() {
        return exportDeleted;
    }

    protected MegaCmdExport justList() {
        listOnly = true;
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy