package cn.hmsoft.hmplatform.identity.control;

import java.util.UUID;

import javax.servlet.ServletOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.hmplatform.identity.service.FaceMatchService;

@RestController
public class VoiceControl extends AjaxControl {
	@Autowired
	private FaceMatchService serviceMatch;
	
	/**
	 * @语音输出mp3.
	 * @param text
	 * @param cuid
	 * @return
	 * @throws BusinessException 
	 */
	@RequestMapping("voice/text2audio")
	public void text2Audio(String text, String cuid) throws BusinessException {
		if (StringHelper.isEmpty(cuid)) {
			cuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
		}
        ServletOutputStream os = null;
		try {
			os = this.getResponse().getOutputStream();
	        this.getResponse().setContentType("application/x-download");
	        this.getResponse().addHeader("Content-Disposition",
					"attachment;filename=\"" + new String("au.mp3".getBytes("utf8"), "ISO_8859_1") + "\"");
	        byte[] bytes = this.serviceMatch.text2AudioBytes(text, cuid);
	        os.write(bytes);
	        os.close();
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}
}
