package com.trade.service.thirdparty.xiaotu.utils;

import com.trade.common.utils.LoggerUtil;
import org.slf4j.Logger;
import org.springframework.mock.web.MockMultipartFile;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @description:
 * @Author: wanglang
 * @Date: 2024/9/23
 * @Version: 1.0
 */
public class XiaoTuUtil {

    private static final Logger log = LoggerUtil.logger(LoggerUtil.LogFileNameEnum.SERVICES_LOG);

    public static void writeFile(String filePath, String content) {
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, length);
            }
            fos.flush();
        } catch (Exception e) {
            log.error("文件写入失败path={},异常={}", filePath, e);
        }
    }

    public static MockMultipartFile getMockMultipartFile(String fileName, String content) throws IOException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
        return new MockMultipartFile(fileName, inputStream);
    }

    public static MultipartFileExtend getMultipartFileExtend(String fileName, String originalFilename, String content)
            throws IOException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
        MultipartFileExtend multipartFileExtend = new MultipartFileExtend(fileName, inputStream);
        multipartFileExtend.setOriginalFilename(originalFilename);
        return multipartFileExtend;
    }
}
