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

com.github.javaclub.ossclient.impl.LocalOssClient Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
package com.github.javaclub.ossclient.impl;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.multipart.MultipartFile;

import com.github.javaclub.BizException;
import com.github.javaclub.Constants.MemCapacity;
import com.github.javaclub.ossclient.OssClient;
import com.github.javaclub.ossclient.OssConstants;
import com.github.javaclub.ossclient.OssConstants.LocalStorage;
import com.github.javaclub.ossclient.util.AssetsUtils;
import com.github.javaclub.toolbox.ToolBox.Files;
import com.github.javaclub.toolbox.ToolBox.IO;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.ToolBox.UUID;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
import com.github.javaclub.toolbox.utils.ThreadLocalDateFormatter;

public class LocalOssClient implements OssClient, InitializingBean {
    
    private String webContext;
    private String fileDirectory;
	
	private String cdnDomain;
	private String cdnProtocol;
	private boolean mappingDirect; // 是否直接域名映射到本地目录的(无需通过应用预览)
	
	private String defaultBucket; 
	
	private int fileUploadLimitMbSize; // 限制上限为 {xx} MB
	private boolean doOriginReferCheck;
	private boolean fileArchivedByDate;

	@Override
	public void afterPropertiesSet() throws Exception {
		
		webContext = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.WEB_CONTEXT);
		fileDirectory = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.FILE_DIRECTORY);
		
		cdnDomain = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.CDN_DOMAIN);
		cdnProtocol = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.CDN_PROTOCOL);
		defaultBucket = CompositeAppConfigProperties.getInstance().getValue(LocalStorage.DEFAULT_BUCKET);
		mappingDirect = CompositeAppConfigProperties.getInstance().boolValue(LocalStorage.MAPPING_DIRECT);
		
		fileUploadLimitMbSize = CompositeAppConfigProperties.getInstance().intValue(OssConstants.FILE_UPLOAD_LIMIT_SIZE, 5);
		doOriginReferCheck = CompositeAppConfigProperties.getInstance().boolValue(OssConstants.SAME_ORIGIN_CHECK, true);
		fileArchivedByDate = CompositeAppConfigProperties.getInstance().boolValue(OssConstants.FILE_ARCHIVED_BY_DATE, true);
	}

	@Override
	public String upload(MultipartFile file, String relativePathInBucket, Map params) {
		String bucketVar = (String) params.getOrDefault(OssConstants.BUCKET_NAME_PARAM_KEY, Strings.EMPTY);
		String thisUploadBucket = Strings.isBlank(bucketVar) ? defaultBucket : bucketVar;
		
		String objectPath = Strings.endsWithIgnoreCase(relativePathInBucket, "/") ? relativePathInBucket 
				: Strings.concat(relativePathInBucket, "/");
		if (fileArchivedByDate) {
			objectPath = Strings.concat(objectPath, ThreadLocalDateFormatter.numericDateFormat(new Date()) , "/");
		}
		String originalFilename = file.getOriginalFilename();
		String suffix = originalFilename.substring(originalFilename.lastIndexOf('.')).toLowerCase();
		AssetsUtils.checkFileExtention(suffix, "local", params);
		
		if (0 < fileUploadLimitMbSize && file.getSize() > MemCapacity.longBytesOfMB(fileUploadLimitMbSize)) { // 文件大小限制
			throw new BizException("文件大小超出最大上传限制!");
		}
		
		String filename = UUID.randomUUID() + suffix;
		String baseDir = Strings.endsWithIgnoreCase(fileDirectory, "/") ? fileDirectory 
							: Strings.concat(fileDirectory, "/");
		String targetDir = Strings.concat(baseDir, thisUploadBucket, "/", objectPath);
		String targetFile = Strings.concat(targetDir, filename);
		String fileObject = Strings.concat(thisUploadBucket, "/", objectPath, filename);
		
		if (doOriginReferCheck) {
			// TODO
		}
		
		// 开始写入本地文件
		try {
			Files.ensureDirectory(targetDir);
			IO.copy(file.getInputStream(), new FileOutputStream(targetFile));
			
			String protocol = Strings.endsWithIgnoreCase(cdnProtocol, "://") ? cdnProtocol : Strings.concat(cdnProtocol, "://");
			String ctxPath = Strings.endsWithIgnoreCase(webContext, "/") ? webContext : Strings.concat(webContext, "/");
			
			String finalFilePath = mappingDirect ? fileObject : Strings.replace(fileObject, "/", "!");
			// 拼装得到资源URL
			String url = Strings.concat(protocol, cdnDomain, ctxPath, finalFilePath);
			return url;
		} catch (BizException e) {
			throw e;
		} catch (Exception e) {
			throw new BizException("LocalOssClient 文件写入异常!", e);
		}
	}

	@Override
	public String upload(byte[] bytes, String relativeFilenameInBucket, Map params) {
		String bucketVar = (String) params.getOrDefault(OssConstants.BUCKET_NAME_PARAM_KEY, Strings.EMPTY);
		String thisUploadBucket = Strings.isBlank(bucketVar) ? defaultBucket : bucketVar;
		
		String relativeFile = relativeFilenameInBucket;
		if (fileArchivedByDate) {
			String archivedPrefix = Strings.concat(ThreadLocalDateFormatter.numericDateFormat(new Date()) , "/");
			relativeFile = Strings.concat(archivedPrefix, relativeFilenameInBucket);
		}
		
		if (0 < fileUploadLimitMbSize && bytes.length > MemCapacity.longBytesOfMB(fileUploadLimitMbSize)) { // 文件大小限制
			throw new BizException("文件大小超出最大上传限制!");
		}
		
		String baseDir = Strings.endsWithIgnoreCase(fileDirectory, "/") ? fileDirectory 
							: Strings.concat(fileDirectory, "/");
		String targetFile = Strings.concat(baseDir, thisUploadBucket, "/", relativeFile);
		String fileObject = Strings.concat(thisUploadBucket, "/", relativeFile);
		
		if (doOriginReferCheck) {
			// TODO
		}
		
		// 开始写入本地文件
		try {
			Files.ensureFile(targetFile);
			IO.copy(new ByteArrayInputStream(bytes), new FileOutputStream(targetFile));
			
			String protocol = Strings.endsWithIgnoreCase(cdnProtocol, "://") ? cdnProtocol : Strings.concat(cdnProtocol, "://");
			String ctxPath = Strings.endsWithIgnoreCase(webContext, "/") ? webContext : Strings.concat(webContext, "/");
			// 上传图片得到URL
			String url = Strings.concat(protocol, cdnDomain, ctxPath, fileObject);
			return url;
		} catch (BizException e) {
			throw e;
		} catch (Exception e) {
			throw new BizException("MinioClient 上传异常!", e);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy