DsmDownloadRequest.java 4.57 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
package requests.filestation.transfert;

import com.fasterxml.jackson.core.type.TypeReference;
import exeptions.DsmDownloadException;
import java.io.OutputStream;
import requests.DsmAbstractRequest;
import requests.DsmAuth;
import requests.filestation.DsmRequestParameters;
import requests.filestation.DsmRequestParameters.Mode;
import responses.filestation.transfert.DsmDownloadResponse;
import responses.Response;
import responses.filestation.transfert.DsmDownloadStreamResponse;
import utils.DsmUtils;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

/**
 * Download files/folders. If only one file is specified, the file content is responded. If more than one
 * file/folder is given, binary content in ZIP format which they are compressed to is responded.
 */
public class DsmDownloadRequest extends DsmAbstractRequest<DsmDownloadStreamResponse> {

    /**
     * One or more file/folder paths starting with a
     * shared folder to be downloaded, separated
     * by a commas. When more than one file
     * is to be downloaded, files/folders will be
     * compressed as a zip file.
     */
    private String filePath;
    /**
     * Mode used to download files/folders, value
     * could be:
     * (1) open: try to trigger the application,
     * such as a web browser, to open it.
     * Content-Type of the HTTP header of
     * the response is set to MIME type
     * according to file extension.
     * (2) download: try to trigger the application,
     * such as a web browser, to download it.
     * Content-Type of the HTTP header of
     * response is set to application/octetstream and Content-Disposition of the
     * HTTP header of the response is set to
     * attachment.
     */
    private Mode mode = Mode.OPEN;

    /**
     * the destination where to save the downloaded file
     */
    private String destinationPath;
    public DsmDownloadRequest(DsmAuth auth) {
        super(auth);
        this.apiName = "SYNO.FileStation.Download";
        this.version = 1;
        this.method = "download";
        this.path = "webapi/entry.cgi";
    }

    @Override
    protected TypeReference getClassForMapper() {
        return new TypeReference<Response<DsmDownloadResponse>>() {};
    }

    public DsmDownloadRequest setFileToDownload(String filePath) {
        this.filePath = filePath;
        return this;
    }


    public DsmDownloadRequest setMode(Mode mode) {
        this.mode = mode;
        return this;
    }

    public DsmDownloadRequest setDestinationPath(String destinationPath) {
        this.destinationPath = destinationPath;
        return this;
    }
//
//    @Override
//    public Response<DsmDownloadResponse> call() {
//
//        addParameter("path", escape(Optional.ofNullable(this.filePath).orElseThrow(() -> new DsmDownloadException("You have to add a folder or file to download"))));
//        addParameter("mode", this.mode.name());
//
//        try {
//            HttpURLConnection conn = handleRequest(build());
//            File downloadedFile = DsmUtils.downloadFile(conn.getInputStream(), Optional.ofNullable(this.destinationPath).orElseThrow(() -> new DsmDownloadException("You have to set a destination path"))+"/"+DsmUtils.extractFileName(this.filePath));
//
//            Response<DsmDownloadResponse> response = new Response<>();
//            response.setSuccess(true);
//            response.setData(new DsmDownloadResponse(downloadedFile));
//            return response;
//        } catch (IOException e) {
//            throw new DsmDownloadException("the file does not exist on the server");
//        }
//    }

    @Override
    public Response<DsmDownloadStreamResponse> call() {

        addParameter("path", escape(Optional.ofNullable(this.filePath).orElseThrow(() -> new DsmDownloadException("You have to add a folder or file to download"))));
        addParameter("mode", this.mode.name());
        try {
            HttpURLConnection conn = handleRequest(build());
            //File downloadedFile = DsmUtils.downloadFile(conn.getInputStream(), Optional.ofNullable(this.destinationPath).orElseThrow(() -> new DsmDownloadException("You have to set a destination path"))+"/"+DsmUtils.extractFileName(this.filePath));

            Response<DsmDownloadStreamResponse> response = new Response<>();
            response.setSuccess(true);
            response.setData(new DsmDownloadStreamResponse(conn.getInputStream(), conn.getContentType(), conn.getContentLength()));
            return response;
        } catch (IOException e) {
            throw new DsmDownloadException("the file does not exist on the server");
        }
    }
}