package com.seer.teach.teacher.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class Base64DecodedMultipartFile implements MultipartFile {

    private final byte[] fileContent;

    public Base64DecodedMultipartFile(byte[] fileContent) {
        this.fileContent = fileContent;
    }

    @Override
    public String getName() {
        return System.currentTimeMillis() + "";
    }

    @Override
    public String getOriginalFilename() {
        return this.getName() + ".jpg";
    }

    @Override
    public String getContentType() {
        return "image/jpg";
    }

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

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

    @Override
    public byte[] getBytes() {
        return fileContent;
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(fileContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(fileContent);
    }
}
