From abfad7ffe8b0fdb788664997b23375548f88b90e Mon Sep 17 00:00:00 2001 From: xhxu Date: Thu, 25 May 2023 16:10:38 +0800 Subject: [PATCH] //photoSpeak --- .../controller/PhotoSpeakVideoController.java | 291 ++++++++++++++++++ .../iformall/controller/VideoController.java | 122 ++++++++ .../java/com/iformall/utils/UrlCheck.java | 3 +- 3 files changed, 415 insertions(+), 1 deletion(-) create mode 100644 suimangCApi/src/main/java/com/iformall/controller/PhotoSpeakVideoController.java create mode 100644 suimangCApi/src/main/java/com/iformall/controller/VideoController.java diff --git a/suimangCApi/src/main/java/com/iformall/controller/PhotoSpeakVideoController.java b/suimangCApi/src/main/java/com/iformall/controller/PhotoSpeakVideoController.java new file mode 100644 index 0000000..8fa9ae7 --- /dev/null +++ b/suimangCApi/src/main/java/com/iformall/controller/PhotoSpeakVideoController.java @@ -0,0 +1,291 @@ +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.PhotoSpeakVideo; +import com.iformall.domain.po.sm.UserMouldVideo; +import com.iformall.enums.EnumVideoStatus; +import com.iformall.exception.MallinkException; +import com.iformall.service.sm.MouldPatchService; +import com.iformall.service.sm.PhotoSpeakVideoService; +import com.iformall.service.sm.UserMouldVideoService; +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.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; + + +@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 VideoFactory videoFactory; + + @Autowired + String videoType; + + @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 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(),"未找到用户数据"); + } + 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(); + } + + @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); + + 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(); + } + } + + +} diff --git a/suimangCApi/src/main/java/com/iformall/controller/VideoController.java b/suimangCApi/src/main/java/com/iformall/controller/VideoController.java new file mode 100644 index 0000000..1bc924b --- /dev/null +++ b/suimangCApi/src/main/java/com/iformall/controller/VideoController.java @@ -0,0 +1,122 @@ +package com.iformall.controller; + +import com.iformall.common.ErrorCode; +import com.iformall.common.ResultData; +import com.iformall.video.VideoFactory; +import com.iformall.video.entity.VideUploadResult; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.Map; + +/** + * @author gongbiao + */ +@RestController +@RequestMapping("api/video") +public class VideoController extends BaseController { + private Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + VideoFactory videoFactory; + + @Autowired + String videoType; + + /** + * 上传视频 + * + * @param multiReq + * @return + * @throws Exception + */ + @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data") + @ApiOperation("上传视频") + public ResultData upload(@RequestParam("file") MultipartFile multiReq,@RequestParam Map param) { + try { + long size = multiReq.getSize(); + String title = param.get("title"); + int dot = multiReq.getOriginalFilename().lastIndexOf('.'); + String fileFormat = ""; + if (dot >= 0) { + fileFormat = multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); + if(StringUtils.isBlank(title)){ + title = multiReq.getOriginalFilename().substring(0,dot); + } + } + if(!fileFormat.endsWith("mp4") && !fileFormat.endsWith("mp3")){ + return new ResultData(ErrorCode.PICTURE_ENDWIDTH_ERROR); + } + VideUploadResult result = videoFactory.getExcutor(videoType).uploadVideoStream(title, multiReq.getInputStream(),fileFormat); + result.setSize(size); + return new ResultData(result); + } catch (Exception e) { + logger.error(e.getMessage()); + return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); + } + } + + /** + * 上传视频 + * + * @param + * @return + * @throws Exception + */ + @GetMapping(value = "/uploadProgress") + @ApiOperation("上传视频进度") + @ApiImplicitParams({ + @ApiImplicitParam(name = "videoId", value = "视频编号", dataType = "String", paramType = "query", required = true)}) + public ResultData uploadProgress(String videoId) { + try { + String result = videoFactory.getExcutor(videoType).getVedioUploadProgress(videoId); + return new ResultData(result); + } catch (Exception e) { + logger.error(e.getMessage()); + return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); + } + } + + /** + * 上传视频 + * + * @param + * @return + * @throws Exception + */ + @GetMapping(value = "/videoContentLength") + @ApiOperation("上传视频进度") + @ApiImplicitParams({ + @ApiImplicitParam(name = "videoId", value = "视频编号", dataType = "String", paramType = "query", required = true)}) + public ResultData videoContentLength(String videoId) { + try { + String result = videoFactory.getExcutor(videoType).getVedioContentLength(videoId); + return new ResultData(result); + } catch (Exception e) { + logger.error(e.getMessage()); + return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); + } + } + + @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); + } + } + +} diff --git a/suimangCApi/src/main/java/com/iformall/utils/UrlCheck.java b/suimangCApi/src/main/java/com/iformall/utils/UrlCheck.java index 37f7a51..bdfb789 100644 --- a/suimangCApi/src/main/java/com/iformall/utils/UrlCheck.java +++ b/suimangCApi/src/main/java/com/iformall/utils/UrlCheck.java @@ -9,7 +9,8 @@ public class UrlCheck { return url.contains("awsFileUpload") || url.contains("awsFilesUpload") || url.contains("awsImgUpload") - || url.contains("getCarStopFee"); + || url.contains("getCarStopFee") + || url.contains("/video/upload"); } }