Search in sources :

Example 1 with Exceptions

use of com.orion.utils.Exceptions in project orion-ops by lijiahangmax.

the class FileDownloadServiceImpl method execDownload.

@Override
public void execDownload(String token, HttpServletResponse response) throws IOException {
    // 获取token信息
    FileDownloadDTO downloadFile = this.getPathByDownloadToken(token);
    if (downloadFile == null) {
        throw Exceptions.notFound();
    }
    InputStream inputStream = null;
    DirectDownloader downloader = null;
    try {
        // 获取类型
        if (FileDownloadType.of(downloadFile.getType()).isLocal()) {
            // 本地文件
            File file = Optional.of(downloadFile).map(FileDownloadDTO::getFilePath).filter(Strings::isNotBlank).map(File::new).filter(Files1::isFile).orElseThrow(Exceptions::notFound);
            inputStream = Files1.openInputStreamFastSafe(file);
        } else {
            // 远程文件
            downloader = new DirectDownloader(downloadFile.getMachineId());
            inputStream = downloader.open().getFile(downloadFile.getFilePath());
        }
        // 返回
        Servlets.transfer(response, inputStream, Strings.bytes(downloadFile.getFileName(), Const.UTF_8));
    } finally {
        Streams.close(inputStream);
        Streams.close(downloader);
    }
}
Also used : InputStream(java.io.InputStream) FileDownloadDTO(com.orion.ops.entity.dto.FileDownloadDTO) Exceptions(com.orion.utils.Exceptions) DirectDownloader(com.orion.ops.handler.sftp.direct.DirectDownloader) File(java.io.File)

Aggregations

FileDownloadDTO (com.orion.ops.entity.dto.FileDownloadDTO)1 DirectDownloader (com.orion.ops.handler.sftp.direct.DirectDownloader)1 Exceptions (com.orion.utils.Exceptions)1 File (java.io.File)1 InputStream (java.io.InputStream)1