UploadUtil.java 10.6 KB
package com.xiniunet.web.util;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.xiniunet.foundation.contract.UploadTypeEnum;
import com.xiniunet.foundation.request.FileUploadRequest;
import com.xiniunet.foundation.response.FileUploadResponse;
import com.xiniunet.foundation.service.FoundationService;
import com.xiniunet.framework.security.Passport;
import com.xiniunet.framework.util.SettingUtil;
import com.xiniunet.framework.util.excel.Excel;
import com.xiniunet.framework.util.excel.OldExcel;
import com.xiniunet.framework.util.excel.datatable.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created on 2016-12-12.
 *
 * @author 小昊
 */
public class UploadUtil<T> {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    private FoundationService foundationService;

    public UploadUtil(FoundationService foundationService) {
        this.foundationService = foundationService;
    }

    public String upload(DataTable<T> table, Long tenantId, Long id, Passport passport) {
        return this.upload(table, tenantId, id, UploadUtil.FileType.XLSX, passport);
    }

    public String upload(DataTable<T> table, Long tenantId, Long id, UploadUtil.FileType fileType, Passport passport) {

        byte[] bytes;
        if(fileType == UploadUtil.FileType.XLSX) {
            try {
                bytes = (new Excel(false, new DataTable[]{table})).getBytes();
            } catch (Exception var19) {
                this.logger.error(var19.getMessage(), var19);
                return null;
            }
        } else {
            try {
                bytes = (new OldExcel(false, new DataTable[]{table})).getBytes();
            } catch (Exception var18) {
                this.logger.error(var18.getMessage(), var18);
                return null;
            }
        }

        FileUploadRequest uploadRequest = new FileUploadRequest();
        uploadRequest.setFileExt(fileType.toString().toLowerCase());
        uploadRequest.setId(id);
        uploadRequest.setType(UploadTypeEnum.TMP);
        uploadRequest.setFileStream(bytes);

        passport.setTenantId(tenantId);

        FileUploadResponse uploadResponse = foundationService.uploadFile(uploadRequest, passport);
        return uploadResponse.getUrl();
    }

    public String upload(DataTable<T> table, Long tenantId, String moduleName, UploadUtil.FileType fileType, Passport passport) {
        String bucketName = "";
        String urlPath = "";
        String AccessKeyId = "";
        String AccessKeySecret = "";
        Properties prop = new Properties();
        InputStream in = Object.class.getResourceAsStream("/setting.properties");
        if(in == null) {
            in = SettingUtil.getCaller(3).getResourceAsStream("/setting.properties");
        }

        try {
            prop.load(in);
            bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
            urlPath = prop.getProperty("aliyun.oss.url").trim();
            AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
            AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
        } catch (IOException var19) {
            this.logger.error(var19.getMessage(), var19);
        }

        OSSClient client;
        if(!"internal".equals(prop.getProperty("aliyun.oss.network"))) {
            client = new OSSClient(AccessKeyId, AccessKeySecret);
        } else {
            client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
        }

        String fileName = moduleName + (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
        String outputFilePath;
        byte[] bytes;
        if(fileType == UploadUtil.FileType.XLSX) {
            outputFilePath = tenantId + "/tmp/" + fileName + ".xlsx";

            try {
                bytes = (new Excel(false, new DataTable[]{table})).getBytes();
            } catch (Exception var18) {
                this.logger.error(var18.getMessage(), var18);
                return null;
            }
        } else {
            outputFilePath = tenantId + "/tmp/" + fileName + ".xls";

            try {
                bytes = (new OldExcel(false, new DataTable[]{table})).getBytes();
            } catch (Exception var17) {
                this.logger.error(var17.getMessage(), var17);
                return null;
            }
        }

        ByteArrayInputStream content = new ByteArrayInputStream(bytes);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength((long)bytes.length);
        client.putObject(bucketName, outputFilePath, content, meta);
        return urlPath + outputFilePath;
    }

    public String upload(List<T> beanList, Long tenantId, Long id, Passport passport) {
        try {
            DataTable e = new DataTable(beanList);
            return this.upload(e, tenantId, id, UploadUtil.FileType.XLSX, passport);
        } catch (Exception var5) {
            this.logger.error(var5.getMessage(), var5);
            return null;
        }
    }

    public String upload(List<T> beanList, Long tenantId, String moduleName, Passport passport) {
        try {
            DataTable e = new DataTable(beanList);
            return this.upload(e, tenantId, moduleName, UploadUtil.FileType.XLSX, passport);
        } catch (Exception var5) {
            this.logger.error(var5.getMessage(), var5);
            return null;
        }
    }

    public String upload(List<T> beanList, Long tenantId, Long id, UploadUtil.FileType type, Passport passport) {
        try {
            DataTable e = new DataTable(beanList);
            return this.upload(e, tenantId, id, type, passport);
        } catch (Exception var6) {
            this.logger.error(var6.getMessage(), var6);
            return null;
        }
    }

    public String uploadWeb(List<T> beanList, Long tenantId, Long id, Passport passport) {
        String bucketName = "";
        String urlPath = "";
        String AccessKeyId = "";
        String AccessKeySecret = "";
        Properties prop = new Properties();
        InputStream in = getCaller(4).getResourceAsStream("/setting.properties");

        try {
            prop.load(in);
            bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
            urlPath = prop.getProperty("aliyun.oss.url").trim();
            AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
            AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
        } catch (IOException var18) {
            this.logger.error(var18.getMessage(), var18);
        }

        OSSClient client;
        if(!"production".equals(prop.getProperty("deploy.mode"))) {
            client = new OSSClient(AccessKeyId, AccessKeySecret);
        } else {
            client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
        }

        String outputFilePath = tenantId + "/tmp/" + id + ".xlsx";
        String fileUrl = urlPath + outputFilePath;

        byte[] bytes;
        try {
            DataTable table = new DataTable(beanList);
            bytes = (new Excel(false, new DataTable[]{table})).getBytes();
        } catch (Exception var17) {
            this.logger.error(var17.getMessage(), var17);
            return null;
        }

        ByteArrayInputStream content = new ByteArrayInputStream(bytes);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength((long)bytes.length);
        client.putObject(bucketName, outputFilePath, content, meta);
        return fileUrl;
    }

    public String upload(Excel excel, Long tenantId, Long id, Passport passport) {
        return this.upload(excel, tenantId, id, UploadUtil.FileType.XLSX, passport);
    }

    public String upload(Excel excel, Long tenantId, Long id, UploadUtil.FileType fileType, Passport passport) {
        String bucketName = "";
        String urlPath = "";
        String AccessKeyId = "";
        String AccessKeySecret = "";
        Properties prop = new Properties();
        InputStream in = Object.class.getResourceAsStream("/setting.properties");
        if(in == null) {
            in = SettingUtil.getCaller(3).getResourceAsStream("/setting.properties");
        }

        try {
            prop.load(in);
            bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
            urlPath = prop.getProperty("aliyun.oss.url").trim();
            AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
            AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
        } catch (IOException var20) {
            this.logger.error(var20.getMessage(), var20);
        }

        OSSClient client;
        if(!"production".equals(prop.getProperty("deploy.mode"))) {
            client = new OSSClient(AccessKeyId, AccessKeySecret);
        } else {
            client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
        }

        String outputFilePath;
        byte[] bytes;
        if(fileType == UploadUtil.FileType.XLSX) {
            outputFilePath = tenantId + "/tmp/" + id + ".xlsx";

            try {
                bytes = excel.getBytes();
            } catch (Exception var19) {
                this.logger.error(var19.getMessage(), var19);
                return null;
            }
        } else {
            outputFilePath = tenantId + "/tmp/" + id + ".xls";

            try {
                bytes = excel.getBytes();
            } catch (Exception var18) {
                this.logger.error(var18.getMessage(), var18);
                return null;
            }
        }

        ByteArrayInputStream content = new ByteArrayInputStream(bytes);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength((long)bytes.length);
        client.putObject(bucketName, outputFilePath, content, meta);
        String fileUrl = urlPath + outputFilePath;
        return fileUrl;
    }

    public static Class getCaller(int level) {
        StackTraceElement[] stack = Thread.currentThread().getStackTrace();

        Class clazz;
        try {
            String ex = stack[level].getClassName();
            clazz = Class.forName(ex);
        } catch (Exception var4) {
            var4.printStackTrace();
            clazz = Object.class;
        }

        return clazz;
    }

    public static enum FileType {
        XLS,
        XLSX;

        private FileType() {
        }
    }
}