package com.tofly.common.controller;

import com.tofly.common.core.util.FtpUtil;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.core.util.io.IOUtil;
import com.tofly.common.service.CommonFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author tuonbed
 * @date 2020/4/1  13:16
 */
@RestController
@RequestMapping("/commonFile")
@Api(tags="文件相关公共接口")
@AllArgsConstructor
@Slf4j
public class CommonFileController {

    private final CommonFileService commonFileService;

    @ApiOperation(value = "导出数据")
    @GetMapping("/download")
    @SneakyThrows
    public void getexcelout(String fileName,HttpServletRequest request, HttpServletResponse response){
        byte[] data = commonFileService.getFileByte(fileName);
        response.reset();
        response.setCharacterEncoding("UTF-8");
        String userAgent = request.getHeader("User-Agent");
        String formFileName;
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            // 针对IE或者以IE为内核的浏览器：
            formFileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            formFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setHeader("Content-Disposition", String.format("attachment; filename=%s",formFileName));
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("content-type", "application/octet-stream");
        try{
            IOUtil.write(response.getOutputStream(), Boolean.TRUE, data);
        }catch (IOException e){
            log.error(e.getMessage());
            e.getMessage();
        }
    }

    /**
     * 返回图片
     * @param response
     * @return
     */
    @GetMapping(value = "/getStream")
    @ApiOperation(value = "返回二进制流")
    public byte[] loadFileImg(String fileName,HttpServletResponse response){
        String fileType=fileName.substring(fileName.lastIndexOf("."));
        if (StringUtil.isNotEmpty(fileType)) {
            fileType = fileType.toLowerCase();
        }
        switch (fileType) {
            case ".jpg":
            case ".jpeg":
                response.setContentType(MediaType.IMAGE_JPEG_VALUE);
                break;
            case ".png":
                response.setContentType(MediaType.IMAGE_PNG_VALUE);
                break;
            case ".gif":
                response.setContentType(MediaType.IMAGE_GIF_VALUE);
                break;
            default:
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
                break;
        }
        byte[] data = commonFileService.getFileByte(fileName);
        return data;
    }
}
