diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/video/VideoController.java b/mallinkAdmin/src/main/java/com/iformall/controller/video/VideoController.java index 597d9d55e..e4e922d52 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/video/VideoController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/video/VideoController.java @@ -90,8 +90,7 @@ public class VideoController extends BaseController { */ @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data") @ApiOperation("上传视频") - @SystemControllerLog(description = "文件上传") - public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq,@RequestParam Map param) { + public ResultData upload(@RequestParam("file") MultipartFile multiReq,@RequestParam Map param) { try { String title = param.get("title"); VideUploadResult result = videoFactory.getExcutor(videoType).uploadLocalVideo(title, multiReq.getInputStream()); @@ -101,5 +100,26 @@ public class VideoController extends BaseController { return new ResultData(ErrorCode.PICTURE_ANALYZING_ERROR); } } + + /** + * 上传视频 + * + * @param multiReq + * @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); + } + } } diff --git a/mallinkVideo/src/main/java/com/iformall/video/VideoExcutor.java b/mallinkVideo/src/main/java/com/iformall/video/VideoExcutor.java index 3fa6395cb..fefce84ac 100644 --- a/mallinkVideo/src/main/java/com/iformall/video/VideoExcutor.java +++ b/mallinkVideo/src/main/java/com/iformall/video/VideoExcutor.java @@ -17,4 +17,6 @@ public interface VideoExcutor { //ext,后缀名 public VideUploadResult uploadNetVideo(String title,String url,String ext); + + public String getVedioUploadProgress(String videoId); } diff --git a/mallinkVideo/src/main/java/com/iformall/video/VideoFactory.java b/mallinkVideo/src/main/java/com/iformall/video/VideoFactory.java index 5f1ac6035..e8afbaddb 100644 --- a/mallinkVideo/src/main/java/com/iformall/video/VideoFactory.java +++ b/mallinkVideo/src/main/java/com/iformall/video/VideoFactory.java @@ -4,6 +4,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import com.iformall.video.aliyun.AliyunVideoExcutor; diff --git a/mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java b/mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java index d95fe4f24..b137e7e29 100644 --- a/mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java +++ b/mallinkVideo/src/main/java/com/iformall/video/aliyun/AliyunVideoExcutor.java @@ -7,6 +7,8 @@ import java.io.IOException; import java.io.InputStream; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import com.aliyun.vod.upload.resp.UploadStreamResponse; @@ -28,6 +30,10 @@ public class AliyunVideoExcutor implements VideoExcutor { @Autowired AliyunVideoConfig config; + @Autowired + @Qualifier("objectCommonRedisTemplate") + RedisTemplate redisTemplate; + private String getUrlByVeidoId(String videoId) { try { return AliyunVedioServer.getVedioUrl(config, videoId); @@ -41,7 +47,7 @@ public class AliyunVideoExcutor implements VideoExcutor { public VideUploadResult uploadLocalVideo(String title,String localFile) { VideUploadResult result = new VideUploadResult(); try { - UploadVideoResponse response =AliyunVedioUpload.uploadVideo(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, localFile); + UploadVideoResponse response =AliyunVedioUpload.uploadVideo(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, localFile,redisTemplate); if (response.isSuccess()) { result.setVideoId(response.getVideoId()); result.setVideoUrl(getUrlByVeidoId(response.getVideoId())); @@ -64,7 +70,7 @@ public class AliyunVideoExcutor implements VideoExcutor { VideUploadResult result = new VideUploadResult(); UploadStreamResponse response; try { - response = AliyunVedioUpload.uploadStream(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, "", inputStream); + response = AliyunVedioUpload.uploadStream(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, "", inputStream,redisTemplate); if (response.isSuccess()) { result.setVideoId(response.getVideoId()); result.setVideoUrl(getUrlByVeidoId(response.getVideoId())); @@ -91,7 +97,7 @@ public class AliyunVideoExcutor implements VideoExcutor { @Override public VideUploadResult uploadNetVideo(String title,String url,String ext) { VideUploadResult result = new VideUploadResult(); - UploadURLStreamResponse response = AliyunVedioUpload.uploadURLStream(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, url, ext); + UploadURLStreamResponse response = AliyunVedioUpload.uploadURLStream(config.getAccessKeyId(), config.getAccessKeySecret(),config.getRegionId(), title, url, ext,redisTemplate); if (response.isSuccess()) { result.setVideoId(response.getVideoId()); result.setVideoUrl(getUrlByVeidoId(response.getVideoId())); @@ -103,4 +109,9 @@ public class AliyunVideoExcutor implements VideoExcutor { } return result; } + + @Override + public String getVedioUploadProgress(String videoId) { + return AliyunVedioUpload.getUploadProgress(redisTemplate, videoId); + } } diff --git a/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java index d0d30e682..a0fd76045 100644 --- a/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java +++ b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/AliyunVedioUpload.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; +import org.springframework.data.redis.core.RedisTemplate; + import com.aliyun.vod.upload.impl.UploadAttachedMediaImpl; import com.aliyun.vod.upload.impl.UploadImageImpl; import com.aliyun.vod.upload.impl.UploadM3u8FileImpl; @@ -103,7 +105,7 @@ public class AliyunVedioUpload { // 任何上传方式文件名必须包含扩展名 String fileName = "/Users/test/video/test.mp4"; // 本地文件上传 - uploadVideo(accessKeyId, accessKeySecret,"cn-beijing", title, fileName); + uploadVideo(accessKeyId, accessKeySecret,"cn-beijing", title, fileName,null); // 待上传视频的网络流地址 String url = "http://test.aliyun.com/video/test.mp4"; @@ -111,7 +113,7 @@ public class AliyunVedioUpload { // 2.网络流上传 // 文件扩展名,当url中不包含扩展名时,需要设置该参数 String fileExtension = "mp4"; - uploadURLStream(accessKeyId, accessKeySecret, "cn-beijing",title, url, fileExtension); + uploadURLStream(accessKeyId, accessKeySecret, "cn-beijing",title, url, fileExtension,null); // 3.文件流上传 // testUploadFileStream(accessKeyId, accessKeySecret, title, fileName); @@ -130,7 +132,7 @@ public class AliyunVedioUpload { } catch (IOException e) { e.printStackTrace(); } - uploadStream(accessKeyId, accessKeySecret, "cn-beijing",title, fileName, inputStream); + uploadStream(accessKeyId, accessKeySecret, "cn-beijing",title, fileName, inputStream,null); // 二、图片上传 @@ -161,7 +163,8 @@ public class AliyunVedioUpload { * @param title * @param fileName */ - public static UploadVideoResponse uploadVideo(String accessKeyId, String accessKeySecret,String regionId, String title, String fileName) { + public static UploadVideoResponse uploadVideo(String accessKeyId, String accessKeySecret,String regionId, String title, String fileName, + RedisTemplate redisTemplate) { UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName); /* 可指定分片上传时每个分片的大小,默认为2M字节 */ request.setPartSize(2 * 1024 * 1024L); @@ -219,7 +222,8 @@ public class AliyunVedioUpload { * @param fileName * @param url */ - public static UploadURLStreamResponse uploadURLStream(String accessKeyId, String accessKeySecret,String regionId, String title, String url, String fileExtension) { + public static UploadURLStreamResponse uploadURLStream(String accessKeyId, String accessKeySecret,String regionId, String title, + String url, String fileExtension,RedisTemplate redisTemplate) { UploadURLStreamRequest request = new UploadURLStreamRequest(accessKeyId, accessKeySecret, title, url); /* 文件扩展名*/ @@ -271,7 +275,8 @@ public class AliyunVedioUpload { * @param title * @param fileName */ - private static void uploadFileStream(String accessKeyId, String accessKeySecret,String regionId, String title, String fileName) { + private static void uploadFileStream(String accessKeyId, String accessKeySecret,String regionId, String title, String fileName, + RedisTemplate redisTemplate) { UploadFileStreamRequest request = new UploadFileStreamRequest(accessKeyId, accessKeySecret, title, fileName); /* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/ //request.setShowWaterMark(true); @@ -323,7 +328,8 @@ public class AliyunVedioUpload { * @param fileName * @param inputStream */ - public static UploadStreamResponse uploadStream(String accessKeyId, String accessKeySecret,String regionId, String title, String fileName, InputStream inputStream) { + public static UploadStreamResponse uploadStream(String accessKeyId, String accessKeySecret,String regionId, String title, + String fileName, InputStream inputStream,RedisTemplate redisTemplate) { UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream); /* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/ //request.setShowWaterMark(true); @@ -365,7 +371,7 @@ public class AliyunVedioUpload { * @param accessKeyId * @param accessKeySecret */ - public static void uploadImageLocalFile(String accessKeyId, String accessKeySecret,String regionId) { + public static void uploadImageLocalFile(String accessKeyId, String accessKeySecret,String regionId,RedisTemplate redisTemplate) { /* 图片类型(必选)取值范围:default(默认),cover(封面),watermark(水印)*/ String imageType = "cover"; UploadImageRequest request = new UploadImageRequest(accessKeyId, accessKeySecret, imageType); @@ -409,7 +415,7 @@ public class AliyunVedioUpload { * @param accessKeyId * @param accessKeySecret */ - public static void uploadImageStream(String accessKeyId, String accessKeySecret,String regionId) { + public static void uploadImageStream(String accessKeyId, String accessKeySecret,String regionId,RedisTemplate redisTemplate) { /* 图片类型(必选)取值范围:default(默认),cover(封面),watermark(水印)*/ String imageType = "cover"; UploadImageRequest request = new UploadImageRequest(accessKeyId, accessKeySecret, imageType); @@ -682,4 +688,8 @@ public class AliyunVedioUpload { } return null; } + + public static String getUploadProgress(RedisTemplate redisTemplate,String videoId) { + return UploadCacheHelper.getUploadProgress(redisTemplate, PutObjectProgressListener.PRE_VIDEO_PROGRESS+videoId); + } } diff --git a/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/PutObjectProgressListener.java b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/PutObjectProgressListener.java index 2868eb70b..c5b9565a3 100644 --- a/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/PutObjectProgressListener.java +++ b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/PutObjectProgressListener.java @@ -1,5 +1,7 @@ package com.iformall.video.aliyun.sdk.upload; +import org.springframework.data.redis.core.RedisTemplate; + import com.aliyun.oss.event.ProgressEvent; import com.aliyun.oss.event.ProgressEventType; import com.aliyun.vod.upload.impl.VoDProgressListener; @@ -33,54 +35,63 @@ public class PutObjectProgressListener implements VoDProgressListener { * 图片ID */ private String imageId; - + + public static final String PRE_VIDEO_PROGRESS = "video:progress:"; + private RedisTemplate redisTemplate; + public void progressChanged(ProgressEvent progressEvent) { long bytes = progressEvent.getBytes(); ProgressEventType eventType = progressEvent.getEventType(); switch (eventType) { // 开始上传事件 case TRANSFER_STARTED_EVENT: - if (videoId != null) { - System.out.println("qqqqStart to upload videoId " + videoId + "......"); - } - if (imageId != null) { - System.out.println("aaaStart to upload imageId " + imageId + "......"); - } + if (null != redisTemplate) { + if (videoId != null) { + UploadCacheHelper.cacheStart(redisTemplate, PRE_VIDEO_PROGRESS+videoId); + //System.out.println("qqqqStart to upload videoId " + videoId + "......"); + } + if (imageId != null) { + //System.out.println("aaaStart to upload imageId " + imageId + "......"); + } + } break; // 计算待上传文件总大小事件通知,只有调用本地文件方式上传时支持该事件 case REQUEST_CONTENT_LENGTH_EVENT: this.totalBytes = bytes; - System.out.println(this.totalBytes + "bytes in total will be uploaded to OSS."); + //System.out.println(this.totalBytes + "bytes in total will be uploaded to OSS."); break; // 已经上传成功文件大小事件通知 case REQUEST_BYTE_TRANSFER_EVENT: this.bytesWritten += bytes; if (this.totalBytes != -1) { int percent = (int) (this.bytesWritten * 100.0 / this.totalBytes); - System.out.println(bytes + " bytes have been written at this time, upload progress: " + - percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")"); + //System.out.println(bytes + " bytes have been written at this time, upload progress: " + + // percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")"); } else { - System.out.println(bytes + " bytes have been written at this time, upload sub total : " + - "(" + this.bytesWritten + ")"); + //System.out.println(bytes + " bytes have been written at this time, upload sub total : " + + // "(" + this.bytesWritten + ")"); } + UploadCacheHelper.cacheProgress(redisTemplate, PRE_VIDEO_PROGRESS+videoId,String.valueOf(this.bytesWritten)); break; // 文件全部上传成功事件通知 case TRANSFER_COMPLETED_EVENT: this.succeed = true; if (videoId != null) { - System.out.println("Succeed to upload videoId " + videoId + " , " + this.bytesWritten + " bytes have been transferred in total."); + UploadCacheHelper.cacheComplete(redisTemplate, PRE_VIDEO_PROGRESS+videoId); + //System.out.println("Succeed to upload videoId " + videoId + " , " + this.bytesWritten + " bytes have been transferred in total."); } if (imageId != null) { - System.out.println("Succeed to upload imageId " + imageId + " , " + this.bytesWritten + " bytes have been transferred in total."); + //System.out.println("Succeed to upload imageId " + imageId + " , " + this.bytesWritten + " bytes have been transferred in total."); } break; // 文件上传失败事件通知 case TRANSFER_FAILED_EVENT: if (videoId != null) { + UploadCacheHelper.cacheProgress(redisTemplate, PRE_VIDEO_PROGRESS+videoId,String.valueOf(this.bytesWritten)); System.out.println("Failed to upload videoId " + videoId + " , " + this.bytesWritten + " bytes have been transferred."); } if (imageId != null) { - System.out.println("Failed to upload imageId " + imageId + " , " + this.bytesWritten + " bytes have been transferred."); + //System.out.println("Failed to upload imageId " + imageId + " , " + this.bytesWritten + " bytes have been transferred."); } break; diff --git a/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/UploadCacheHelper.java b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/UploadCacheHelper.java new file mode 100644 index 000000000..ef409b078 --- /dev/null +++ b/mallinkVideo/src/main/java/com/iformall/video/aliyun/sdk/upload/UploadCacheHelper.java @@ -0,0 +1,37 @@ +package com.iformall.video.aliyun.sdk.upload; + +import java.util.concurrent.TimeUnit; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; + +public class UploadCacheHelper { + + + public static void cacheStart(RedisTemplate template,String key) { + ValueOperations operations = template.opsForValue(); + operations.set(key, "begin", 24*3600,TimeUnit.SECONDS); + } + + public static void cacheComplete(RedisTemplate template,String key) { + ValueOperations operations = template.opsForValue(); + operations.set(key, "complete", 24*3600,TimeUnit.SECONDS); + } + + public static void cacheProgress(RedisTemplate template,String key,String bytes) { + ValueOperations operations = template.opsForValue(); + operations.set(key, bytes, 24*3600,TimeUnit.SECONDS); + } + + public static String getUploadProgress(RedisTemplate template,String key) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + return (String)o; + } + } + return null; + } + +}