FileAttachmentUtil.java 4.8 KB
package com.pashanhoo.common.util.fileupload;

import polaris.fileattachment.AttachmentService;
import polaris.fileattachment.models.FileUrl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileAttachmentUtil {
    private static final String FILE_URL_PATTERN = "^[^\\\\/:*?\"<>|\r\n]+?:[^\\\\/:*?\"<>|\r\n]+(/[^\\\\/:*?\"<>|\r\n]+)*$";
    private static final Pattern DIRECTORY_NAME_PATTERN = Pattern.compile("([^/]*)[/]?$");
    private static final Pattern SERVER_REGEX = Pattern.compile("^[^\\\\/:*?\"<>|\r\n]+?:");
    private static final Pattern FILE_REGEX = Pattern.compile("[/:]([^\\\\/:*?\"<>|\r\n]+)$");

    @Resource
    private AttachmentService attachmentService;

    public FileAttachmentUtil() {
    }

    public static FileUrl parseFileUrl(String fileUrl) throws RuntimeException {
        int index1 = 0;
        int index2 = 0;
        if (fileUrl != null && fileUrl.matches(FILE_URL_PATTERN)) {
            Matcher regexMatcher = SERVER_REGEX.matcher(fileUrl);
            if (regexMatcher.find()) {
                index1 = regexMatcher.end();
            }
            regexMatcher = FILE_REGEX.matcher(fileUrl);
            if (regexMatcher.find()) {
                index2 = regexMatcher.start(1);
            }
            if (index1 != 0 && index2 != 0 && index1 <= index2) {
                return new FileUrl(fileUrl.substring(0, index1 - 1), fileUrl.substring(index1, index2), fileUrl.substring(index2));
            } else {
                throw new RuntimeException("附件url格式错误,fileUrl = " + fileUrl);
            }
        } else {
            throw new RuntimeException("附件url格式错误,fileUrl = " + (fileUrl == null ? "null" : fileUrl));
        }
    }

    public static String getServerNameFromFileUrl(String fileUrl) {
        if (fileUrl != null && fileUrl.matches(FILE_URL_PATTERN)) {
            Matcher regexMatcher = SERVER_REGEX.matcher(fileUrl);
            if (regexMatcher.find()) {
                int index = regexMatcher.end();
                return fileUrl.substring(0, index - 1);
            }
        }
        throw new RuntimeException("附件url格式错误,fileUrl = " + (fileUrl == null ? "null" : fileUrl));
    }

    public static String generateFileUrl(String serverName, String path, String fileId) throws RuntimeException {
        if (serverName != null && !serverName.isEmpty()) {
            if (path != null && !path.isEmpty()) {
                if (fileId != null && !fileId.isEmpty()) {
                    StringBuilder result = new StringBuilder();
                    result.append(serverName);
                    result.append(":");
                    result.append(path);
                    if (!path.endsWith("/")) {
                        result.append("/");
                    }
                    result.append(fileId);
                    return result.toString();
                } else {
                    throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件名称(ID)为null");
                }
            } else {
                throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件路径为null");
            }
        } else {
            throw new RuntimeException("创建附件服务器自定义格式的FileUrl地址时附件服务器名为null");
        }
    }

    public static boolean isFileUrl(String fileUrl) {
        return fileUrl.matches(FILE_URL_PATTERN);
    }

    public static String getParentPath(String path) {
        Matcher m = DIRECTORY_NAME_PATTERN.matcher(path);
        m.find();
        return m.replaceAll("");
    }

    public static String getPathName(String path) {
        Matcher m = DIRECTORY_NAME_PATTERN.matcher(path);
        m.find();
        return m.group(1);
    }

    public static String getSavePath(String bsm) {
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String path = filterString(bsm);
        return date + "/" + path;
    }

    public static String filterString(String str) {
        StringBuffer s = new StringBuffer(str.length());
        for (int i = 0; i < str.length(); i++) {
            if (!hasFullChar(String.valueOf(str.charAt(i)))) {
                s.append(str.charAt(i));
            }
        }
        return s.toString();
    }

    public static boolean hasFullChar(String str) {
        return str.getBytes().length != str.length();
    }

    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}