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

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

Go to download

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

The newest version!
package io.github.eliux.mega.cmd;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

public class MegaCmdShare extends AbstractMegaCmdRunnerWithParams {

    private final Optional remotePath;

    private final Optional username;

    private boolean shared = true;

    private AccessLevel accessLevel = AccessLevel.READ_ONLY;

    protected MegaCmdShare() {
        remotePath = Optional.empty();
        username = Optional.empty();
    }

    public MegaCmdShare(String remotePath, String username) {
        this.remotePath = Optional.of(remotePath);
        this.username = Optional.of(username);
    }

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

    @Override
    List cmdParams() {
        final List cmdParamsBuilder = new LinkedList<>();

        cmdParamsBuilder.add(shared ? "-a" : "-d");

        username.ifPresent(x -> cmdParamsBuilder
                .add(String.format("--with=%s", x)));

        if (shared) {
            cmdParamsBuilder.add(String.format("--level=%s", accessLevel));
        }

        remotePath.ifPresent(cmdParamsBuilder::add);

        return cmdParamsBuilder;
    }

    public boolean isShared() {
        return shared;
    }

    public MegaCmdShare startSharing() {
        shared = true;
        return this;
    }

    public MegaCmdShare stopSharing() {
        shared = true;
        return this;
    }

    public MegaCmdShare grantReadOnlyAccess() {
        accessLevel = AccessLevel.READ_ONLY;
        return this;
    }

    public MegaCmdShare grantReadAndWriteAccess() {
        accessLevel = AccessLevel.READ_WRITE;
        return this;
    }

    public MegaCmdShare grantFullAccess() {
        accessLevel = AccessLevel.FULL;
        return this;
    }

    public MegaCmdShare grantOwnerAccess() {
        accessLevel = AccessLevel.OWNER;
        return this;
    }

    protected enum AccessLevel {

        READ_ONLY('0'), READ_WRITE('1'), FULL('2'), OWNER('3');

        private Character id;

        AccessLevel(char id) {
            this.id = id;
        }

        public char getId() {
            return id;
        }

        @Override
        public String toString() {
            return id.toString();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy