package com.ym.mec.thirdparty.storage;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ym.mec.thirdparty.exception.ThirdpartyException;

@Component
public class StoragePluginContext {

	private static final Map<String, StoragePlugin> mapper = new HashMap<String, StoragePlugin>();
	
	public static void addStoragePlugin(StoragePlugin storagePlugin) {
		if (mapper.containsKey(storagePlugin.getName())) {
			throw new ThirdpartyException("存储插件:{}已存在", storagePlugin.getName());
		}
		mapper.put(storagePlugin.getName(), storagePlugin);
	}
	
	public String uploadFile(String storagePluginName, String folderName, File file){
		StoragePlugin StoragePlugin = getStoragePlugin(storagePluginName);
		return StoragePlugin.uploadFile(folderName, file);
	}

	private StoragePlugin getStoragePlugin(String storagePluginName) {
		StoragePlugin storagePlugin = mapper.get(storagePluginName);

		if (storagePlugin == null) {
			throw new ThirdpartyException("存储插件:{}不存在", storagePluginName);
		}

		return storagePlugin;
	}

}
