DsmFileStationClient.java 11.3 KB
Newer Older
敖文武 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
package clients;

import exeptions.DsmLoginException;
import requests.DsmAuth;
import requests.filestation.action.DsmCopyMoveRequest;
import requests.filestation.action.DsmCreateFolderRequest;
import requests.filestation.action.DsmDirSizeRequest;
import requests.filestation.action.DsmRenameRequest;
import requests.filestation.delete.DsmAdvancedDeleteRequest;
import requests.filestation.delete.DsmSimpleDeleteRequest;
import requests.filestation.favorite.DsmListFavoriteRequest;
import requests.filestation.favorite.DsmManageFavoriteRequest;
import requests.filestation.lists.DsmListFolderRequest;
import requests.filestation.lists.DsmSharedFolderRequest;
import requests.filestation.login.DsmLoginRequest;
import requests.filestation.login.DsmLogoutRequest;
import requests.filestation.search.DsmSearchResultRequest;
import requests.filestation.search.DsmSearchStartRequest;
import requests.filestation.search.DsmSearchStopRequest;
import requests.filestation.share.DsmShareCreateOrEditRequest;
import requests.filestation.share.DsmShareDeleteRequest;
import requests.filestation.share.DsmShareInfoRequest;
import requests.filestation.share.DsmShareListRequest;
import requests.filestation.transfert.DsmDownloadRequest;
import requests.filestation.transfert.DsmUploadRequest;
import responses.Response;
import responses.filestation.login.DsmLoginResponse;
import responses.filestation.DsmResponseFields;
import responses.filestation.DsmSimpleResponse;
import utils.DsmUtils;

import java.io.InputStream;
import java.util.Optional;

/**
 * this is the principal client of fileStation
 * all the functions are specified here
 */
public class DsmFileStationClient {

    private DsmAuth dsmAuth;

    public DsmFileStationClient(DsmAuth dsmAuth) {
        this.dsmAuth = dsmAuth;
    }

    public DsmAuth getDsmAuth() {
        return dsmAuth;
    }

    /**
     * login to the server
     * @param auth information about the server and user to connect
     * @return DsmFileStationClient
     */
    public static DsmFileStationClient login(DsmAuth auth) {
        Response<DsmLoginResponse> response = new DsmLoginRequest(Optional.ofNullable(auth).orElseThrow(() -> new DsmLoginException("DsmAuth can't be null"))).call();

        response = Optional.ofNullable(response).orElseThrow(() -> new DsmLoginException("An error occurred while trying to connect"));

        if(!response.isSuccess()) {
            throw new DsmLoginException(response.getError());
        }
        auth = auth.setSid(response.getData().getSid());

        return new DsmFileStationClient(auth);
    }

    /**
     * logout from server
     * @return boolean
     */
    public boolean logout() {
        Response<DsmSimpleResponse> response = new DsmLogoutRequest(
                Optional.ofNullable(this.dsmAuth).orElseThrow(() -> new DsmLoginException("You are already logged out"))
        )
        .call();

        this.dsmAuth = null;
        response = Optional.ofNullable(response).orElseThrow(() -> new DsmLoginException("The request generates no response"));

        if(!response.isSuccess()) {
            throw new DsmLoginException(response.getError());
        }

        return response.isSuccess();
    }

    public DsmSharedFolderRequest getAllSharedFolders() {
        return new DsmSharedFolderRequest(dsmAuth);
    }

    /**
     * get the list of the files and folder in a folder
     * @param folderPath the path to the folder
     * @return DsmListFolderRequest
     */
    public DsmListFolderRequest ls(String folderPath) {
        return new DsmListFolderRequest(dsmAuth)
                    .setFolderPath(folderPath);
    }

    /**
     * upload a file to the server
     * @param destinationPath the destination path
     * @param filePath the file to upload
     * @return DsmUploadRequest
     */
    public DsmUploadRequest upload(String destinationPath, String filePath) {
        return new DsmUploadRequest(dsmAuth)
                .setDestinationFolderPath(destinationPath)
                .setFilePath(filePath);
    }

    /**
     * upload a file to the server
     * @param destinationPath the destination path
     * @param filePath the file to upload
     * @param filePath the destination file name
     * @return DsmUploadRequest
     */
    public DsmUploadRequest upload(String destinationPath, String filePath, String destinationFileName) {
        return new DsmUploadRequest(dsmAuth)
                .setDestinationFolderPath(destinationPath)
                .setFilePath(filePath)
                .setDestinationFileName(destinationFileName);
    }

    /**
     * upload a file to the server
     * @param destinationPath the destination path
     * @param fileContent the file to upload
     * @param filePath the destination file name
     * @return DsmUploadRequest
     */
    public DsmUploadRequest upload(String destinationPath, InputStream fileContent, String destinationFileName) {
        return new DsmUploadRequest(dsmAuth)
                .setDestinationFolderPath(destinationPath)
                .setFileContent(fileContent)
                .setDestinationFileName(destinationFileName);
    }

    /**
     * download a file
     * @param fileOrFolderToDownload the path of the file to download
     * @param destinationPath the destination path
     * @return DsmDownloadRequest
     */
    public DsmDownloadRequest download(String fileOrFolderToDownload, String destinationPath) {
        return new DsmDownloadRequest(dsmAuth)
                .setFileToDownload(fileOrFolderToDownload)
                .setDestinationPath(destinationPath);
    }

    /**
     * download a file
     * @param destinationPath the destination path
     * @return DsmDownloadRequest
     */
    public DsmDownloadRequest download(String destinationPath) {
        return new DsmDownloadRequest(dsmAuth)
            .setDestinationPath(destinationPath);
    }

