Search in sources :

Example 1 with FileDownloadDTO

use of com.orion.ops.entity.dto.FileDownloadDTO in project orion-ops by lijiahangmax.

the class FileDownloadServiceImpl method getDownloadToken.

@Override
public String getDownloadToken(Long id, FileDownloadType type) {
    String path = null;
    String name = null;
    Long machineId = Const.HOST_MACHINE_ID;
    // 获取日志绝对路径
    switch(type) {
        case SECRET_KEY:
            // 秘钥
            path = this.getDownloadSecretKeyFilePath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case TERMINAL_LOG:
            // terminal 日志
            path = this.getDownloadTerminalLogFilePath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case EXEC_LOG:
            // 执行日志
            path = commandExecService.getExecLogFilePath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case SFTP_DOWNLOAD:
            // sftp 下载文件
            FileTransferLogDO transferLog = sftpService.getDownloadFilePath(id);
            if (transferLog != null) {
                path = transferLog.getLocalFile();
                name = Files1.getFileName(transferLog.getRemoteFile());
            }
            break;
        case TAIL_LIST_FILE:
            // tail 列表文件
            FileTailListDO tailFile = fileTailListDAO.selectById(id);
            if (tailFile != null) {
                path = tailFile.getFilePath();
                name = Files1.getFileName(path);
                machineId = tailFile.getMachineId();
            }
            break;
        case APP_BUILD_LOG:
            // 应用构建日志
            path = applicationBuildService.getBuildLogPath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case APP_BUILD_ACTION_LOG:
        case APP_RELEASE_ACTION_LOG:
            // 应用构建操作日志
            path = applicationActionLogService.getActionLogPath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case APP_BUILD_BUNDLE:
            // 应用构建 产物文件
            path = applicationBuildService.getBuildBundlePath(id);
            // 如果是文件夹则获取压缩文件
            if (path != null && Files1.isDirectory(path)) {
                path += "." + Const.SUFFIX_ZIP;
            }
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case APP_RELEASE_MACHINE_LOG:
            // 应用发布机器日志
            path = applicationReleaseMachineService.getReleaseMachineLogPath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        case SCHEDULER_TASK_MACHINE_LOG:
            // 调度任务机器日志
            path = schedulerTaskMachineRecordService.getTaskMachineLogPath(id);
            name = Optional.ofNullable(path).map(Files1::getFileName).orElse(null);
            break;
        default:
            break;
    }
    // 检查文件是否存在
    if (path == null || (type.isLocal() && !Files1.isFile(path))) {
        throw Exceptions.httpWrapper(HttpWrapper.of(ResultCode.FILE_MISSING));
    }
    // 设置缓存
    FileDownloadDTO download = new FileDownloadDTO();
    download.setFilePath(path);
    download.setFileName(Strings.def(name, Const.UNKNOWN));
    download.setUserId(Currents.getUserId());
    download.setType(type.getType());
    download.setMachineId(machineId);
    String token = UUIds.random19();
    String key = Strings.format(KeyConst.FILE_DOWNLOAD_TOKEN, token);
    redisTemplate.opsForValue().set(key, JSON.toJSONString(download), KeyConst.FILE_DOWNLOAD_EXPIRE, TimeUnit.SECONDS);
    return token;
}
Also used : FileTailListDO(com.orion.ops.entity.domain.FileTailListDO) FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) Files1(com.orion.utils.io.Files1) FileDownloadDTO(com.orion.ops.entity.dto.FileDownloadDTO)

Example 2 with FileDownloadDTO

use of com.orion.ops.entity.dto.FileDownloadDTO in project orion-ops by lijiahangmax.

the class FileDownloadServiceImpl method getPathByDownloadToken.

@Override
public FileDownloadDTO getPathByDownloadToken(String token) {
    if (Strings.isBlank(token)) {
        return null;
    }
    String key = Strings.format(KeyConst.FILE_DOWNLOAD_TOKEN, token);
    String value = redisTemplate.opsForValue().get(key);
    if (Strings.isBlank(value)) {
        return null;
    }
    FileDownloadDTO download = JSON.parseObject(value, FileDownloadDTO.class);
    if (download == null) {
        return null;
    }
    redisTemplate.delete(key);
    return download;
}
Also used : FileDownloadDTO(com.orion.ops.entity.dto.FileDownloadDTO)

Example 3 with FileDownloadDTO

use of com.orion.ops.entity.dto.FileDownloadDTO 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)3 FileTailListDO (com.orion.ops.entity.domain.FileTailListDO)1 FileTransferLogDO (com.orion.ops.entity.domain.FileTransferLogDO)1 DirectDownloader (com.orion.ops.handler.sftp.direct.DirectDownloader)1 Exceptions (com.orion.utils.Exceptions)1 Files1 (com.orion.utils.io.Files1)1 File (java.io.File)1 InputStream (java.io.InputStream)1