package com.jeeplus.modules.isignature.workthread;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.utils.Office2PdfUtil;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class ImageConvertThread implements Runnable{

    private static Logger logger = LoggerFactory.getLogger(ImageConvertThread.class);
    private ByteArrayInputStream imageInputStream;
    private String pathString;
    private String newFileName;
    private OSSClientUtil ossClientUtil;

    public ImageConvertThread(ByteArrayInputStream imageInputStream, String pathString, String newFileName, OSSClientUtil ossClientUtil) {
        this.imageInputStream = imageInputStream;
        this.pathString = pathString;
        this.newFileName = newFileName;
        this.ossClientUtil = ossClientUtil;
    }

    @Override
    public void run() {
        logger.info("开始异步保存预览图片信息，文件名：{}",newFileName);
        String imageName = newFileName;
        if (FilenameUtils.isExtension(newFileName,new String[]{"PDF","pdf"})) {
            ByteArrayOutputStream imageOutputStream = Office2PdfUtil.pdfToImage(imageInputStream, newFileName);
            imageName = Office2PdfUtil.convertPdfFileName(newFileName);
            imageInputStream = new ByteArrayInputStream(imageOutputStream.toByteArray());
        }
        ossClientUtil.uploadFile2OSS(imageInputStream, pathString,imageName);
    }
}
