package com.ningdatech.carapi.log.manage;

import java.io.File;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import cn.hutool.core.io.FileUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author CMM
 * @since 2023/01/06 16:40
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class LogBackupManage {

    public ResponseEntity<byte[]> downloadFile() {
        // 指定.tar.gz文件的路径
        File directory = new File("");
        // 临时文件存储路径
        String filePath = directory.getAbsolutePath() + File.separator + "bash/logs.tar.gz";
        File file = new File(filePath);
        byte[] fileBytes = FileUtil.readBytes(file);


        // 设置响应头信息
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=logs.tar.gz");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileBytes.length));

        // 返回ResponseEntity对象
        return ResponseEntity.ok()
                .headers(headers)
                .body(fileBytes);
    }
}
