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

com.fastchar.oss.ali.FastAliOSSFile Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package com.fastchar.oss.ali;

import com.aliyun.oss.model.ObjectMetadata;
import com.fastchar.annotation.AFastPriority;
import com.fastchar.core.FastChar;
import com.fastchar.core.FastFile;
import com.fastchar.oss.interfaces.IFastOSSListener;
import com.fastchar.utils.FastFileUtils;
import com.fastchar.utils.FastStringUtils;

import java.io.File;


@AFastPriority
public class FastAliOSSFile extends FastFile {
    private String configOnlyCode;

    public String getConfigOnlyCode() {
        return configOnlyCode;
    }

    public FastAliOSSFile setConfigOnlyCode(String configOnlyCode) {
        this.configOnlyCode = configOnlyCode;
        return this;
    }

    public FastAliOSSFile moveToOSS() throws Exception {
        FastAliOSSConfig config = FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class);
        FastAliOSSBlock defaultBlock = config.getDefaultBlock();
        return moveToOSS(defaultBlock.getBlockName());
    }

    public FastAliOSSFile moveToOSS(ObjectMetadata metadata) throws Exception {
        FastAliOSSConfig config = FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class);
        FastAliOSSBlock defaultBlock = config.getDefaultBlock();
        return moveToOSS(defaultBlock.getBlockName(), metadata);
    }

    public FastAliOSSFile moveToOSS(String blockName) throws Exception {
        ObjectMetadata metadata = new ObjectMetadata();
        String uploadFileName = getUploadFileName();
        if (FastStringUtils.isEmpty(uploadFileName)) {
            uploadFileName = getFileName();
        }
        metadata.setContentDisposition("attachment;filename=\"" + uploadFileName + "\"");
        metadata.setContentEncoding("utf-8");
        return moveToOSS(blockName, metadata);
    }



    public FastAliOSSFile moveToOSS(String blockName, ObjectMetadata metadata) throws Exception {
        File file = getFile();
        IFastOSSListener iFastOSSListener = FastChar.getOverrides().newInstance(false, IFastOSSListener.class);
        if (iFastOSSListener != null) {
            if (!iFastOSSListener.onMoveToOSS(this)) {
                return this;
            }
        }
        FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class).getBlock(blockName)
                .uploadFile(getKey(), file.getAbsolutePath(), metadata);
        try {
            FastFileUtils.forceDelete(file);
        } catch (Exception ignored) {}
        return this;
    }

    @Override
    public boolean exists() {
        FastAliOSSConfig config = FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class);
        FastAliOSSBlock defaultBlock = config.getDefaultBlock();
        return exists(defaultBlock.getBlockName());
    }

    public boolean exists(String blockName) {
        return FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class).getBlock(blockName)
                .existFile(getKey());
    }

    @Override
    public String getUrl() throws Exception {
        FastAliOSSConfig config = FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class);
        FastAliOSSBlock defaultBlock = config.getDefaultBlock();
        if (exists()) {
            return getUrl(defaultBlock.getBlockName());
        }
        moveToOSS(defaultBlock.getBlockName());
        return getUrl(defaultBlock.getBlockName());
    }

    public String getUrl(String blockName) {
        return FastChar.getConfig(getConfigOnlyCode(),FastAliOSSConfig.class).getBlock(blockName).getFileUrl(getKey());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy