|
- package com.iformall.controller;
-
- import com.alibaba.fastjson.JSONObject;
- import com.github.pagehelper.PageInfo;
- import com.iformall.annotation.AuthIgnore;
- import com.iformall.common.ErrorCode;
- import com.iformall.common.ResultData;
- import com.iformall.domain.po.base.BaseEntity;
- import com.iformall.domain.po.sm.*;
- import com.iformall.enums.EnumMouldSendType;
- import com.iformall.enums.EnumVideoStatus;
- import com.iformall.enums.EnumaMouldPatchStatus;
- import com.iformall.exception.MallinkException;
- import com.iformall.service.sm.*;
- import com.iformall.video.VideoFactory;
- import com.iformall.video.entity.VideUploadResult;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
- import lombok.SneakyThrows;
- import org.apache.commons.lang3.StringUtils;
- import org.checkerframework.checker.units.qual.A;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.Date;
- import java.util.List;
-
-
- @RestController
- @RequestMapping("/api/userPhotoVideo")
- @Api(description = "模板接口")
- public class PhotoSpeakVideoController extends BaseController {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
-
- @Autowired
- private PhotoSpeakVideoService photoSpeakVideoService;
-
- @Autowired
- private MouldPatchService mouldPatchService;
-
- @Autowired
- private VoiceInfoService voiceInfoService;
-
- @Autowired
- private VideoFactory videoFactory;
-
- @Autowired
- String videoType;
-
- @Autowired
- private MusicInfoService musicInfoService;
-
- @ApiOperation("分页列表接口")
- @GetMapping("list")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
- public ResultData list(@ModelAttribute PhotoSpeakVideo record, Integer pageNum, Integer pageSize) {
- logger.debug("[" + getIpAddr() + "] UserMouldVideoController::list");
- if (record == null) record = new PhotoSpeakVideo();
- record.setUserId(getMemberId());
- record.setSortColumns(BaseEntity.SortField.UpdateDate_DESC);
- final PageInfo<PhotoSpeakVideo> page = photoSpeakVideoService.cListAsPage(record, pageNum, pageSize);
- return new ResultData(page);
- }
-
- @ApiOperation("根据id查询接口")
- @GetMapping("/findById")
- @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
- public ResultData findById(Long id) {
- logger.debug("[" + getIpAddr() + "] MouldPatchController::findById");
- if(id == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"参数为空");
- }
- PhotoSpeakVideo photoSpeakVideo = photoSpeakVideoService.getById(id);
- if(photoSpeakVideo == null || !photoSpeakVideo.getUserId().equals(getMemberId())){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到用户数据");
- }
- if(photoSpeakVideo.getVoiceMouldId() != null){
- VoiceInfo voiceInfo = voiceInfoService.getById(photoSpeakVideo.getVoiceMouldId());
- photoSpeakVideo.setVoiceInfo(voiceInfo);
- }
- return new ResultData(photoSpeakVideo);
- }
-
- @ApiOperation("根据id删除接口")
- @GetMapping("/del")
- @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
- public ResultData del(Long id) {
- logger.debug("[" + getIpAddr() + "] MouldPatchController::del");
- if(id == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"参数为空");
- }
- PhotoSpeakVideo photoSpeakVideo = photoSpeakVideoService.getById(id);
- if(photoSpeakVideo == null || !photoSpeakVideo.getUserId().equals(getMemberId())){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到用户数据");
- }
- // if(EnumVideoStatus.ing.getCode().equals(mouldVideo.getVideoStatus())
- // || EnumVideoStatus.success.getCode().equals(mouldVideo.getVideoStatus())
- // || EnumVideoStatus.upload_ing.getCode().equals(mouldVideo.getVideoStatus())
- // || EnumVideoStatus.upload_fail.getCode().equals(mouldVideo.getVideoStatus())){
- // return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频生成中");
- // }
- photoSpeakVideoService.deleteById(id);
- return new ResultData();
- }
-
- @AuthIgnore
- @ApiOperation("新增接口")
- @PostMapping("saveOrUpdate")
- public ResultData saveOrUpdate(@RequestBody PhotoSpeakVideo record) {
- logger.debug("[" + getIpAddr() + "] MouldPatchController::saveOrUpdate");
- // record.setUserId(getMemberId());
- if(record.getId() != null){
- PhotoSpeakVideo photoSpeakVideo = photoSpeakVideoService.findVideo(record.getId());
- if(photoSpeakVideo == null){
- return new ResultData(ErrorCode.SYS_SERVER_ERROR);
- }
- if(EnumVideoStatus.ing.getCode().equals(photoSpeakVideo.getVideoStatus())
- || EnumVideoStatus.success.getCode().equals(photoSpeakVideo.getVideoStatus())
- || EnumVideoStatus.upload_ing.getCode().equals(photoSpeakVideo.getVideoStatus())
- || EnumVideoStatus.upload_fail.getCode().equals(photoSpeakVideo.getVideoStatus())){
- return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频生成中");
- }
- if(EnumVideoStatus.upload_success.getCode().equals(photoSpeakVideo.getVideoStatus())){
- //上传阿里云状态 生成成功
- return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频已生成完成");
- }
- }
- if(record.getPersonPhotoId() == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未选择照片");
- }
- return photoSpeakVideoService.saveOrUpdate(record);
- }
-
- @ApiOperation("生成视频")
- @PostMapping("createVideo")
- public ResultData create(@RequestBody PhotoSpeakVideo record) {
- logger.debug("[" + getIpAddr() + "] MouldPatchController::saveOrUpdate");
- if(record.getId() == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
- }
- PhotoSpeakVideo mouldVideo = photoSpeakVideoService.getById(record.getId());
-
- logger.info("TEST--"+ JSONObject.toJSONString(mouldVideo));
-
- if(mouldVideo == null || !mouldVideo.getUserId().equals(getMemberId())){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到用户数据");
- }
-
- if(EnumVideoStatus.ing.getCode().equals(mouldVideo.getVideoStatus())
- || EnumVideoStatus.success.getCode().equals(mouldVideo.getVideoStatus())
- || EnumVideoStatus.upload_ing.getCode().equals(mouldVideo.getVideoStatus())
- || EnumVideoStatus.upload_fail.getCode().equals(mouldVideo.getVideoStatus())){
- return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频生成中");
- }
- if(EnumVideoStatus.upload_success.getCode().equals(mouldVideo.getVideoStatus())){
- //上传阿里云状态 生成成功
- return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频已生成完成");
- }
-
- PhotoSpeakVideo mouldVideoUpd = new PhotoSpeakVideo();
- mouldVideoUpd.setId(record.getId());
- mouldVideoUpd.setVideoStatus(EnumVideoStatus.ing.getCode());
- mouldVideoUpd.setVideoMsg("");
- mouldVideoUpd.setCreateVideoDate(new Date());
- photoSpeakVideoService.saveOrUpdate(mouldVideoUpd);
-
- photoSpeakVideoService.createVideo(mouldVideo,true);
-
- return new ResultData();
- }
-
- @ApiOperation("根据id查询接口")
- @GetMapping("/findVideo")
- @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
- public ResultData findVideo(Long id) {
- logger.debug("[" + getIpAddr() + "] MouldPatchController::findVideo");
- PhotoSpeakVideo photoSpeakVideo = photoSpeakVideoService.findVideo(id);
- // if(EnumVideoStatus.upload_ing.getCode().equals(userMouldVideo.getVideoStatus())
- // || EnumVideoStatus.upload_success.getCode().equals(userMouldVideo.getVideoStatus())
- // || EnumVideoStatus.upload_fail.getCode().equals(userMouldVideo.getVideoStatus())){
- // //上传阿里云状态 都是生成成功
- // userMouldVideo.setVideoStatus(EnumVideoStatus.success.getCode());
- // }
- return new ResultData(photoSpeakVideo);
- }
-
- @GetMapping(value = "/videoDetial")
- @ApiOperation("视频详情")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "videoId", value = "视频编号", dataType = "String", paramType = "query", required = true)})
- public ResultData videoDetial(String videoId) {
- try {
- VideUploadResult videoDetail = videoFactory.getExcutor(videoType).getVideoDetailWithCache(videoId);
- return new ResultData(videoDetail);
- } catch (Exception e) {
- logger.error(e.getMessage());
- return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR);
- }
- }
-
-
- @AuthIgnore
- @SneakyThrows
- @GetMapping("exportVideo")
- public void exportVideo(Long id, HttpServletRequest request, HttpServletResponse response) {
- logger.debug("[" + getIpAddr() + "] UserMouldVideoController::exportVideo");
-
- response.reset();
-
- File tmpFile = null;
- OutputStream outputStream = null;
- try {
- outputStream = response.getOutputStream();
- } catch (IOException e) {
- throw new MallinkException(ErrorCode.SYS_SERVER_ERROR);
- }
-
- try{
- if(id == null){
- throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL);
- }
- PhotoSpeakVideo photoSpeakVideo = photoSpeakVideoService.getById(id);
-
- // if(mouldVideo == null || !mouldVideo.getUserId().equals(getMemberId())){
- // throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到用户数据");
- // }
- if(!EnumVideoStatus.upload_success.getCode().equals(photoSpeakVideo.getVideoStatus())){
- throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"视频未生成成功");
- }
-
- //获取响应的输出流
- InputStream inputStream = new URL(photoSpeakVideo.getVideoPlayUrl()).openStream();
-
- //获取从那个字节开始读取文件
- // String rangeString = request.getHeader("Range");
- // if(StringUtils.isNotBlank(rangeString)){
- // tmpFile = FileUtils.createTmpFile(inputStream, mouldVideo.getId().toString(), "mp4");
- // RandomAccessFile targetFile = new RandomAccessFile(tmpFile, "r");
- // long fileLength = targetFile.length();
- // long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
- // //设置内容类型
- // response.setHeader("Content-Type", "video/mp4");
- // //设置此次相应返回的数据长度
- // response.setHeader("Content-Length", String.valueOf(fileLength - range));
- // //设置此次相应返回的数据范围
- // response.setHeader("Content-Range", "bytes "+range+"-"+(fileLength-1)+"/"+fileLength);
- // //返回码需要为206,而不是200
- // response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
- // //设定文件读取开始位置(以字节为单位)
- // targetFile.seek(range);
- //
- // byte[] cache = new byte[1024 * 300];
- // int flag;
- // while ((flag = targetFile.read(cache))!=-1){
- // outputStream.write(cache, 0, flag);
- // }
- // }else{
- response.setHeader("Content-Disposition", "attachment; filename="
- + URLEncoder.encode(photoSpeakVideo.getId()+".mp4", "UTF-8"));
- //解决编码问题
- response.setCharacterEncoding("UTF-8");
- response.setHeader("Content-Type","application/octet-stream");
-
- byte[] cache = new byte[1024 * 300];
- int flag;
- while ((flag = inputStream.read(cache))!=-1){
- outputStream.write(cache, 0, flag);
- }
- // }
-
- }catch(MallinkException e){
- ResultData resultData = new ResultData(e.getErrorCode(), e.getMessage());
- //解决编码问题
- response.setCharacterEncoding("UTF-8");
- response.setHeader("Content-Type","application/json");
- outputStream.write(JSONObject.toJSONString(resultData).getBytes(StandardCharsets.UTF_8));
-
- }finally {
- if (tmpFile != null) {
- tmpFile.delete();
- }
- outputStream.flush();
- outputStream.close();
- }
- }
-
- @AuthIgnore
- @GetMapping(value = "/music")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
- @ApiOperation("获取默认音乐")
- public ResultData music(@ModelAttribute MusicInfo record, Integer pageNum, Integer pageSize) {
- logger.debug("[" + getIpAddr() + "] UserMouldVideoController::userList");
- if (record == null) record = new MusicInfo();
- record.setSortColumns(BaseEntity.SortField.UpdateDate_DESC);
- PageInfo<MusicInfo> music = musicInfoService.music(pageNum, pageSize, record);
- return new ResultData(music);
- }
-
- @GetMapping(value = "/userMusic")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
- @ApiOperation("获取用户音乐")
- public ResultData userMusic(@ModelAttribute MusicInfo record, Integer pageNum, Integer pageSize) {
- logger.debug("[" + getIpAddr() + "] UserMouldVideoController::userMusic");
- if (record == null) record = new MusicInfo();
- record.setUserId(getMemberId());
- record.setSortColumns(BaseEntity.SortField.UpdateDate_DESC);
- PageInfo<MusicInfo> music = musicInfoService.userMusic(pageNum, pageSize, record);
- return new ResultData(music);
- }
-
- @ApiOperation("新增接口")
- @PostMapping("/save")
- public ResultData save(@RequestBody MusicInfo record) {
- logger.debug("[" + getIpAddr() + "] UserMouldVideoController::save");
- if (record == null) record = new MusicInfo();
- record.setType(EnumMouldSendType.build.getCode());
- record.setUserId(getMemberId());
- if (record.getTitle() == null) {
- record.setTitle("用户自建");
- }
- return musicInfoService.saveOrUpdate(record);
- }
-
- @ApiOperation("根据id删除接口")
- @GetMapping("/delete")
- @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
- public ResultData delete(Long id) {
- logger.debug("[" + getIpAddr() + "] PersonPhotoController::delete");
- if(id == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"参数为空");
- }
- MusicInfo musicInfo = musicInfoService.getById(id);
- if(musicInfo == null){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到数据");
- }
- if(!musicInfo.getUserId().equals(getMemberId())){
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"未找到用户数据");
- }
- musicInfoService.deleteById(id);
- return new ResultData();
- }
-
- }
|