package com.qmth.boot.core.fss.utils;

import com.qmth.boot.core.fss.config.FileStoreProperty;
import com.qmth.boot.core.fss.config.FssSecretProvider;
import com.qmth.boot.core.fss.store.FileStore;
import com.qmth.boot.core.fss.store.impl.DiskStore;
import com.qmth.boot.core.fss.store.impl.OssStore;
import org.apache.commons.lang3.StringUtils;

public class FileStoreBuilder {

    public static FileStore buildFileStore(String name, FileStoreProperty fileStoreProperty,
            FssSecretProvider fssSecretProvider) {
        String server = StringUtils.trimToEmpty(fileStoreProperty.getServer());
        if (!server.endsWith(FssUtils.FILE_PATH_SEPARATOR)) {
            server = server.concat(FssUtils.FILE_PATH_SEPARATOR);
        }
        FssSigner fssSigner = null;
        if (server.equals(FssUtils.INNER_ENDPOINT_PREFIX.concat(FssUtils.FILE_PATH_SEPARATOR))) {
            fssSigner = new FssSigner(name, fssSecretProvider.getSecret());
        }
        String config = fileStoreProperty.getConfig();
        if (config.startsWith("oss://")) {
            return new OssStore(new OssConfig(fileStoreProperty));
        } else {
            return new DiskStore(server, config, System.getProperty("java.io.tmpdir"), fssSigner);
        }
    }
}
