package me.qwq.doghouse.controller.blog;

import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
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 com.google.code.kaptcha.impl.DefaultKaptcha;

import me.qwq.doghouse.controller.common.BaseController;
import me.qwq.doghouse.util.SessionUtils;

@RestController
@RequestMapping("/captcha")
public class KaptchaController extends BaseController {
    
    @Autowired
    DefaultKaptcha kaptcha;
    
    @GetMapping(value="/get", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getCaptcha() throws Exception {
        String createText = kaptcha.createText();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        session.setAttribute(SessionUtils.CAPTCHA, createText);
        ImageIO.write(kaptcha.createImage(createText), "jpg", os);
        byte[] result = os.toByteArray();
        os.close();
        return result;
    }
}
