package com.microservices.dms.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * 自定义MultipartFile实现（通用版，无外部依赖）
 */
public class CustomMultipartFile implements MultipartFile {
    private final byte[] content;
    private final String filename;
    private final String contentType;

    public CustomMultipartFile(byte[] content, String filename, String contentType) {
        this.content = content;
        this.filename = filename;
        this.contentType = contentType;
    }

    @Override
    public String getName() {
        return "file";
    }

    @Override
    public String getOriginalFilename() {
        return filename;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return content == null || content.length == 0;
    }

    @Override
    public long getSize() {
        return content.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(content);
    }

    @Override
    public void transferTo(java.io.File dest) throws IOException, IllegalStateException {
        try (java.io.OutputStream os = new java.io.FileOutputStream(dest)) {
            os.write(content);
        }
    }
}