package com.ruoyi.common.utils.file;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author : fangk
 * @date : 2024/7/5 13:12
 * @desc : TODO
 */
public class MediaConvertUtils {


    // public ByteArrayOutputStream convertToPdf(MultipartFile file) throws IOException {
    //     String fileName = file.getOriginalFilename();
    //     if (fileName == null) {
    //         throw new IllegalArgumentException("File name cannot be null");
    //     }
    //
    //     if (fileName.endsWith(".docx")) {
    //         return convertDocxToPdf(file.getInputStream());
    //     } else if (fileName.endsWith(".doc")) {
    //         return convertDocToPdf(file.getInputStream());
    //     } else {
    //         throw new IllegalArgumentException("Unsupported file format");
    //     }
    // }
    //
    // public ByteArrayOutputStream convertFileToPdf(String filePath) throws IOException {
    //     Path path = Paths.get(filePath);
    //     if (!Files.exists(path)) {
    //         throw new IllegalArgumentException("File does not exist");
    //     }
    //
    //     try (InputStream inputStream = new FileInputStream(path.toFile())) {
    //         String fileName = path.getFileName().toString();
    //         if (fileName.endsWith(".docx")) {
    //             return convertDocxToPdf(inputStream);
    //         } else if (fileName.endsWith(".doc")) {
    //             return convertDocToPdf(inputStream);
    //         } else {
    //             throw new IllegalArgumentException("Unsupported file format");
    //         }
    //     }
    // }

    // private ByteArrayOutputStream convertDocxToPdf(InputStream inputStream) throws IOException {
    //     try (XWPFDocument document = new XWPFDocument(inputStream)) {
    //         ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
    //         PdfOptions options = PdfOptions.create();
    //         PdfConverter.getInstance().convert(document, pdfOutputStream, options);
    //         return pdfOutputStream;
    //     }
    // }
    //
    // private ByteArrayOutputStream convertDocToPdf(InputStream inputStream) throws IOException {
    //     try (HWPFDocument doc = new HWPFDocument(inputStream)) {
    //         XWPFDocument docx = new XWPFDocument();
    //         // 简单转换，实际使用中可能需要更复杂的逻辑
    //         docx.createParagraph().insertNewRun(0).setText(doc.getDocumentText());
    //         ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream();
    //         docx.write(docxOutputStream);
    //         return convertDocxToPdf(new ByteArrayInputStream(docxOutputStream.toByteArray()));
    //     }
    // }
}
