package com.winsun.controller;

import com.winsun.auth.core.annotion.Permission;
import com.winsun.constant.FilePath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;

/**
 * @Author xuede
 * @Date 2020/3/6 16:44
 * @Version 1.0
 */
@Slf4j
@RestController
public class ImgController {

    //menuname = "图片路径"  只支持jpeg 和 png
    @Permission(menuname = "图片查询", value =  "/images/**", produces ={MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE})
    public byte[] getbackground(HttpServletRequest request) {
        byte[] bytes={};
        request.getServletPath();
        String  imgurl= FilePath.BACKGROUNDIMG.getValue()+"/"+request.getServletPath();
        imgurl = imgurl.replace("//", "/");
        
        File file = new File(imgurl);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);

            bytes = new byte[0];

            bytes = new byte[inputStream.available()];

            if (inputStream != null) {
                inputStream.read(bytes, 0, inputStream.available());
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("图片下载失败",e.getMessage());
        }finally{
            if (inputStream != null) {
                try{
                    inputStream.close();
                }catch (Exception e){}
            }
        }
        return bytes;
    }
}
