package com.dcsoft.file.utils;

import org.springframework.web.multipart.MultipartFile;

public class Base64ToMultipartFileUtil {

	public static MultipartFile convertToMultipartFile(String base64Data, String fileName) {
		return new Base64MultipartFile(base64Data, fileName);
	}

	/**
	 * 自动生成文件名
	 */
	public static MultipartFile convertToMultipartFile(String base64Data) {
		String fileName = generateFileName(base64Data);
		return convertToMultipartFile(base64Data, fileName);
	}

	private static String generateFileName(String base64Data) {
		String extension = ".jpg";
		if (base64Data.startsWith("data:image/png")) {
			extension = ".png";
		} else if (base64Data.startsWith("data:image/gif")) {
			extension = ".gif";
		}
		return "image_" + System.currentTimeMillis() + extension;
	}
}