    /**
     *
     * @param filePath the path of the file to delete
     * @return DsmSimpleDeleteRequest
     */
    public DsmSimpleDeleteRequest simpleDelete(String filePath) {
        return new DsmSimpleDeleteRequest(dsmAuth)
                .addFileToDelete(filePath);
    }

    /**
     * delete folder of file asynchroniously
     * @return DsmAdvancedDeleteRequest
     */
    public DsmAdvancedDeleteRequest advancedDelete() {
        return new DsmAdvancedDeleteRequest(dsmAuth);
    }


    /**
     * check if the file or folder exits
     * @param filePath the path of the file or folder
     * @return boolean
     */
    public boolean exists(String filePath) {
       return this.ls(DsmUtils.extractRootFolderPath(filePath))
               .call()
               .getData()
               .getFiles()
               .stream()
               .anyMatch(f -> f.getName().equals(DsmUtils.extractFileName(filePath)));
    }

    /**
     * rename a file or folder
     * @param path the path of the folder or dile
     * @param newName the new name of the folder or file
     * @return DsmRenameRequest
     */
    public DsmRenameRequest rename(String path, String newName) {
        return new DsmRenameRequest(dsmAuth).addFileOrFolderToRename(path).addNewNames(newName);
    }

    /**
     * copy or move a file or folder
     * @param pathToCopy the path of the file or folder to move or copy
     * @param destination the destination where to copy or move the file
     * @return DsmCopyMoveRequest
     */
    public DsmCopyMoveRequest copyOrMove(String pathToCopy, String destination) {
        return new DsmCopyMoveRequest(dsmAuth)
                .addPathToCopy(pathToCopy)
                .setDestinationFolderPath(destination);
    }

    /**
     * create a new folder
     * @param parentPath the path of the parent folder
     * @param newFolderName the name of the new folder
     * @return DsmCreateFolderRequest
     */
    public DsmCreateFolderRequest createFolder(String parentPath, String newFolderName) {
        return new DsmCreateFolderRequest(dsmAuth)
                .addNewFolder(parentPath, newFolderName);
    }

    /**
     * get the information about the share links
     * @param id the id of the shared link
     * @return DsmResponseFields.SharingLink
     */
    public Response<DsmResponseFields.SharingLink> getShareLinkInfo(String id) {
        return new DsmShareInfoRequest(dsmAuth).getInfo(id);
    }

    /**
     * get all shared links
     * @return DsmShareListRequest
     */
    public DsmShareListRequest getAllShareLinks() {
        return new DsmShareListRequest(dsmAuth);
    }

    /**
     * delete a shared link
     * @param id the id of the share link to delete
     * @return DsmSimpleResponse
     */
    public Response<DsmSimpleResponse> deleteShareLink(String id) {
        return new DsmShareDeleteRequest(dsmAuth).delete(id);
    }

    /**
     * clear invalid links
     * @return DsmSimpleResponse
     */
    public Response<DsmSimpleResponse> clearInvalidShareLinks() {
        return new DsmShareDeleteRequest(dsmAuth).clearInvalidLinks();
    }

    /**
     * edit the shared link
     * @param id the id of the shared link to edit
     * @return DsmShareCreateOrEditRequest
     */
    public DsmShareCreateOrEditRequest editShareLink(String id) {
        return new DsmShareCreateOrEditRequest(dsmAuth)
                .setId(id);
    }

    /**
     * create a new shared link
     * @param fileOrFilePath the path of the file or folder to share
     * @return DsmShareCreateOrEditRequest
     */
    public DsmShareCreateOrEditRequest createShareLink(String fileOrFilePath) {
        return new DsmShareCreateOrEditRequest(dsmAuth)
                .addFileOrFolder(fileOrFilePath);
    }

    /**
     * Search for file or directory
     *
     * @param fileName the file or directoru to search
     * @return DsmSearchStartRequest
     */
    public DsmSearchStartRequest startSearch(String fileName) {
        return new DsmSearchStartRequest(dsmAuth)
                .addPattern(fileName);
    }

    /**
     * get the search result
     *
     * @param id the search task id
     * @return DsmSearchResultRequest
     */
    public DsmSearchResultRequest getSearchResult(String id) {
        return new DsmSearchResultRequest(dsmAuth)
                .setTaskId(id);
    }

    /**
     * stop the search task
     * @param id the it of the search task
     * @return DsmSearchStopRequest
     */
    public DsmSearchStopRequest stopSearch(String id) {
        return new DsmSearchStopRequest(dsmAuth).addTaskId(id);
    }

    /**
     * @param path the path of file or folder for favorite
     * @param name the favorite name
     * @return DsmManageFavoriteRequest
     */
    public DsmManageFavoriteRequest favoriteManager(String path, String name) {
        return new DsmManageFavoriteRequest(dsmAuth)
                .addPath(path, name);
    }

    /**
     * get favorite manager
     * @return DsmManageFavoriteRequest
     */
    public DsmManageFavoriteRequest favoriteManager() {
        return new DsmManageFavoriteRequest(dsmAuth);
    }

    /**
     * the list of all favorite
     * @return DsmListFavoriteRequest
     */
    public DsmListFavoriteRequest listFavorite() {
        return new DsmListFavoriteRequest(dsmAuth);
    }

    /**
     * get the size of directory or file
     * @param path the path the file or folder
     * @return DsmDirSizeRequest
     */
    public DsmDirSizeRequest getSize(String path) {
        return new DsmDirSizeRequest(dsmAuth)
                .addPath(path);
    }
}