package cn.exploring.engine.server.core.util;

import cn.exploring.engine.server.core.storage.StorageService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class FileUploadUtil {
    /**
     * 将文件上传到本地服务器
     * @param storageService
     * @param file
     * @return url
     * @throws IOException
     */
    public static String store(StorageService storageService, MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] tmp = originalFilename.split("\\.");
        String fileExtension = tmp[1];

        // 唯一路径名
        String pathName = storageService.generateUniquePath(originalFilename);
        // 路径+文件名
        String pathAndName = pathName + "/" + originalFilename;
        // 文件ContentType
        String contentType = getContentTypeByFileExtension(fileExtension);

        return storageService.localDbStore(file.getInputStream(), file.getSize(), contentType, originalFilename, pathAndName);
    }

    /**
     * 通过文件扩展名查询ContentType
     * @param fileExtension
     * @return
     */
    private static String getContentTypeByFileExtension(String fileExtension) {
        String contentType = null;

        switch (fileExtension) {
            case "apk":
                contentType = "application/vnd.android.package-archive";
                break;
            case "chm":
                contentType = "application/octet-stream";
                break;
            case "pdf":
                contentType = "application/pdf";
                break;
            case "txt":
                contentType = "application/txt";
                break;
            case "zip":
                contentType = "application/zip";
                break;
            case "doc":
                contentType = "application/msword";
                break;
            case "docx":
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                break;
            case "dotx":
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
                break;
            case "xls":
                contentType = "application/vnd.ms-excel";
                break;
            case "xlsx":
                contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
            case "ppt":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case "gif":
                contentType = "image/gif";
                break;
            case "png":
                contentType = "image/png";
                break;
            case "mpeg":
                contentType = "video/mpg";
                break;
            case "jpeg":
            case "jpg":
                contentType = "image/jpg";
                break;
            default:
                contentType = "application/force-download";
        }

        return contentType;
    }
}
