package nckd.base.helper;

import kd.bos.fileservice.FileServiceFactory;
import kd.bos.fileservice.extension.FileServiceExt;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.service.attachment.FilePathService;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * 自定义附件服务
 * 处理附件加密解密
 *
 * @author turborao
 * @date 2025/05/26 14:07
 */
public class CusFileServiceExt extends FilePathService {

    private static final Log logger = LogFactory.getLog(CusFileServiceExt.class);

    @Override
    public InputStream encode(String originalPath, InputStream in) {
        logger.info("--------------SEC附件解密 "+originalPath+"----------------");

//        InputStream inForSEC = FileSECUtils.processFileWithSEC(originalPath, in);
//        if (inForSEC != null) {
//            return inForSEC;
//        }
        return in;
    }

    /**
     * 附件解密
     * 先解决附件解密，后面再解决附件加密问题
     * @param originalPath
     * @param in
     * @return
     */
    @Override
    public InputStream decode(String originalPath, InputStream in) {
        logger.info("--------------SEC附件解密 "+originalPath+"----------------");

//        InputStream inForSEC = FileSECUtils.processFileWithSEC(originalPath, in);
//        if (inForSEC != null) {
//            return inForSEC;
//        }
        return in;
    }

    @Override
    public InputStream beforeWriteToResponse(String originalPath, InputStream in, String userAgent) {
        logger.info("--------------beforeWriteToResponse测试 "+originalPath+"----------------" + userAgent);

        if("preview".equals(userAgent)){
            logger.info("--------------beforeWriteToResponse 解密 ");

            in = FileSECUtils.processFileWithSEC(originalPath, in);
        }

        return super.beforeWriteToResponse(originalPath, in, userAgent);
    }

    @Override
    public Map<String, Object> beforeWrite(String originalPath, InputStream in, String userAgent, Map<String, Object> params) {
        return super.beforeWrite(originalPath, in, userAgent, params);
    }
}
