All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.crazywalker.fsf.oss.OSSFileHandler Maven / Gradle / Ivy

The newest version!
package cn.crazywalker.fsf.oss;

import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

/**
 * 上传对象存储的文件处理抽象类
 * @author CrazyWalker
 * @since 周日, 02/26 2023 12:21 GMT+8
 */
public abstract class OSSFileHandler {

    private static final String EMPTY_STRING = "";
    private static final Integer ZERO = 0;
    private static final Integer NOT_OCCUR = -1;
    private static final Character DOT = '.';

    public abstract String getModuleName();
    public abstract String filePath(Object info, MultipartFile multipartFile);

    /**
     * todo @author CrazyWalker @date 20230923 待进一步优化实现,而不是实现空方法
     * @param info
     * @param fileBytes
     * @return
     */
    public String filePath(Object info, byte[] fileBytes) {
        return EMPTY_STRING;
    }

    protected static String multipartFileTypeSuffix(MultipartFile file) {
        String typeName = multipartFileType(file);
        return EMPTY_STRING.equals(typeName) ? typeName : DOT + typeName;
    }

    private static String multipartFileType(MultipartFile file) {
        Optional filenameOptional = Optional.ofNullable(file.getOriginalFilename());
        int dot = filenameOptional.map(s -> s.lastIndexOf(DOT)).orElse(NOT_OCCUR);
        int length = filenameOptional.map(String::length).orElse(ZERO);
        String type = EMPTY_STRING;
        boolean hasTypeName = !NOT_OCCUR.equals(dot);
        if (!hasTypeName) {
            return type;
        }
        return file.getOriginalFilename().substring(dot + 1, length);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy