/** * Copyright (c) 2018 人人开源 All rights reserved. *
* https://www.renren.io *
 * 版权所有,侵权必究!
 */
package com.zt.modules.oss.cloud;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.spire.xls.Workbook;
import com.zt.common.exception.ErrorCode;
import com.zt.common.exception.RenException;
import com.zt.core.oss.encry.IOssEncryptService;
import com.zt.modules.oss.enums.CloudChannel;
import com.zt.modules.oss.model.QdSysOss;
import com.zt.modules.oss.model.SysOss;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
 * 本地上传
 *
 * @author hehz
 */
public class LocalStorageService extends AbstractStorageService {
    private IOssEncryptService ossEncryptService;
    public LocalStorageService(StorageConfig config, QdStorageConfig qdConfig, IOssEncryptService ossEncryptService) {
        this.config = config;
        this.qdConfig = qdConfig;
        this.ossEncryptService = ossEncryptService;
    }
    @Override
    public SysOss upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }
    @Override
    public SysOss upload(InputStream inputStream, String path) {
        File file = new File(config.getLocalPath() + File.separator + path);
        try {
            FileUtils.copyToFile(ossEncryptService.encrypt(inputStream), file);
        } catch (IOException e) {
            throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR.getCode(), e, "");
        }
        SysOss oss = new SysOss();
        oss.setId(IdWorker.getId());
        oss.setPath(path);
        oss.setUrl(config.getLocalDomain() + "/sys/oss/content");
        return oss;
    }
    @Override
    public SysOss uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(config.getLocalPrefix(), suffix));
    }
    @Override
    public SysOss uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(config.getLocalPrefix(), suffix));
    }
    @Override
    public void delete(SysOss oss) {
        if (oss != null) {
            File file = new File(config.getLocalPath() + File.separator + oss.getPath());
            if (file.exists()) {
                File dir = file.getParentFile();
                try {
                    if (dir.listFiles().length == 1) {
                        FileUtils.forceDelete(dir);
                    } else {
                        FileUtils.forceDelete(file);
                    }
                } catch (IOException e) {
                    throw new RenException("本地文件删除失败!");
                }
            }
        }
    }
    public void content(SysOss oss, HttpServletResponse response) throws Exception {
        if (oss == null) {
            throw new IOException("文件不存在!");
        }
        if (CloudChannel.LOCAL.getValue().equals(oss.getChannel())) { // 本地
            String filename = oss.getName();
            if (",gif,jpg,jpeg,png,bmp,".contains("," + oss.getType() + ",")) {// 浏览图片
                response.setContentType("image/jpeg");
            } else {
                response.setContentType("application/octet-stream");
//                response.setHeader("Content-disposition",
//                        "attachment;filename=" + new String(filename.getBytes("ISO8859-1"), "UTF-8"));
                response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            }
            File file = new File(config.getLocalPath() + File.separator + oss.getPath());
            System.out.println(config.getLocalPath());
            System.out.println(File.separator);
            System.out.println(oss.getPath());
            // 创建文件输入流
            InputStream is = ossEncryptService.decrypt(file);
            // 响应输出流
            ServletOutputStream out = response.getOutputStream();
            // 创建缓冲区
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            is.close();
            out.flush();
            out.close();
        }
    }
    public ResponseEntity