package com.ruoyi.web.controller.common;

import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 内存中的 MultipartFile 实现
 */
public class InMemoryMultipartFile implements MultipartFile {
    
    private final String name;
    private final String originalFilename;
    private final String contentType;
    private final byte[] bytes;
    
    public InMemoryMultipartFile(String name, String originalFilename, 
                                String contentType, byte[] bytes) {
        this.name = name;
        this.originalFilename = originalFilename;
        this.contentType = contentType;
        this.bytes = bytes != null ? bytes : new byte[0];
    }
    
    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public String getOriginalFilename() {
        return originalFilename;
    }
    
    @Override
    public String getContentType() {
        return contentType;
    }
    
    @Override
    public boolean isEmpty() {
        return bytes.length == 0;
    }
    
    @Override
    public long getSize() {
        return bytes.length;
    }
    
    @Override
    public byte[] getBytes() throws IOException {
        return bytes;
    }
    
    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(bytes);
    }
    
    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        try (FileOutputStream fos = new FileOutputStream(dest)) {
            fos.write(bytes);
        }
    }
    
    @Override
    public void transferTo(Path dest) throws IOException, IllegalStateException {
        Files.write(dest, bytes);
    }
}