DefaultFileProxyFactory.java 3.3 KB
Newer Older
S
smallchill 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**
 * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.core.toolbox.file;

import org.springblade.common.vo.ShiroUser;
import org.springblade.core.constant.Cst;
import org.springblade.core.plugins.dao.Blade;
import org.springblade.core.shiro.ShiroKit;
import org.springblade.core.toolbox.Func;
import org.springblade.core.toolbox.kit.DateKit;
import org.springblade.core.toolbox.kit.ImageKit;
import org.springblade.system.model.Attach;

27 28 29 30 31 32 33 34 35
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * 文件代理工厂
 * @author zhuangqian
 */
S
smallchill 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
public class DefaultFileProxyFactory implements IFileProxy {

	@Override
	public File rename(File f, String path) {
		File dest = new File(path);
		f.renameTo(dest);
		return dest;
	}

	@Override
	public String [] path(File f, String dir) {
		//避免网络延迟导致时间不同步
		long time = System.currentTimeMillis();
		
		StringBuilder uploadPath = new StringBuilder()
		.append(getFileDir(dir, Cst.me().getUploadRealPath()))
		.append(time)
		.append(getFileExt(f.getName()));
		
		StringBuilder virtualPath = new StringBuilder()
		.append(getFileDir(dir, Cst.me().getUploadCtxPath()))
		.append(time)
		.append(getFileExt(f.getName()));
		
		return new String [] {BladeFileKit.formatUrl(uploadPath.toString()), BladeFileKit.formatUrl(virtualPath.toString())};
	}
	
	@Override
	public Object getFileId(BladeFile bf) {
		Attach attach = new Attach();
		ShiroUser user = ShiroKit.getUser();
		Object creater = (null == user) ? 0 : user.getId();
		attach.setCreater(Func.toInt(creater));
		attach.setCreatetime(new Date());
		attach.setName(bf.getOriginalFileName());
		attach.setStatus(1);
		attach.setUrl(bf.getUploadVirtualPath());
		return Blade.create(Attach.class).saveRtStrId(attach);
	}
	
	/**
	 * 获取文件后缀
	 */
	public static String getFileExt(String fileName) {
80 81 82 83 84
		if (fileName.indexOf(".") == -1) {
            return ".jpg";
        } else {
            return fileName.substring(fileName.lastIndexOf('.'), fileName.length());
        }
S
smallchill 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	}

	/**
	 * 获取文件保存地址
	 * @param saveDir
	 * @return
	 */
	public static String getFileDir(String dir, String saveDir) {
		StringBuilder newFileDir = new StringBuilder();
		newFileDir.append(saveDir)
				.append(File.separator).append(dir).append(File.separator).append(DateKit.getDays())
				.append(File.separator);
		return newFileDir.toString();
	}


	/**
	 * 图片压缩
	 * @param path 文件地址
	 * @return
	 */
106 107
	@Override
    public void compress(String path) {
S
smallchill 已提交
108 109 110 111 112 113 114 115
		try {
			ImageKit.zoomScale(ImageKit.readImage(path), new FileOutputStream(new File(path)), null, Cst.me().getCompressScale().doubleValue(), Cst.me().isCompressFlag());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

}